以下内容為原創,轉載請注明出處!
先前一直用的apache部署django項目,檢視連結位址:https://www.520pf.cn/article/22.html 。這次幫同僚用nginx部署服務,特此記錄下過程:
環境:CentOS Linux release 7.2.1511 (Core)
django2.1(項目名稱box,項目位址: /opt/box/box)
Vue
python3.6
uwsgi配置,wsgi檔案目錄/opt/box/box/box/wsgi.py[uwsgi]
# 項目目錄
chdir=/opt/box/box/
# 指定項目的application
module=box.wsgi:application
# 指定sock的檔案路徑, 為什麼用sock不用下面http,是因為性能問題,二進制肯定比http協定快
socket=/opt/box/box/uwsgi.sock
# 程序個數
workers=2
pidfile=/opt/box/box/uwsgi.pid
# 指定IP端口,伺服器内網IP
# http=172.16.0.3:9003
# 指定靜态檔案
static-map=/static=/opt/box/box/static
# 指定權限
chmod-socket = 777
# 啟動uwsgi的使用者名和使用者組
uid=root
gid=root
# 啟用主程序
master=true
# 自動移除unix Socket和pid檔案當服務停止的時候
vacuum=true
# 序列化接受的内容,如果可能的話
thunder-lock=true
# 啟用線程
enable-threads=true
# 設定自中斷時間
harakiri=30
# 設定緩沖
post-buffering=4096
# 設定日志目錄
# daemonize=/opt/box/box/log/uwsgi.log
背景接口nginx配置,vim /etc/nginx/conf.d/box_api.confserver {
listen 9003;
server_name www.xxxx.com xxxx.com;
access_log /var/log/nginx/access.log main; # Nginx日志配置
location / {
include uwsgi_params;
uwsgi_connect_timeout 30;
uwsgi_pass unix:/opt/box/box/uwsgi.sock; # 指定uwsgi的sock檔案所有動态請求就會直接丢給他
}
# 指定靜态檔案路徑
location /static/ {
alias /opt/box/box/static/;
index index.html index.htm;
}
}
前端vue nginx配置,vim /etc/nginx/conf.d/box_vue.confserver {
listen 80;
server_name xxxx.com www.xxxx.com;
# 指定vue編譯後的dist檔案位址
root /opt/box-vue/dist/;
index index.html;
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt){
root /opt/box-vue/dist/;
}
location / {
try_files $uri $uri/ @router;
index index.html;
}
location @router {
rewrite ^.*$ /index.html last;
}
}
supervisor配置, vim /etc/supervisord.d/box_web.ini[program:box.web]
command=/opt/box/box_env/bin/uwsgi --ini /opt/box/box/uwsgi.ini
numprocs=1
directory=/opt/box/box
stdout_logfile=/opt/box/box/log/supervisord.log
stderr_logfile=/opt/box/box/log/supervisord.log
stopasgroup=true
killasgroup=true
autostart=true
autorestart=true
startsecs=10
stopwaitsecs = 120
priority=998
environment=PYTHONPATH='$PYTHONPATH:/opt/box/box'
啟動supervisor:supervisord -c /etc/supervisord.conf
supervisorctl
start box.web
啟動nginx:/bin/systemctl reload nginx.service
檢視:背景api位址:www.xxxx.com:9003
前端位址(項目網頁入口):www.xxxx.com