天天看點

python程式設計:linux環境gunicorn+nginx部署django項目gunicornnginxsupervisor

安裝包

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:8090

nginx

找到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      

參考

  1. django2.0+uwsgi+nginx部署
  2. 檢視nginx配置檔案路徑
  3. mac環境composer建立php的symfony項目并用nginx配置
  4. python程式設計:mac環境gunicorn+nginx部署flask項目
  5. Django 部署(Nginx)
  6. django 中靜态檔案配置 static