以下内容为原创,转载请注明出处!
先前一直用的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.conf
server {
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.conf
server {
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