64. Django 2 生产环境部署 uwsgi nginx
前言
Django在开发过程中可以使用python3 manage.py runserver
启动web服务,但是这个服务只是用来开发调试使用的。正常的部署情况需要使用 uwsgi + nginx 进行服务部署。
配置项目的settings
-
修改Debug状态为 False -
允许接收所有hosts的方法
DEBUG = False
ALLOWED_HOSTS = ['*']
接下来配置uwsgi
部署 uwsgi
安装 uwsgi
pip3 install uwsgi
执行如下:
[root@server01 ~]# pip3 install uwsgi
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting uwsgi
Downloading https://mirrors.aliyun.com/pypi/packages/e7/1e/3dcca007f974fe4eb369bf1b8629d5e342bb3055e2001b2e5340aaefae7a/uwsgi-2.0.18.tar.gz (801kB)
|████████████████████████████████| 808kB 5.4MB/s
Installing collected packages: uwsgi
Running setup.py install for uwsgi ... done
Successfully installed uwsgi-2.0.18
WARNING: 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 bin
405620 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:55
os: Linux-3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018
nodename: server01
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /work/performance
detected binary path: /usr/local/python3/bin/uwsgi
uWSGI 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 7914
your memory page size is 4096 bytes
detected max file descriptor number: 65535
lock engine: pthread robust mutexes
thunder 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 --http ip:port --file project/wsgi.py --static-map=/static=static
参数说明:
-
--http
这个就和runserver一样指定IP 端口 -
--file
指定项目的wsgi.py
-
--static
指定静态文件
执行如下:
[root@server01 performance]# uwsgi --http 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:55
os: Linux-3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018
...
使用测试访问 http://127.0.0.1:8001/ :
可以看到服务正常访问,但是这只是测试的启动方式。下面来看看怎么使用 uwsgi 的配置文件方式来启动。
使用uwsgi配置文件启动django项目
-
在django项目下,创建 uwsgi.ini
配置文件:
[root@server01 performance]# > uwsgi.ini
-
编辑 vim uwsgi.ini
文件
# uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/work/performance
# 指定项目的application
module=performance.wsgi:application
# 指定sock的文件路径
socket=/work/performance/uwsgi.sock
# 进程个数
workers=5
pidfile=/work/performance/uwsgi.pid
# 指定IP端口
http=127.0.0.1:8001
# 指定静态文件
static-map=/static=/work/performance/static
# 启动uwsgi的用户名和用户组
uid=root
gid=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 uwsgi
root 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]# ls
celeryd.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 http 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 -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
# 重载nginx配置
[root@server01 conf]# nginx -s reload
-
测试访问