当前位置:首页 » 《随便一记》 » 正文

华为云部署Centos7.6 Django+Gunicorn+Gevent+Supervisor+Nginx_mighty13的专栏

0 人参与  2022年04月06日 16:49  分类 : 《随便一记》  评论

点击全文阅读


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

安装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
  • 替换系统低版本 sqlite3
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 ~]# 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.pyDjango项目和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"        #错误日>志文件

测试运行gunicorn

[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

配置supervisor

  • 安装nginx和supervisor
 yum install -y nginx supervisor

安装版本为nginx:1.20.1supervisor:3.4.0

  • 添加supervisor子配置
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

点击全文阅读


本文链接:http://zhangshiyu.com/post/37477.html

安装  配置  版本  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1