Nginx + Django + uwsgi + celery + supervisor 部署多个django项目

阿里云CentOS 7 中部署多个django项目

  • 一、nginx
  • 二、uwsgi
  • 三、用 supervisor 守护进程启动 celery
      • 一、安装
      • 二、配置
      • 三、常用命令
      • 四、开机自启

一、nginx

server {
    listen 80;
    server_name 外网ip地址;

    location / {
        include /etc/nginx/uwsgi_params;
        uwsgi_read_timeout 3600;
        uwsgi_send_timeout 300;
        proxy_read_timeout 300;
        proxy_connect_timeout 300;
        proxy_send_timeout 300;
        uwsgi_pass 127.0.0.1:8000;
    }
    location  ~/static/ {
        root /data/wwwroot/product/django_package;
    }
}

server {
    listen 8001;   # 此处对应外部访问的端口,即需要在防火墙和安全组开启的端口
    server_name 外网ip地址;

    location / {
        include /etc/nginx/uwsgi_params;
        uwsgi_read_timeout 3600;
        uwsgi_send_timeout 300;
        proxy_read_timeout 300;
        proxy_connect_timeout 300;
        proxy_send_timeout 300;
        uwsgi_pass 127.0.0.1:8002;   # 此处对应 uwsgi 的配置,端口不能与 listen 中的一样
    }
    location  ~/static/ {
        root /data/wwwroot/develop/django_package;
    }
}

nginx自启:/lib/systemd/system/nginx.service

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

二、uwsgi

uwsgi 启动文件:vim /etc/uwsgi.ini

[uwsgi]
uid = uwsgi
gid = uwsgi
pidfile = /run/uwsgi/uwsgi.pid
emperor = /etc/uwsgi.d
stats = /run/uwsgi/stats.sock
chmod-socket = 660
emperor-tyrant = true
cap = setgid,setuid

正式环境:/etc/uwsgi.d/product.ini

[uwsgi]
plugins       = python36u
master        = true
protocol      = uwsgi
socket        = 127.0.0.1:8000
wsgi-file     = /data/wwwroot/product/django_package/django_package/wsgi.py
home          = /data/wwwroot/django_project/env
chdir         = /data/wwwroot/product/django_package
buffer-size   = 8192
enable-threads= true
close-on-exec = true

测试环境:/etc/uwsgi.d/develop.ini

[uwsgi]
plugins       = python36u
master        = true
protocol      = uwsgi
socket        = 127.0.0.1:8002
wsgi-file     = /data/wwwroot/develop/django_package/django_package/wsgi.py
home          = /data/wwwroot/django_project/env
chdir         = /data/wwwroot/develop/django_package
buffer-size   = 8192
enable-threads= true
close-on-exec = true

注意:以上两个 product.ini 和 develop.ini 配置文件的权限不能为 root,需要改为 nobody或者uwsgi;

uwsgi 自启设置:/lib/systemd/system/uwsgi.service

[Unit]
Description=uWSGI Emperor Service
After=syslog.target

[Service]
EnvironmentFile=-/etc/sysconfig/uwsgi
ExecStartPre=/bin/mkdir -p /run/uwsgi
ExecStartPre=/bin/chown uwsgi:uwsgi /run/uwsgi
ExecStart=/usr/sbin/uwsgi --ini /etc/uwsgi.ini
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGINT
Restart=always
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

三、用 supervisor 守护进程启动 celery

一、安装

pip3 install supervisor

二、配置

mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf
vim /etc/supervisor/supervisord.conf
[include]
files = conf.d/*.conf
mkdir /etc/supervisor/conf.d
vim /etc/supervisor/conf.d/celery.conf
[program:celery]
command=/data/wwwroot/django_project/env/bin/python manage.py celery worker -l info
directory=/data/wwwroot/develop/django_package
user=root
autostart=true
autorestart=true
stderr_logfile=/var/log/celery.err.log
stdout_logfile=/var/log/celery.out.log
supervisord -c /etc/supervisor/supervisord.conf

三、常用命令

# 命令启动
supervisord -c /etc/supervisor/supervisord.conf
        
# 重启
supervisorctl reload

# 关闭
supervisorctl shutdown

四、开机自启

vim /etc/systemd/system/supervisord.service

[Unit]
Description=supervisord - Supervisor process control system for UNIX
Documentation=http://supervisord.org
After=network.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecReload=/usr/bin/supervisorctl reload
ExecStop=/usr/bin/supervisorctl shutdown
User=root

[Install]
WantedBy=multi-user.target

systemctl daemon-reload
systemctl start supervisord.service
systemctl status supervisord.service
systemctl enable supervisord.service

你可能感兴趣的:(uwsgi,supervisor,celery)