安装
1
2
|
apt install nginx
pip install uwsgi supervisord gunicorn
|
uwsgi.ini配置文件如下:
1
2
3
4
5
6
7
|
[uwsgi]
socket = 127.0.0.1:8001
chdir = /var/www/ ; Django项目地址
wsgi-file = web/wsgi.py
processes = 4
threads = 2
stats = 127.0.0.1:9001
|
/etc/nginx/nginx.conf配置文件,只需要将server替换为如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
server {
listen 80;
server_name 127.0.0.1;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
proxy_read_timeout 1200;
# uwsgi和gunicorn只能配置其中一个,gunicorn更好
# include uwsgi_params;
# uwsgi_pass 127.0.0.1:8001; # 这里指向uwsgi host的服务地址,uwsgi_pass的值必须和uwsgi.ini中socket的值一致
proxy_pass http://127.0.0.1:8001; # 这里指向gunicorn host的服务地址, proxy_pass的值必须和gunicorn的-b一致,且前面需要加http://
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /media {
alias /var/www/project/media; # 媒体文件配置
}
location /static {
alias /var/www/project/static; # 静态文件配置
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
|
- 另外,还需要将配置文件开头的user改为使用服务的用户,如root。*
- 可以指定自己的日志路径及格式:*
1
2
3
4
5
6
7
|
error_log /var/www/project/log/nginx-error.log;
http {
log_format main '$remote_addr ^A $time_local ^A $request ^A '
'$status ^A $body_bytes_sent ^A $http_referer ^A '
'$upstream_response_time ^A $request_time';
access_log /var/www/project/log/nginx-access.log main;
}
|
nginx常用命令如下:
1
2
3
4
|
/usr/sbin/nginx # 启动
/usr/sbin/nginx -s reload # 重载配置后重启
/usr/sbin/nginx -s stop # 快速停止
/usr/sbin/nginx -s quit # 完全停止,实测若修改日志配置后,需要完全停止后重启,才生效。
|
supervisord.conf配置文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[program:project]
command=/var/www/project/venv/bin/gunicorn --chdir /var/www/project --pythonpath /var/www/project/venv -w4 -b 127.0.0.1:8001 project.wsgi:application ; supervisor启动命令
directory=/var/www/project ; 项目的文件夹路径
timeout=60*60
startsecs=0 ; 启动时间
stopwaitsecs=0 ; 终止等待时间
autostart=true ; 是否自动启动
autorestart=false ; 是否自动重启
stderr_logfile=/var/www/project/log/gunicorn.log ; log 日志
;gunicorn的方式性能更好,不用uwsgi的话,可以忽略
;[program:uwsgi]
;command=/var/www/project/venv/bin/uwsgi /var/www/project/uwsgi.ini ; 启动命令
;directory=/var/www/project
;startsecs=0
|
- 目前gunicorn未成功,先用uwsgi的方式代替 *
supervisord常用命令如下:
1
2
3
4
5
|
echo_supervisord_conf > supervisord.conf 生成配置文件
supervisorctl status # 查看守护进程的状态
supervisorctl reload # 更新新的配置supervisord.conf到supervisord
supervisorctl restart # 重启守护进程
supervisorctl update # supervisorctl reload + supervisorctl restart
|