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
pip
源pip3 config set global.index-url https://repo.huaweicloud.com/repository/pypi/simple
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
[root@ecs-f3bd ~]# mkdir django-test
[root@ecs-f3bd ~]# cd django-test
[root@ecs-8990 django-test]# django-admin startproject hello
[root@ecs-8990 django-test]# cd hello
[root@ecs-8990 hello]# cd hello
[root@ecs-8990 hello]# pwd
/root/django-test/hello/hello
[root@ecs-8990 hello]# vi settings.py
修改部分如下:
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1', 'localhost ', 'YOUR IP']
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
admin
模块[root@ecs-8990 hello]# cd -
/root/django-test/hello
[root@ecs-8990 hello]# python3 manage.py migrate
[root@ecs-8990 hello]# python3 manage.py createsuperuser
gunicorn.py
gunicorn.py
将Django
项目和nginx
连接在一起。[root@ecs-8990 hello]# ls
db.sqlite3 hello manage.py
[root@ecs-8990 hello]# mkdir gunicorn_log
[root@ecs-8990 hello]# ls
db.sqlite3 gunicorn_log gunicorn.py hello manage.py __pycache__
[root@ecs-f3bd hello]# vi gunicorn.py
gunicorn.py
内如如下:
# coding:utf-8
import multiprocessing
bind = '0.0.0.0:8001' #绑定ip和端口号
backlog = 512 #监听队列
chdir = '/root/django-test/hello' #gunicorn要切换到的目的工作目录, 项目的根目录
timeout = 30 #超时
worker_class = 'gevent' #使用gevent模式,还可以使用sync 模式,默认的是sync模式
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"' #设置gunicorn访问日志格式,错误日志无法设置
accesslog = "/root/django-test/hello/gunicorn_log/gunicorn_access.log" #访问日>志文件
errorlog = "/root/django-test/hello/gunicorn_log/gunicorn_error.log" #错误日>志文件
[root@ecs-8990 ~]# gunicorn hello.wsgi -c /root/django-test/hello/gunicorn.py
检查进程
[root@ecs-8990 ~]# ps aux| grep gunicorn
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
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
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