部署环境
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:~ root@Thortest:~ root@Thortest:~ (tweb_env) root@Thortest:~ 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:~ (tweb_env) root@Thortest:/data/Twebpool
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 -eTIMEOUT=300 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
3.创建supervisor配置 安装supervisor并创建配置文件
1 2 3 4 5 6 7 8 9 10 11 root@Thortest:/data/Twebpool root@Thortest:/data/Twebpool [program:web] directory=/data/Twebpool/ command =/bin/sh gunicorn.shstopsignal=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 supervisor> reread supervisor> update 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:~ 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: 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:~
5.访问测试