Django+Gunicorn+Gevent+Supervisor+Nginx 是相对比较成熟的部署方案。
基础环境
华为云ECS OS:CentOS7.6 Python:Python3.6.8
配置pip
- 升级
pip ,否则不能使用pip config 命令。升级后pip 版本为21.3 。
pip3 install -U pip -i https://repo.huaweicloud.com/repository/pypi/simple
pip3 config set global.index-url https://repo.huaweicloud.com/repository/pypi/simple
安装Python第三方包
- 安装Python3开发包,否则出现
gcc 命令异常。
yum install -y python3-devel
- 安装
gunicorn gevent`` flask
pip3 install gunicorn gevent django
最终安装版本为:
Django==3.2.9
gevent==21.8.0
gunicorn==20.1.0
升级sqlite3
测试环境Django 默认使用sqlite3 。由于CentOS7.6默认安装sqlite3 版本为3.7.17 ,与较新的Django 不兼容,因此需要升级。
wget --no-check-certificate https://www.sqlite.org/2021/sqlite-autoconf-3360000.tar.gz
tar zxvf sqlite-autoconf-3360000.tar.gz
cd sqlite-autoconf-3360000/
./configure --prefix=/usr/local
make && make install
mv /usr/bin/sqlite3 /usr/bin/sqlite3_old
ln -s /usr/local/bin/sqlite3 /usr/bin/sqlite3
echo "/usr/local/lib" > /etc/ld.so.conf.d/sqlite3.conf
ldconfig
sqlite3 -version
测试Django
[root@ecs-f3bd ~]
[root@ecs-f3bd ~]
[root@ecs-8990 django-test]
[root@ecs-8990 django-test]
[root@ecs-8990 hello]
[root@ecs-8990 hello]
/root/django-test/hello/hello
[root@ecs-8990 hello]
修改部分如下:
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1', 'localhost ', 'YOUR IP']
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
[root@ecs-8990 hello]
/root/django-test/hello
[root@ecs-8990 hello]
[root@ecs-8990 hello]
- 创建
gunicorn.py 通过gunicorn.py 将Django 项目和nginx 连接在一起。
[root@ecs-8990 hello]
db.sqlite3 hello manage.py
[root@ecs-8990 hello]
[root@ecs-8990 hello]
db.sqlite3 gunicorn_log gunicorn.py hello manage.py __pycache__
[root@ecs-f3bd hello]
gunicorn.py 内如如下:
import multiprocessing
bind = '0.0.0.0:8001'
backlog = 512
chdir = '/root/django-test/hello'
timeout = 30
worker_class = 'gevent'
workers = multiprocessing.cpu_count() * 2 + 1
threads = 2
loglevel = 'info'
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'
accesslog = "/root/django-test/hello/gunicorn_log/gunicorn_access.log"
errorlog = "/root/django-test/hello/gunicorn_log/gunicorn_error.log"
测试运行gunicorn
[root@ecs-8990 ~]
检查进程
[root@ecs-8990 ~]
root 10857 2.1 0.5 253288 23004 pts/0 S+ 21:10 0:00 /usr/bin/python3 /usr/local/bin/gunicorn hello.wsgi -c /root/django-test/hello/gunicorn.py
root 10860 4.7 1.0 300508 38848 pts/0 S+ 21:10 0:00 /usr/bin/python3 /usr/local/bin/gunicorn hello.wsgi -c /root/django-test/hello/gunicorn.py
root 10861 5.3 1.0 300516 38852 pts/0 S+ 21:10 0:00 /usr/bin/python3 /usr/local/bin/gunicorn hello.wsgi -c /root/django-test/hello/gunicorn.py
root 10862 5.3 1.0 300516 38856 pts/0 S+ 21:10 0:00 /usr/bin/python3 /usr/local/bin/gunicorn hello.wsgi -c /root/django-test/hello/gunicorn.py
root 10863 5.3 1.0 300520 38892 pts/0 S+ 21:10 0:00 /usr/bin/python3 /usr/local/bin/gunicorn hello.wsgi -c /root/django-test/hello/gunicorn.py
root 10865 5.0 1.0 300520 38896 pts/0 S+ 21:10 0:00 /usr/bin/python3 /usr/local/bin/gunicorn hello.wsgi -c /root/django-test/hello/gunicorn.py
root 10873 0.0 0.0 112812 976 pts/1 S+ 21:10 0:00 grep --color=auto gunicorn
配置supervisor
yum install -y nginx supervisor
安装版本为nginx:1.20.1 ,supervisor:3.4.0
vi /etc/supervisord.d/hello.ini
内容如下:
command=gunicorn hello.wsgi -c /root/django-test/hello/gunicorn.py
directory=/root/django-test/hello
autostart=true
autorestart=unexpected
user=root
systemctl restart supervisord && supervisorctl reload
配置Nginx
vi /etc/nginx/conf.d/hello.conf
内容如下:
server {
listen 80;
server_name xxx;
charset utf-8;
location / {
proxy_pass http://0.0.0.0:8001;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /static {
alias /root/django-test/hello/static/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
systemctl restart nginx.service
|