安装包
pip install gunicorn supervisor
gunicorn
确保
django项目中有
wsgi.py
文件
通过gunicorn启动django项目(project需要换为相应的名称)
gunicorn --chdir project_dir --pythonpath venv/bin/python -w4 -b0.0.0.0:8090 project_name.wsgi:application
如果启动失败,就kill
lsof -i:8090
kill pid
访问测试:
http://0.0.0.0:8090nginx
找到nginx的配置文件
nginx -t
在末尾添加一下内容, 当然也可以单独配置
http {
server {
listen 8000; # nginx的监听端口
server_name localhost;
location ^~ /static/ {
root /project; # 此处配置静态文件路径,不带static
}
location / {
proxy_pass http://127.0.0.1:8090; # guncorn启动django的监听路径和端口
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
nginx的基本操作
service nginx start # 启动
service nginx stop # 关闭
service nginx restart # 重启
配置完了执行:
nginx -t # 检查语法
service nginx restart # 重启nginx
http://127.0.0.1:8000/ supervisor
配置supervisor
[program:project_name]
directory=project_dir
command=gunicorn --pythonpath venv/bin/python -w4 -b0.0.0.0:8090 project_name.wsgi:application
autostart=true
autorestart=true
stdout_logfile=project.log
stderr_logfile=project.err ; stderr log path, NONE for none; default AUTO
redirect_stderr=true ; redirect proc stderr to stdout (default false)
stopsignal=QUIT ; signal used to kill process (default TERM)
先将之前启动的gunicorn关闭, 再启动supervisor,这样可以确保程序异常退出后自动重启
python supervisord -c supervisord.conf
参考