uWSGI + Nginx + Django 部署

时间:2023-03-09 21:49:34
uWSGI + Nginx + Django 部署

1. uWSGI 服务器

Django 默认使用 WSGI(Python Web Server Gateway )

作为 Web 服务器,一般仅用来作为测试使用,实际生产环境而是使用 uWSGI 和 Nginx 作为服务器。

uWSGI 代码完全用C编写,效率高、性能稳定,但是处理 静态文件能力较弱,因此实际生产环境中,一般采用 uWSGI + Nginx 两者搭配使用:

  • uWSGI:处理动态请求(css、js、图片文件等)
  • Nginx:处理静态文件请求(提交表单、mysql、动态渲染 html)

安装:

 pip3 install uWSGI

settings.py 配置

设置 ALLOWED_HOSTS

ALLOWED_HOSTS = [
# 加上本机的IP地址
'192.168.xx.xxx',
'127.0.0.1',
'localhost'
]

也可以这样设置:

ALLOWED_HOSTS = ['*']

1.1 通过参数启动 uWSGI

# 请更换成你服务器 ip 和端口,其中 Celery_Test/wsgi.py 为Django 项目中自带的 wsgi web 服务
hj@hj:~/桌面/Celery_Test$ uwsgi --http 192.168.21.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static

出现如下效果便是运行成功了:

uWSGI + Nginx + Django 部署

查看启动端口:

hj@hj:~/桌面/Celery_Test/script$ ps -ef|grep uwsgi

hj       17176  5231  0 15:37 pts/1    00:00:00 uwsgi --http 192.168.xx.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static
hj 17177 17176 0 15:37 pts/1 00:00:00 uwsgi --http 192.168.xx.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static
hj 17206 6554 0 15:38 pts/2 00:00:00 grep --color=auto uwsgi

访问 192.168.21.128:8080

uWSGI + Nginx + Django 部署

1.2 通过配置文件启动 uWSGI

在项目根目录创建一个名为 uwsgi.ini 的配置文件,配置如下:

[uwsgi]
# 项目目录
chdir=/home/hj/桌面/Celery_Test/
# 指定项目的application
module=Celery_Test.wsgi:application
# 指定sock的文件路径
socket=/home/hj/桌面/Celery_Test/script/uwsgi.sock
# 进程个数
workers=5
pidfile=/home/hj/桌面/Celery_Test/script/uwsgi.pid
# 指定IP端口
http=192.168.21.128:8080
# 指定静态文件
static-map=/static=/home/hj/桌面/Celery_Test/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=/home/hj/桌面/Celery_Test/script/uwsgi.log

配置好之后,运行 uwsgi --ini uwsgi.ini 启动 uwsgi,出现如下信息即表示启动成功:

hj@hj:~/桌面/Celery_Test$ uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
[uwsgi-static] added mapping for /static => /home/hj/桌面/Celery_Test/static

查看运行情况:

ps ajx|grep uwsgi

效果如下图:

uWSGI + Nginx + Django 部署

常用命令:

uwsgi --ini uwsgi.ini       # 启动
# 启动时会生成两个文件,分别为:
# PID文件 标识这个程序所处的状态
# SOCK文件 用来和其他程序通信的
uwsgi --stop uwsgi.pid # 停止
uwsgi --reload uwsgi.ini # 重置

Tips

停止时出现 signal_pidfile()/kill(): No such process [core/uwsgi.c line 1693]

原因:当前端口进程与 uwsgi.pid 不一致,查看当前端口实际进程 ID,并修改 uwsgi.pid

# 根据端口,查看进程
hj@hj:~/桌面/Celery_Test$ sudo netstat -nap | grep 8080
tcp 0 0 192.168.21.128:8080 0.0.0.0:* LISTEN 6943/uwsgi # 修改 uwsgi.pid 的值为 6943,并再重新停止
hj@hj:~/桌面/Celery_Test$ uwsgi --stop script/uwsgi.pid # 查看发现已经成功停止
hj@hj:~/桌面/Celery_Test$ ps ajx|grep uwsgi
5231 14550 14549 5231 pts/1 14549 S+ 1000 0:00 grep --color=auto uwsgi

Linux 中怎么查看端口和进程号

# 加上 sudo
# 根据进程 pid 查看端口
lsof -i | grep pid # 根据端口查看进程
lsof -i:port # 根据进程 pid 查看端口
netstat -nap | grep pid # 根据端口查看进程号
netstat -nap | grep port

2. Nginx 服务器

我们知道 uWSGI 处理静态文件请求能力比较弱,因此一般实际生产环境中以 动静分离 的方式处理动静请求,即 uWSGI + Nginx。

Nginx 作用还包括负载均衡、反向代理等。

2.1 Ubuntu 上安装 Nginx

Nginx 的软件包在 Ubuntu 默认软件仓库中可用。 安装非常简单,只需键入以下命令:

sudo apt update
udo apt install nginx

