1.Nginx安装
sudo apt-get install nginx
如果安装了apache并占用了80端口会导致报错,先停掉apache服务再重新安装即可。
2.申请ssl证书,腾讯云、阿里云都有免费的https证书申请,这里用的是腾讯云申请的证书,证书申请通过之后可以下载到crt和key两个后缀的文件。
3.修改Nginx配置文件
/ect/nginx/nginx.conf
nginx 常用命令:
关闭命令: nginx -s stop
退出命令: nginx -s quit 等程序执行完毕后关闭
动态加载配置文件 nginx -s reload可以不关闭Nginx的情况下更新配置文件
查看Nginx版本: nginx -v
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
###
upstream django {
server 0.0.0.0:8000; #web的socket端口
}
server{
listen 443;
access_log /path/to/project/log.txt; # Nginx日志配置
server_name www.demo.cn;
charset utf-8;
ssl on;
root html;
index index.html index.htm;
ssl_certificate /etc/nginx/ssl/www.demo.cn_bundle.crt; # crt 文件路径
ssl_certificate_key /etc/nginx/ssl/www.demo.cn.key; # key 文件路径
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
include uwsgi_params;
uwsgi_pass 0.0.0.0:8000;
uwsgi_param UWSGI_SCRIPT /path/to/project/wxapp.wsgi:application;
uwsgi_param UWSGI_CHDIR /path/to/project/wxapp;
index index.html index.htm;
client_max_body_size 35m;
root /path/to/project//wxapp; #django项目的根目录
uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
}
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
}
}
4.uwsgi安装和配置
uwsgi常用命令:
uwsgi --ini wxapp.ini 通过.ini配置文件启动uwsgi服务
uwsgi --reload uwsgi.pid 通过.pid文件重载uwsgi服务
[uwsgi]
socket = 0.0.0.0:8000
# Django-related settings
# the django project directory (full path)
chdir = /path/to/project
# Django's wsgi file
module = wxapp.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 2
threads = 2
max-requests = 6000
# ... with appropriate permissions - may be needed
chmod-socket = 664
# clear environment on exit
vacuum = true
static-map=/static=/path/to/project/static
# uwsgi重载
pidfile = /path/to/project/uwsgi.pid
#uwsgi日志输出
daemonize=/path/to/project/wxapp.log