64. Django 2 生产环境部署 uwsgi nginx

网友投稿 989 2022-11-21

64. Django 2 生产环境部署 uwsgi nginx

64. Django 2 生产环境部署 uwsgi  nginx

前言

Django在开发过程中可以使用​​python3 manage.py runserver​​启动web服务,但是这个服务只是用来开发调试使用的。正常的部署情况需要使用 uwsgi + nginx 进行服务部署。

配置项目的settings

修改Debug状态为 False允许接收所有hosts的方法

DEBUG = FalseALLOWED_HOSTS = ['*']

接下来配置uwsgi

部署 uwsgi

安装 uwsgi

pip3 install uwsgi

执行如下:

[root@server01 ~]# pip3 install uwsgiLooking in indexes: uwsgi Downloading (801kB) |████████████████████████████████| 808kB 5.4MB/s Installing collected packages: uwsgi Running setup.py install for uwsgi ... doneSuccessfully installed uwsgi-2.0.18WARNING: You are using pip version 19.2.1, however version 19.2.3 is available.You should consider upgrading via the 'pip install --upgrade pip' command.[root@server01 ~]#

-完毕后,还需要配置一下环境变量。

配置uwsgi执行文件的软链接至/usr/bin

搜索二进制可执行文件的所在路径

# 搜索文件所在路径[root@server01 performance]# find / -name "*uwsgi*" -ls | grep python3 | grep bin405620 14852 -rwxr-xr-x 1 root root 15205552 Oct 14 01:15 /usr/local/python3/bin/uwsgi# 查看可执行文件[root@server01 performance]# ls -ll /usr/local/python3/bin/uwsgi-rwxr-xr-x 1 root root 15205552 Oct 14 01:15 /usr/local/python3/bin/uwsgi[root@server01 performance]#

设置软链接

ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi

执行命令测试

[root@server01 performance]# uwsgi *** Starting uWSGI 2.0.18 (64bit) on [Mon Oct 14 17:44:34 2019] ***compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-36) on 13 October 2019 17:14:55os: Linux-3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018nodename: server01machine: x86_64clock source: unixpcre jit disableddetected number of CPU cores: 1current working directory: /work/performancedetected binary path: /usr/local/python3/bin/uwsgiuWSGI running as root, you can use --uid/--gid/--chroot options*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** WARNING: you are running uWSGI without its master process manager ***your processes number limit is 7914your memory page size is 4096 bytesdetected max file descriptor number: 65535lock engine: pthread robust mutexesthunder lock: disabled (you can enable it with --thunder-lock)uWSGI running as root, you can use --uid/--gid/--chroot options*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** uWSGI running as root, you can use --uid/--gid/--chroot options*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** The -s/--socket option is missing and stdin is not a socket.[root@server01 performance]#

测试uwsgi启动django服务

命令说明:

cd /your/django/path # 进入项目目录uwsgi --ip:port --file project/wsgi.py --static-map=/static=static

参数说明:

​​--这个就和runserver一样指定IP 端口​​--file​​​ 指定项目的​​wsgi.py​​​​--static​​ 指定静态文件

执行如下:

[root@server01 performance]# uwsgi --127.0.0.1:8001 --file performance/wsgi.py --static-map=/static=static[uwsgi-static] added mapping for /static => static*** Starting uWSGI 2.0.18 (64bit) on [Mon Oct 14 17:50:59 2019] ***compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-36) on 13 October 2019 17:14:55os: Linux-3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018...

使用测试访问 :

可以看到服务正常访问,但是这只是测试的启动方式。下面来看看怎么使用 uwsgi 的配置文件方式来启动。

使用uwsgi配置文件启动django项目

在django项目下,创建​​uwsgi.ini​​配置文件:

[root@server01 performance]# > uwsgi.ini

编辑​​vim uwsgi.ini​​文件

# uwsig使用配置文件启动[uwsgi]# 项目目录chdir=/work/performance# 指定项目的applicationmodule=performance.wsgi:application# 指定sock的文件路径 socket=/work/performance/uwsgi.sock# 进程个数 workers=5pidfile=/work/performance/uwsgi.pid# 指定IP端口 指定静态文件static-map=/static=/work/performance/static# 启动uwsgi的用户名和用户组uid=rootgid=root# 启用主进程master=true# 自动移除unix Socket和pid文件当服务停止的时候vacuum=true# 序列化接受的内容,如果可能的话thunder-lock=true# 启用线程enable-threads=true# 设置自中断时间harakiri=30# 设置缓冲post-buffering=4096# 设置日志目录daemonize=/work/performance/logs/uwsgi.log

启动项目​​uwsgi --ini uwsgi.ini​​

[root@server01 performance]# uwsgi --ini uwsgi.ini[uWSGI] getting INI configuration from uwsgi.ini[uwsgi-static] added mapping for /static => /work/performance/static[root@server01 performance]#

uwsgi 停止命令

[root@server01 performance]# uwsgi --stop uwsgi.pid [root@server01 performance]# [root@server01 performance]# ps -ef | grep uwsgiroot 1073 31902 0 19:58 pts/8 00:00:00 grep --color=auto uwsgi[root@server01 performance]#

uwsgi 重载配置命令

[root@locust01 performance]# uwsgi --reload uwsgi.pid

测试访问页面

成功访问服务了。

查看uwsgi日志

[root@server01 performance]# cd logs/[root@server01 logs]# lsceleryd.log uwsgi.log[root@server01 logs]# tail -f uwsgi.log WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x24b5a90 pid: 29508 (default app)*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** uWSGI is running in multiple interpreter mode ***spawned uWSGI master process (pid: 29508)spawned uWSGI worker 1 (pid: 29512, cores: 1)spawned uWSGI worker 2 (pid: 29513, cores: 1)spawned uWSGI worker 3 (pid: 29514, cores: 1)spawned uWSGI worker 4 (pid: 29515, cores: 1)spawned uWSGI worker 5 (pid: 29516, cores: 1)spawned uWSGI 1 (pid: 29517)[pid: 29513|app: 0|req: 1/1] 127.0.0.1 () {40 vars in 818 bytes} [Mon Oct 14 19:11:26 2019] GET / => generated 8398 bytes in 519 msecs (HTTP/1.1 200) 6 headers in 437 bytes (1 switches on core 0)next_url = machine_unit:machine_unit_list[pid: 29512|app: 0|req: 1/2] 127.0.0.1 () {48 vars in 963 bytes} [Mon Oct 14 19:11:28 2019] POST / => generated 0 bytes in 671 msecs (HTTP/1.1 302)

到了这里已经部署好了 uwsgi + django 的服务了,但是uwsgi处理动态请求能力高,处理静态文件的请求就比较差了,下一步使用nginx结合使用,静态文件由nginx进行处理。

配置nginx

编辑​​vim nginx.conf​​文件,增加如下配置:

server { listen 80; server_name localhost; .... # 配置动态请求使用uwsgi location / { include uwsgi_params; # 导入nginx与uwsgi通讯的模块 uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间 uwsgi_pass unix:/work/performance/uwsgi.sock; # 指定uwsgi的sock文件: 所有动态请求直接转发 } # 配置静态文件路径 location /static/ { alias /work/performance/static/; } ... }

重加载nginx服务

# 检查nginx配置是否正确[root@server01 conf]# nginx -tnginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful# 重载nginx配置[root@server01 conf]# nginx -s reload

测试访问

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Lambda函数式编程思想
下一篇:Maven相关的概念 - Maven仓库和坐标
相关文章

 发表评论

暂时没有评论,来抢沙发吧~