查看服务器版本信息:

sudo nginx -v

nginx version: nginx/1.14.0 (Ubuntu)

查看服务器状态:

# 两个都可以
sudo systemctl status nginx
ps -ef | grep nignx

uWSGI + Nginx + Django 部署

配置防火墙

打开 80 和 443 端口允许通过防火墙:

hj@hj:~$ sudo ufw allow 'Nginx Full'
防火墙规则已更新
规则已更新(v6)

检查是否更改:

hj@hj:~$ sudo ufw status
状态: 激活 至 动作 来自
- -- --
22 ALLOW Anywhere
4200 ALLOW Anywhere
Nginx Full ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
4200 (v6) ALLOW Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)

测试安装

访问:http://192.168.21.128/

uWSGI + Nginx + Django 部署

使用 systemctl 管理 Nginx 服务

您可以像任何其他 systemd 单位一样管理 Nginx 服务:

# 停止Nginx服务
sudo systemctl stop nginx # 再次启动
sudo systemctl start nginx # 重新启动Nginx服务:
sudo systemctl restart nginx # 在进行一些配置更改后重新加载 Nginx 服务:
$sudo systemctl reload nginx # 如果你想禁用Nginx服务在启动时启动:
$sudo systemctl disable nginx # 并重新启用它:
$sudo systemctl enable nginx

参考链接:如何在Ubuntu 18.04上安装Nginx

2.2 CentOS 上安装

以CentOS6.x 系统为例

  1. 更换源:
# 备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup # 更换成国内源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo # 生成缓存
yum makecache
  1. 安装 Nginx:
yum -y install nginx

2.3 与 uWSGI 结合使用部署 Django

为 Nginx 添加配置文件,Ngnix 默认配置文件加载是在 /etc/nginx/conf.d/ 目录下,新建一个配置文件(名字随意),编辑如下:

# 新建到配置文件 conf.d
vim /etc/nginx/conf.d/ # 编辑配置文件
server { # 开始配置了
listen 80; # 监听端口
server_name 10.129.xxx.183 ; # 你访问的路径前面的 url名称
access_log /var/log/nginx/access.log main; # Nginx日志配置
charset utf-8; # Nginx编码
gzip on; # 启用压缩,这个的作用就是给用户一个网页,比如3M压缩后1M这样传输速度就会提高很多
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; # 支持压缩的类型 error_page 404 /404.html; # 错误页面
error_page 500 502 503 504 /50x.html; # 错误页面 # 指定项目路径 uwsgi
location / { # 类似于 Django的 url(r'^admin/', admin.site.urls),
include uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的
uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
uwsgi_pass unix:/opt/project_teacher/script/uwsgi.sock; # 指定uwsgi的sock文件所有动态请求就会直接丢给他
} # 指定静态文件路径
location /static/ {
alias /opt/project_teacher/teacher/static/;
index index.html index.htm;
} }

参考:

server {
listen 80;
server_name 192.168.xx.128 ;
access_log /var/log/nginx/access.log main;
charset utf-8;
gzip on;
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html; location / {
include uwsgi_params;
uwsgi_connect_timeout 30;
uwsgi_pass unix:/home/hj/桌面/Celery_Test/script/uwsgi.sock;
} location /static/ {
alias /home/hj/桌面/Celery_Test/static/;
index index.html index.htm

启动 Nginx 服务 /etc/init.d/nginx start,访问:http://192.168.21.128:8080/app/index/,效果如下图:

uWSGI + Nginx + Django 部署

常用命令:

/etc/init.d/nginx start     # 启动
/etc/init.d/nginx stop # 关闭 # Nginx配置是重启才生效,若修改配置不知道是否对不对,可以用来测试
/etc/init.d/nginx configtest # 生产环境直接 reload就行了,不要 stop start 或者 restart
/etc/init.d/nginx reload

配置 Django 静态文件

admin 所需的静态文件都在 Django 的安装内,我们没有配置指向 Django 的配置文件。

解决办法:

  1. 设置 settings.py ,将所有静态文件收集到 static_all 中:
# settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "static_all") # 收集成功提示(仅供参考)
120 static files copied to '/home/hj/桌面/Celery_Test/static_all'.
  1. 收集静态文件到 static_all 这个目录中(可不创建 static_all):
python3 manage.py collectstatic --noinput
  1. 修改 /etc/nginx/conf.d/test.ini 中静态文件路径:
alias  /home/hj/桌面/Celery_Test/static_all;

指定其他地方放置静态文件

# 新建目录
sudo mkdir -vp /var/www/test2/static/ # 赋予权限
sudo chmod 777 /var/www/test2/static/ # 修改项目中 settings.py,指定静态文件路径
STATIC_ROOT = '/var/www/test2/static/'
STATIC_URL = '/static/' # 收集静态文件到 /var/www/test2/static/ 中
python3 manage.py collectstatic # 输入 yes,开始收集,重新加载 Nginx 服务