Nginx+uwsgi部署 Diango(生产环境)
环境:CentOS6.5 + Nginx1.11.5 + Python3.5.2
1. 安装基础软件包#
1 2 3 | yum install - y zlib - devel bzip2 - devel \ pcre - devel openssl - devel ncurses - devel sqlite - devel \ readline - devel tk - devel |
2. 安装Python3.5.2版本#
1 2 3 4 5 6 | wget https: / / www.python.org / ftp / python / 3.5 . 2 / Python - 3.5 . 2.tar .xz tar xf Python - 3.5 . 2.tar .xz cd Python - 3.5 . 2 . / configure make - j 2 make altinstall |
PS:Python3.x默认已经安装pip等包管理工具,如果没有需要手动安装`pip`包管理工具
3. uWSGI的安装与使用
#
什么是uwsgi,what?
uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。
要注意 WSGI / uwsgi / uWSGI 这三个概念的区分。
- WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架写的程序)通信的一种规范。
- uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。
- 而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。
- uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。
uWSGI的主要特点如下
- 超快的性能
- 低内存占用(实测为apache2的mod_wsgi的一半左右)
- 多app管理(终于不用冥思苦想下个app用哪个端口比较好了-.-)
- 详尽的日志功能(可以用来分析app性能和瓶颈)
- 高度可定制(内存大小限制,服务一定次数后重启等)
安装uWSGI
1 2 3 4 | # Install the latest stable release: pip install uwsgi # ... or if you want to install the latest LTS (long term support) release, pip install https: / / projects.unbit.it / downloads / uwsgi - lts.tar.gz |
基本测试
创建测试文件
1 2 3 4 5 | # test.py def application(env, start_response): start_response( '200 OK' , [( 'Content-Type' , 'text/html' )]) return [b "Hello World" ] # python3 #return ["Hello World"] # python2 |
运行
1 | uwsgi - - http : 8000 - - wsgi - file test.py |
使用浏览器访问`http://ip:8000`验证
使用uWSGI运行Django
1 | uwsgi - - http : 8000 - - module mysite.wsgi |
使用浏览器访问`http://ip:8000`验证
可以将参数写到一个配置文件中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [root@localhost Django_test] # vim mysite_uwsgi.ini [uwsgi] http = : 9000 #the local unix socket file than commnuincate to Nginx socket = 127.0 . 0.1 : 8001 # the base directory (full path) chdir = / usr / local / Django_test # Django's wsgi file wsgi - file = Django_test / wsgi.py # maximum number of worker processes processes = 4 #thread numbers startched in each worker process threads = 2 #monitor uwsgi status stats = 127.0 . 0.1 : 9191 # clear environment on exit or restart vacuum = true |
启动
1 | uwsgi - - ini mysite_uwsgi.ini |
使用浏览器访问`http://ip:9000`验证
3. 安装并配置Nginx#
1 2 3 4 5 6 | useradd - M - s / sbin / nologin nginx tar zxf nginx - 1.11 . 5.tar .gz cd nginx - 1.11 . 5 . / configure - - prefix = / usr / local / nginx - - user = nginx - - group = nginx - - with - http_stub_status_module - - with - http_gzip_static_module make && make install ln - s / usr / local / nginx / sbin / nginx / usr / local / sbin / |
修改nginx配置文件
1 2 3 4 | #修改主配置文件,引入下面要写的uwsgi的配置文件,也可以在当前配置文件中写 # vim /usr/local/nginx/conf/nginx.conf #在http区段中 include mysite_uwsgi_nginx.conf; |
创建一个uwsgi的配置文件
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 | # vim /usr/local/nginx/conf/mysite_uwsgi_nginx.conf # the upstream component nginx needs to connect to upstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0 . 0.1 : 8001 ; # for a web port socket (we'll use this first) } # configuration of the server server { # the port your site will be served on listen 8000 ; # the domain name it will serve for server_name .example.com; # substitute your machine's IP address or FQDN charset utf - 8 ; # max upload size client_max_body_size 75M ; # adjust to taste location / static { alias / usr / local / Django_test / static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include uwsgi_params; # the uwsgi_params file you installed } } |
到此Django项目部署已经基本部署完成,你可以访问我们写的页面,但是访问`http://ip:8000/admin`却发现没有样式,waht?
这个问题是由于admin的样式文件都在django内部,而不是在我们的项目的静态文件的目录中,到此我们需要把所有的静态文件汇聚到一个目录中
1. 修改项目目录中的`setting.py`文件,添加 (all_statics可以自己指定目录)
1 | STATIC_ROOT = os.path.join(BASE_DIR, "all_statics/" ) |
2.run (提示输入yes)
1 | python manage.py collectstatic |
3.现在我们需要去修改nginx的配置文件,将静态文件的目录修改为汇聚后的静态文件的目录
1 2 3 | location / static { alias / usr / local / Django_test / all_statics; } |
现在去启动nginx和uwsgi
1 2 | [root@localhost Django_test] # # nginx [root@localhost Django_test] # uwsgi mysite_uwsgi.ini & |
现在admin页面总算可以看了
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· 趁着过年的时候手搓了一个低代码框架
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现