CentOS部署Django+Gunicorn+Supervisor+Nginx

服務(wù)器如果是新做的一個系統(tǒng),先升級一下服務(wù)器的環(huán)境,yum和pip

yum update

pip install --upgrade pip

先安裝Python3

centos安裝python3

然后用pyvenv做隔離,因為我的服務(wù)器上跑的東西不多,先用這個對付著,服務(wù)器東西跑的多的,最好還是用Docker隔離比較徹底

到項目目錄后創(chuàng)建虛擬環(huán)境venv并激活

cd /var/www/django_web/

python3 -m venv venv

cd venv/bin

source activate

然后在開發(fā)環(huán)境打包一下需要的依賴包

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

把這個requirements.txt上傳到服務(wù)器上,再按這個包安裝環(huán)境

python3 -m pip install -r requirements.txt?

安裝的時候,裝到mysqlclient這個庫的時候出錯了,OSError: mysql_config not found

網(wǎng)上找了一下mysql_config要下面這些依賴,安裝上,就可以了

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

ftp上傳項目文件,然后生成數(shù)據(jù)庫,django官方推薦migrations放到版本控制中,所以migrations文件我一并上傳到服務(wù)器,不需要再make

python3 manage.py migrate

創(chuàng)建超級管理員帳號

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

之前的開發(fā)包里我已經(jīng)安裝了gunicorn,如果沒安裝就在服務(wù)器的虛擬環(huán)境里pip安裝一下

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

接著可以直接運行

gunicorn django_web.wsgi:application -b 0.0.0.0:8000

如果沒問題,可以接下去配置一個配置文件,方便以后設(shè)置

guni.conf.py

import multiprocessing

bind = '0.0.0.0:8000' #監(jiān)聽端口

workers = 2 #負責(zé)處理請求的線程數(shù),一般設(shè)置成:服務(wù)器CPU個數(shù) + 1

#worker_class = "gevent" #使用的網(wǎng)絡(luò)模型,默認sync,如果要性能好點就用gevent,我的項目暫時不用,注釋掉

worker_connections = 200 #同時最大連接數(shù)

#daemon = True #是否后臺運行,因為使用了supervisor,gunicorn不需要運行daemon模式

pidfile = 'gunicorn.pid' #pid文件

proc_name = 'guni_web' #進程名稱

reload = True #當(dāng)代碼有更新的話自動重啟workers,有點類似于調(diào)試模式

errorlog = "/var/www/django_web/log/guni_error.log" #錯誤日志#

accesslog = "/var/www/django_web/log/guni_access.log" #訪問日志

#loglevel = 'debug'

用 -c 參數(shù)運行g(shù)unicorn

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


Supervisor安裝

先退出虛擬環(huán)境再安裝,否則這個是裝到虛擬環(huán)境下的

deactivate

pip install supervisor

這個地方有可能會出現(xiàn)pip command not found的錯誤

需要安裝一下pip,就可以了

yum install epel-release?

yum install -y python-pip?

配置文件,先創(chuàng)建兩個目錄

mkdir /etc/supervisor

mkdir /etc/supervisor/conf.d

創(chuàng)建配置文件:/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

創(chuàng)建子配置文件 /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參數(shù)指定配置文件來運行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

# 結(jié)束所有屬于名為 groupworker 這個分組的進程 (start,restart 同理)

supervisorctl stop groupworker:

# 結(jié)束 groupwor:name1 這個進程 (start,restart 同理)

supervisorctl stop groupworker:name1

# 停止全部進程,注:start、restart、stop 都不會載入最新的配置文件

supervisorctl stop all

# 載入最新的配置文件,停止原有進程并按新的配置啟動、管理所有進程

supervisorctl reload

# 根據(jù)最新的配置文件,啟動新配置或有改動的進程,配置沒有改動的進程不會受影響而重啟

supervisorctl update

創(chuàng)建一個supervisor服務(wù),這樣服務(wù)器重啟后會自動啟動

創(chuàng)建文件/usr/lib/systemd/system/supervisord.service

# dservice for systemd (CentOS 7.0+)

# by ET-CS (https://github.com/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

開啟服務(wù)

systemctl enable supervisord

檢查一下看看是否設(shè)置成功

systemctl is-enabled supervisord

Gunicorn無法處理靜態(tài)資源,所以還需要Nginx

先把Django項目的靜態(tài)文件收集到一個地方

修改settings.py ,這個路徑就是我們要收集所有靜態(tài)文件的路徑,到時候加到nginx

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

在虛擬環(huán)境下運行

python manage.py collectstatic

安裝Nginx

yum install nginx -y

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

server{

? ? listen 80;監(jiān)聽的端口

? ? server_name 127.0.0.1;

? ? server_name your_www;

? ? #當(dāng)請求這些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 顧名思義,定位,就是當(dāng)訪問 / 的時候,nginx會將請求轉(zhuǎn)給本地的8080端口,而后面的設(shè)置都是一些基本的配置,可以直接用? ?

location /static {

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

? ? }

? ? # 這個就是配置靜態(tài)文件的地方,要用絕對地址,你在開發(fā)的時候建議每個app的靜態(tài)文件都用多了一層app_name的文件夾來包住。

}

配置好后,先檢查一下對不對,如果沒有問題就可以運行了

nginx -t

運行nginx的命令很簡單,直接命令行輸入nginx即可

nginx -s reload|reopen|stop|quit #重新加載配置|重啟|停止|退出 nginx

如果有防火墻,打開服務(wù)器80端口,加上--permanent就是永久有效,不加重啟后會失效

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

firewall-cmd --reload

阿里云還要開一個安全組的80端口

這樣基本就部署完了,用瀏覽器打開就可以訪問到你的網(wǎng)頁了。

如果怕nginx會運行中崩潰,或者是想重啟后自動開啟,直接加個文件到supervisor里面就可以自動監(jiān)控了

創(chuàng)建子配置文件 /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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容