CentOS部署Django+Gunicorn+Supervisor+Nginx

作者 : 开心源码 本文共5459个字,预计阅读时间需要14分钟 发布时间: 2022-05-12 共143人阅读

服务器假如是新做的一个系统,先更新一下服务器的环境,yum和pip

yum update

pip install –upgrade pip

先安装Python3

centos安装python3

而后用pyvenv做隔离,由于我的服务器上跑的东西不多,先用这个对付着,服务器东西跑的多的,最好还是用Docker隔离比较彻底

到项目目录后创立虚拟环境venv并激活

cd /var/www/django_web/

python3 -m venv venv

cd venv/bin

source activate

而后在开发环境打包一下需要的依赖包

pip3 freeze > requirements.txt? 或者 python3 -m pip freeze > requirements.txt

把这个requirements.txt上传到服务器上,再按这个包安装环境

python3 -m pip install -r requirements.txt?

安装的时候,装到mysqlclient这个库的时候出错了,OSError: mysql_config not found

网上找了一下mysql_config要下面这些依赖,安装上,即可以了

yum install mysql-devel gcc gcc-devel python-devel

ftp上传项目文件,而后生成数据库,django官方推荐migrations放到版本控制中,所以migrations文件我一并上传到服务器,不需要再make

python3 manage.py migrate

创立超级管理员帐号

python3 manage.py createsuperuser

运行runserver试一下

python3 manage.py runserver 0.0.0.0:8000

假如访问不了,把防火墙8000端口打开一下再尝试

firewall-cmd –zone=public –add-port=8000/tcp

firewall-cmd –reload

没问题的话开始部署gunicorn

之前的开发包里我已经安装了gunicorn,假如没安装就在服务器的虚拟环境里pip安装一下

把gunicorn增加到django的settings.py里的INSTALLED_APPS里

接着可以直接运行

gunicorn django_web.wsgi:application -b 0.0.0.0:8000

假如没问题,可以接下去配置一个配置文件,方便以后设置

guni.conf.py

import multiprocessing

bind = ‘0.0.0.0:8000’ #监听端口

workers = 2 #负责解决请求的线程数,一般设置成:服务器CPU个数 + 1

#worker_class = “gevent” #使用的网络模型,默认sync,假如要性能好点就用gevent,我的项目暂时不用,注释掉

worker_connections = 200 #同时最大连接数

#daemon = True #能否后端运行,由于使用了supervisor,gunicorn不需要运行daemon模式

pidfile = ‘gunicorn.pid’ #pid文件

proc_name = ‘guni_web’ #进程名称

reload = True #当代码有升级的话自动重启workers,有点相似于调试模式

errorlog = “/var/www/django_web/log/guni_error.log” #错误日志#

accesslog = “/var/www/django_web/log/guni_access.log” #访问日志

#loglevel = ‘debug’

用 -c 参数运行gunicorn

gunicorn -c guni.conf.py django_web.wsgi:application? #django_web.wsgi为django_web目录下的wsgi.py文件(由django生成)

Supervisor安装

先退出虚拟环境再安装,否则这个是装到虚拟环境下的

deactivate

pip install supervisor

配置文件,先创立两个目录

mkdir /etc/supervisor

mkdir /etc/supervisor/conf.d

创立配置文件:/etc/supervisor/supervisord.conf

[unix_http_server]

file=/tmp/supervisor.sock? ; the path to the socket file

chmod=0700

[supervisord]

logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log

logfile_maxbytes=10MB? ? ? ? ; max main logfile bytes b4 rotation; default 50MB

logfile_backups=1? ? ? ? ? ? ; # of main logfile backups; 0 means none, default 10

loglevel=info? ? ? ? ? ? ? ? ; log level; default info; others: debug,warn,trace

pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid

nodaemon=false? ? ? ? ? ? ? ; start in foreground if true; default false

minfds=1024? ? ? ? ? ? ? ? ? ; min. avail startup file descriptors; default 1024

minprocs=200? ? ? ? ? ? ? ? ; min. avail process descriptors;default 200

[rpcinterface:supervisor]

supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]

serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL? for a unix socket

[include]

