Django项目部署: supervisor+gunicorn+Nginx

Django项目部署: supervisor+gunicorn+Nginx

Gunicorn 绿色独角兽'是一个Python WSGI UNIX的HTTP服务器。这是一个pre-fork worker的模型,从Ruby的独角兽(Unicorn )项目移植。该Gunicorn服务器大致与各种Web框架兼容,只需非常简单的执行,轻量级的资源消耗,以及相当迅速。

项目发布思路

  • 用gunicorn来运行django app
  • 用supervisor来管理gunicorn的进程
  • 用nginx来配置web,来发布static和media文件

注意: python manage.py runsever当是调试模式的时候,会监测静态文件等的,在生产环境中,我们静态文件是用ngnix来当服务器的。

准备工作

  • 安装了Python虚拟环境在:/data/.env/3.4.2
  • 激活虚拟环境: source /data/.env/3.4.2/bin/activate
  • 安装django项目依赖: pip install django gunicorn
  • 安装supervisor:以ubuntu或者debian为例: apt-get install supervisor
  • 项目目录在:/data/www/mysite.com/public_html/
  • python manage.py runserver测试ok,再进行后续的操作

gunicorn基本使用

进入项目目录,简单运行gunicorn:

gunicorn codelieche.wsgi:application -b 127.0.0.1:8000

gunicorn需要的配置:
文件位置:/data/www/mysite.com/gunicorn.conf.py

import multiprocessing
bind="127.0.0.1:8081"
workers=3
errorlog = '/data/www/mysite.com/logs/gunicorn.error.log'
accesslog = '/data/www/mysite.com/logs/gunicorn.access.log'
loglevel = 'info'
proc_name="gunicorn_codelieche"

把配置写到一个文件中运行:

gunicorn codelieche.wsgi:application -c ../gunicorn.conf.py

现在用gunicron运行django项目:

  1. 先要激活python虚拟环境
  2. 进入django所在的项目
  3. 执行gunicorn操作。

下面我们吧这些操作写成一个shell脚本:run.sh
位置:/data/www/mysite.com/run.sh

#!/bin/bash
NAME="codelieche"
USER=usertest
WORKERS=2
DJANGODIR=/data/www/mysite.com
ENV_ACTIVATE_PATH=/data/.env/3.4.2/bin/activate
DJANGO_WSGI_MODULE=${NAME}.wsgi
PID_PATH=/tmp/gunicorn.${NAME}.pid

source $ENV_ACTIVATE_PATH
cd ${DJANGODIR}/public_html
# gunicorn ${DJANGO_WSGI_MODULE}:application --name $NAME --pid $PID_PATH -c ${DJANGODIR}/gunicorn.conf.py 
# 可以用配置文件,但是还是直接所有配置写到run.sh文件中 方便查看

exec gunicorn ${DJANGO_WSGI_MODULE}:application \
    --name $NAME \
    --user $USER \
    --pid $PID_PATH \
    --workers $WORKERS \
    --bind=127.0.0.1:8081 \
    --log-level=info \
    --access-logfile=${DJANGODIR}/logs/gunicorn.access.log \
    --error-logfile=${DJANGODIR}/logs/gunicorn.error.log \

run.sh添加运行权限:

chmod +X run.sh
./run.sh 

注意事项:

  • run.sh中设置的user要对,配置的logfile有写的权限,项目文件也要有读写权限
  • 这里运行的static,media文件我们用nginx来配置的

supervisor

supervisor就是用Python开发的一套通用的进程管理程序.能将一个普通的命令行(supervisorctl start appname)进程变为后台驻留程序(daemon),并监控进程状态,异常退出时能自动重启。

安装: sudo apt-get install supervisor(debian 为例)

编写supervisor配置文件

sudo vim /etc/supervisor/conf.d/app.conf

配置文件内容:

[program:appname]
user=usertest
command = /data/www/mysite.com/run.sh
environment=MYSQL_USER=user,MYSQL_PASSWORD=xxxxxx
stdout_logfile = /data/www/mysite.com/logs/supervisor.log
redirect_stderr = true

启动appname

usertest@debian:~ $ sudo supervisorctl start codelieche
codelieche: started

项目启动成功
如果启动失败,可以查看日志,大部分是读写权限问题.
同时注意djang项目中需要用到的环境变量注意配置好。

停止、重启、查看状态: stop,restart,status.

$ sudo supervisorctl status codelieche
appname        RUNNING    pid 23765, uptime 0:12:33

nginx配置文件

Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。

文件位置: /etc/nginx/sites-enabled/mysite.com

server {
    listen 80; 
    server_name codelieche.com www.codelieche.com;
    access_log /data/www/mysite.com/logs/access.log;
    error_log /data/www/mysite.com/logs/error.log;

    location / { 
        proxy_pass http://127.0.0.1:8081;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }   

    location /robots.txt {
        alias /data/www/mysite.com/public_html/static/robots.txt;
    }   

    location /favicon.ico {
        alias /data/www/mysite.com/public_html/static/favicon.ico;
    }   

    location ~ ^/(media|static)/ {
        root /data/www/mysite.com/public_html;
        expires 30d;
    }   

    location ~ /\. {
        access_log off; log_not_found off; deny all;
    }   
}

nginx重启/重新加载配置:service restart/reload nginx

到这里: supervisor + gunicorn + nginx部署Django项目就OK了。

注意事项

  1. 环境变量的设置一定要正确
  2. 注意gunicorn运行脚本,exec和直接用gunicorn
  3. 遇到问题,注意查看配置的日志文件,特别是报错文件log信息

遇到的问题

1. 执行shell脚本,总是说路径找不到

-bash: ./run.sh: /bin/bash^M: 解释器错误: 没有那个文件或目录 原因是:run.sh脚本的文件格式不对。

  • vim run.sh
  • set ff可以查看文件的编码:fileformat=dos
  • set ff=unix吧它强制为unix格式,保存退出后就可以运行了

2. supervisorctl status app

error: <class 'socket.error'="">, [Errno 13] Permission denied: file: /usr/lib/python2.7/socket.py line: 224

  • 原因1:在/etc/supervisor/conf.d/app.conf中没设置user
  • 原因2:在/etc/supervisor/conf.d/app.conf中没设置依赖的环境变量
  • 原因3:gunicorn配置的pid文件log文件,执行user没有写入权限
posted @ 2021-10-14 15:01  较劲儿  阅读(635)  评论(0编辑  收藏  举报