普拉多VX

人生一路,不问来时,不知归期

0%

Ubuntu 18.4 使用 gunicorn 部署Django项目

部署环境

  • Ubuntu 18.04.5 LTS
  • Python 3.6.9
  • Django .2.14
  • nginx 1.14.0
  • gunicorn 20.0.4

1.创建虚拟环境

在root目录下创建虚拟环境,在线上部署建议专门在/下建立一个虚拟环境的目录。

1
2
3
4
5
6
7
8
9
10
11
12
13
root@Thortest:~# pip3 install virtualenv

root@Thortest:~# virtualenv tweb_env


root@Thortest:~# source tweb_env/bin/activate # 进入虚拟环境 退出可以使用deactivate 指令
(tweb_env) root@Thortest:~# python
Python 3.6.9 (default, Jul 17 2020, 12:50:27)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
(tweb_env) root@Thortest:~# cd /data/Twebpool # 切换到项目路径
(tweb_env) root@Thortest:/data/Twebpool# pip install -r requirements.txt 在当前虚拟环境中安装依赖文件

2.编写gunicorn启动脚本

在项目文件夹下创建gunicorn.sh,此脚本位置与manage.py同一目录

1
2
3
4
5
6
7
├── db.sqlite3
├── gunicorn.sh
├── logs
│   ├── gunicorn.info.log
│   └── web.log
├── manage.py
├── pool

gunicorn.sh

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
set -e
TIMEOUT=300 #to solve upload app package timeout issue

# Twebpool为项目名,gunicorn路径必须是虚拟环境的绝对路径
exec /root/tweb_env/bin/gunicorn Twebpool.wsgi:application -w 6 \
-b 0.0.0.0:8000 \
--max-requests 10000 \
--timeout=300 \
--access-logfile=server.access.log \
--error-logfile=server.error.log

设置可执行权限

1
root@Thortest:/data/Twebpool# chmod +x gunicorn.sh

3.创建supervisor配置

安装supervisor并创建配置文件

1
2
3
4
5
6
7
8
9
10
11
root@Thortest:/data/Twebpool# apt-get install supervisor
root@Thortest:/data/Twebpool# vim /etc/supervisor/conf.d/web.conf
[program:web]
directory=/data/Twebpool/
command=/bin/sh gunicorn.sh
stopsignal=QUIT
autostart=true
autorestart=true
stdout_logfile=/data/Twebpool/logs/gunicorn.info.log
stderr_logfile=/data/Twebpool/logs/gunicorn.error.log
redirect_stderr=true

启动项目

1
2
3
4
5
6
root@Thortest:/data/Twebpool# supervisorctl 
supervisor> reread
supervisor> update # 默认会直接启动,如果需要停止使用stop|start|restart
supervisor> status
web RUNNING pid 305, uptime 0:0:47
supervisor>

4.配置nginx

x.x.x.x 为服务器域名或者外网ip

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
(tweb_env) root@Thortest:~# vim /etc/nginx/conf.d/api.conf 
server {
listen 80;
server_name x.x.x.x;
keepalive_timeout 300;


location / {
client_max_body_size 100m;
proxy_http_version 1.1;
proxy_pass http://127.0.0.1:8000;
proxy_connect_timeout 300s;
proxy_read_timeout 300s;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_send_timeout 15000;


proxy_buffer_size 1024k;
proxy_buffers 4 1024k;
proxy_busy_buffers_size 1024k;
proxy_temp_file_write_size 1024k;
}


}

测试nginx配置

ps:nginx -t 测试。习惯很重要,修改配置后一定要使用-t检查下有没有问题

1
2
3
4
(tweb_env) root@Thortest:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
(tweb_env) root@Thortest:~#

重启或者重新加载

1
(tweb_env) root@Thortest:~# systemctl  restart nginx.service # reload 不重启服务应用新配置| restart 重启

5.访问测试