Nginx+uWSGI或fastcgi部署Django项目
nginx+uWSGI
ubuntu下先安装下C编译器和Python环境:
1 | sudo apt - get install build - essential python - dev |
使用pip安装uWSGI:
1 | pip install uwsgi |
nginx配置:
可以单独为站点设置一个配置文件:
1 | sudo vim / etc / nginx / sites - enabled / mysite |
或者直接在nginx.conf中设置:
1 | sudo vim / etc / nginx / nginx.conf |
设置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | server { listen 80 ; ## listen for ipv4; this line is default and implied #listen [::]:80 default ipv6only=on; ## listen for ipv6 server_name 站点域名; location / { uwsgi_pass 127.0 . 0.1 : 8080 ; include uwsgi_params; } #设置该网站应用中所需要的静态文件的根目录,需要将admin和用到的第三方库像restframework的静态文件都放到此目录中 location ~ / static / { root / home / user / mysite / ; #项目地址 # root html; # index index.html index.htm; break ; } #设置媒体文件的根目录 location ~ / media / { root / home / user / mysite / ; # root html; # index index.html index.htm; break ; } } |
自己电脑上搭建localhost服务器时,注意别被/etc/nginx/sites-enabled/default中的配置覆盖掉了,最好将其注释掉。
然后设置uWSGI,建立文件myfile.ini:
1 2 3 4 5 6 7 8 9 | [uwsgi] socket = 127.0.0.1:8080 #与nginx配置中的uwsgi_pass相同 chdir = /home/user/mysite/ #项目地址 wsgi-file = mysite/wsgi.py processes = 4 threads = 2 pidfile=/tmp/project-master.pid stats = 127.0.0.1:9191 virtualenv = <path to env> #virtualenv |
其中wsgi-file是django(1.4版本以上)项目自动建立的文件,里面内容为:
1 2 3 4 5 | import os os.environ.setdefault( "DJANGO_SETTINGS_MODULE" , "weixian.settings" ) from django.core.wsgi import get_wsgi_application application = get_wsgi_application() |
如果django版本过低无此文件的话,可以自己建立,或者在myfile.ini中设置env,module,pythonpath:
1 2 3 4 5 6 7 8 9 | [uwsgi] socket = 127.0 . 0.1 : 8080 chdir = / home / user / mysite / pythonpath = .. env = DJANGO_SETTINGS_MODULE = mysite.settings module = django.core.handlers.wsgi:WSGIHandler() processes = 4 threads = 2 stats = 127.0 . 0.1 : 9191virtualenv = <path to env> |
按配置重启nginx:
1 | / usr / sbin / nginx - s reload |
或者:
1 2 | killall - 9 nginx nginx - c / etc / nginx / nginx.conf |
启动uWSGI:
1 | uwsgi myfile.ini |
重启uWSGI:
1 2 3 4 5 6 | # using kill to send the signal kill - HUP `cat / tmp / project - master.pid` # or the convenience option --reload uwsgi - - reload / tmp / project - master.pid # or if uwsgi was started with touch-reload=/tmp/somefile touch / tmp / somefile |
ini文件也可以用xml文件来设置。
添加xml支持:
1 | sudo apt - get install libxml2 - dev |
配置myfile.xml
1 2 3 4 5 | <uwsgi> <socket> 127.0 . 0.1 : 8080 < / socket> <chdir> / home / user / mysite / < / chdir> <module>mysite / wsgi< / module> < / uwsgi> |
启动:
1 | uwsgi - x myfile.xml |
nginx+fastcgi
fastcgi需要安装flup:
1 2 3 4 | wget http: / / www.saddi.com / software / flup / dist / flup - 1.0 . 2.tar .gz tar zxvf flup - 1.0 . 2.tar .gz cd flup - 1.0 . 2 python setup.py install |
或者:
1 | sudo apt - get install python - flup |
nginx配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | server { listen 80 ; server_name localhost; #设置该网站应用中所需要的静态文件的根目录 location ~ / static / { root / home / user / mysite / ; # root html; # index index.html index.htm; break ; } #设置媒体的根目录 location ~ / media / { root / home / user / mysite / ; # root html; # index index.html index.htm; break ; } #host and port to fastcgi server location / { fastcgi_pass 127.0 . 0.1 : 8080 ; fastcgi_param PATH_INFO $fastcgi_script_name; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param QUERY_STRING $query_string; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param pass_header Authorization; fastcgi_intercept_errors off; } #设置浏览器缓存这些图片格式文件浏览器缓存时间是30天,css/js缓存时间1小时 location ~ . * \.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d ; } location ~ . * \.(js|css)?$ { expires 1h ; } } |
重启nginx,然后启动fcgi:
1 | python manage.py runfcgi host = 127.0 . 0.1 port = 8080 method = prefork - - settings = mysite.settings #采用静态进程池的话性能会比动态线程生成高2倍左右 |
ok。
要更新django项目的话,
1 | ps - ef |grep fcgi |
找出主进程号kill掉,再重启fcgi即可。
也可以写个重启脚本。使用pidfile选项将fcgi的pid存储到文件中,重启时读取kill掉。
1 2 3 4 5 6 7 8 9 10 11 12 | #!/bin/bash PROJDIR = "/home/user/myproject" PIDFILE = "$PROJDIR/mysite.pid" cd $PROJDIR if [ - f $PIDFILE ]; then kill `cat - - $PIDFILE` rm - f - - $PIDFILE fi exec python manage.py runfcgi host = 127.0 . 0.1 port = 8080 method = prefork pidfile = $PIDFILE - - settings = mysite.settings |
分类:
Django
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探