files = /etc/supervisor/conf.d/*.conf

创立子配置文件 /etc/supervisor/conf.d/webserver.conf

[program:webserver]

directory = /var/www/django_web

command = /var/www/django_web/venv/bin/gunicorn -c /var/www/django_web/guni.conf.py lodgenotice.wsgi:application

stdout_logfile = /var/www/django_web/log/supervisor.log

autostart = true

startsecs = 5

autorestart = true

startretries = 3

user = root

redirect_stderr = true

stdout_logfile_maxbytes = 10MB

stdout_logfile_backups = 1

用-c参数指定配置文件来运行supervisor

supervisord -c /etc/supervisor/supervisord.conf

启动后可以用supervisorctl来管理

# 中止某一个进程,program_name 为 [program:x] 里的 x

supervisorctl stop program_name

# 启动某个进程

supervisorctl start program_name

# 重启某个进程

supervisorctl restart program_name

# 结束所有属于名为 groupworker 这个分组的进程 (start,restart 同理)

supervisorctl stop groupworker:

# 结束 groupwor:name1 这个进程 (start,restart 同理)

supervisorctl stop groupworker:name1

# 中止一律进程,注:start、restart、stop 都不会载入最新的配置文件

supervisorctl stop all

# 载入最新的配置文件,中止原有进程并按新的配置启动、管理所有进程

supervisorctl reload

# 根据最新的配置文件,启动新配置或者有改动的进程,配置没有改动的进程不会受影响而重启

supervisorctl update

创立一个supervisor服务,这样服务器重启后会自动启动

创立文件/usr/lib/systemd/system/supervisord.service

# dservice for systemd (CentOS 7.0+)

# by ET-CS ( ET-CS)

[Unit]

Description=Supervisor daemon

[Service]

Type=forking

ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf

ExecStop=/usr/bin/supervisorctl shutdown

ExecReload=/usr/bin/supervisorctl reload

KillMode=process

Restart=on-failure

RestartSec=42s

[Install]

WantedBy=multi-user.target

开启服务

systemctl enable supervisord

检查一下看看能否设置成功

systemctl is-enabled supervisord

Gunicorn无法解决静态资源,所以还需要Nginx

先把Django项目的静态文件收集到一个地方

修改settings.py ,这个路径就是我们要收集所有静态文件的路径,到时候加到nginx

STATIC_ROOT=‘/var/www/django_web/static/’

在虚拟环境下运行

python manage.py collectstatic

安装Nginx

yum install nginx -y

配置文件/etc/nginx/nginx.conf,修改server字段的内容如下(不要把下面的直接做一个文件,要在原文件上修改)

server{

? ? listen 80;监听的端口

? ? server_name 127.0.0.1;

? ? server_name your_www;

? ? #当请求这些server name的时候,nginx才会做反向代理商,0.0.0.0是指一律? ?

????location / {

? ? ? proxy_intercept_errors on;

? ? ? proxy_pass http://127.0.0.1:8080;? ? ? 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_set_header X-Forwarded-Proto $scheme;

? ? }

? ? # location 顾名思义,定位,就是当访问 / 的时候,nginx会将请求转给本地的8080端口,然后面的设置都是少量基本的配置,可以直接用? ?

location /static {

? ? ? alias /var/www/django_web/static;

? ? }

? ? # 这个就是配置静态文件的地方,要用绝对地址,你在开发的时候建议每个app的静态文件都用多了一层app_name的文件夹来包住。

}

配置好后,先检查一下对不对,假如没有问题即可以运行了

nginx -t

运行nginx的命令很简单,直接命令行输入nginx就可

nginx -s reload|reopen|stop|quit #重新加载配置|重启|中止|退出 nginx

假如有防火墙,打开服务器80端口,加上–permanent就是永久有效,不加重启后会失效

firewall-cmd –zone=public –add-port=80/tcp –permanent

firewall-cmd –reload

阿里云还要开一个安全组的80端口

这样基本就部署完了,用浏览器打开即可以访问到你的网页了。

假如怕nginx会运行中崩溃,或者者是想重启后自动开启,直接加个文件到supervisor里面即可以自动监控了

创立子配置文件 /etc/supervisor/conf.d/nginx.conf

[program:nginx]

command = /usr/sbin/nginx? -g ‘daemon off;’

autostart=true

startsecs=5

autorestart=true

user = root

stdout_logfile=/var/www/django_web/log/nginx_stdout.log

stdout_logfile_maxbytes=10MB

stderr_logfile=/var/www/django_web/log/nginx_stderr.log

stderr_logfile_maxbytes=10MB

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » CentOS部署Django+Gunicorn+Supervisor+Nginx

发表回复