Django中使用Nginx与uwsgi将项目部署上线至服务器
1.确保python已经搭建(推荐建立虚拟环境)
2.确保python库中存在uwsgi
3.确保Nginx已经正确安装
提醒:注意要将自己云服务器上的端口开启
uwsgi参数
例如:
uwsgi --chdir /DjangoProject/performanceAppraisal/ --module performanceAppraisal.wsgi:application --http :8000
其中标红内容为你的django项目路径,标橙内容为该django项目里面和项目名相同的子文件夹。
由于uwsgi单处理动态文件,所以需要设置nginx来配合uwsgi实现项目完整部署。
安装nginx
sudo apt-get install nginx
nginx目录
注意:其中site-enabled是已经启用的配置,sites-available待启用的配置
Nginx配置proxy_pass转发的/路径问题:https://blog.51cto.com/wangwei007/1103734
上面链接是对于 proxy_pass 的设置
这边我使用的是http而不是使用socket进行转发
upstream appraisal { #server 127.0.0.1:8002; server 133.3.33.20:8000; keepalive 300; } server { listen 80; server_name 133.3.33.20; # substitute your machine's IP address or FQDN charset utf-8; client_max_body_size 75M; # adjust to taste #admin中的样式 location /media { alias /DjangoProject/performanceAppraisal/media; # your Django project's media files - amend as required } location /static { alias /DjangoProject/performanceAppraisal/static; # your Django project's static files - amend as required } location / { root /DjangoProject/performanceAppraisal/dist/; index index.html index.htm; try_files $uri $uri/ /index.html; } #这个是你项目url location ^~ /appraisal/ { #rewrite ^.+appraisal/?(.*)$ /appraisal/$1 break; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://appraisal/; } #这个是将django后台开启 location ^~ /admin/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8000; } }
创建软链接
ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/mysite.conf
检查是否成功
nginx -t
service nginx start #开启
service nginx reload #重新加载
service nginx restart #重新开始
接下来为了方便启动uwsgi,我们将命令封装成一个文件,下次便以文件的方式启动uwsgi
[uwsgi]
chdir = /DjangoProject/performanceAppraisal/
module = performanceAppraisal.wsgi:application
#wsgi-file=/DjangoProject/performanceAppraisal/performanceAppraisal/wsgi.py #module替代了wsgi-file这句话
master =True
harakiri = 60
http = :8000
pidfile = /home/mysite_uwsgi/master.pid #这个文件可以使用uwsgi --pid等命令重启或者关闭
daemonize = /home/mysite_uwsgi/mysite.log
vacuum = True
home为虚拟环境地址,如果不指定默认使用系统环境
以文件方式启动uwsgi
uwsgi --ini /home/mysite_uwsgi/mysite.ini
查看是否开启,一般来说正常开启之后会有多个进程
uwsgi命令
uwsgi --reload /home/mysite_uwsgi/master.pid
uwsgi --stop /home/mysite_uwsgi/master.pid
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?