systemd管理虚拟环境Django+uwsgi+nginx配置教程

下面我直接写出模板,直接复制修改便可使用.--by jonnyan404 https://mrdoc.fun

uwsgi.ini配置

[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /opt/jonnyan404/MrDoc
# Django's wsgi file
module          = MrDoc.wsgi:application
wsgi-file       = MrDoc/wsgi.py
# the virtualenv (full path)
home            = /opt/jonnyan404/mrdoc_env

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 5
# the socket (use the full path to be safe
socket          = /opt/jonnyan404/mrdoc.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true
  • chdir: django项目路径(绝对路径)
  • module: django版本小于1.4版本需要这个参数
  • wsgi-file: django项目的wsgi.py文件目录,相对于chdir的相对目录
  • home: 虚拟环境路径(绝对路径),没有可忽略此参数
  • master: uwsgi服务器角色
  • processes: 进程数,建议设置为电脑cpu核数
  • socket: 可为文件形式,也可为IP+port形式,与nginx连接通道
  • chmod-socket: 给上一步socket文件授权
  • vacuum: 是否在退出时,清理uwsgi环境
    --by jonnyan404 https://mrdoc.fun

systemd配置

[Unit]
Description=mrdoc service by jonnyan404
Requires=network.target
After=network.target
After=syslog.target

[Service]
TimeoutStartSec=0
RestartSec=3
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
StandardError=syslog
RuntimeDirectory=uwsgi
# Main call: Virtual env is activated and uwsgi is started with INI file as argument
ExecStart=/bin/bash -c 'cd /opt/jonnyan404/; source mrdoc_env/bin/activate; uwsgi --ini /opt/jonnyan404/mrdoc_uwsgi.ini'

[Install]
WantedBy=multi-user.target

nginx配置

server {
    # the port your site will be served on
    listen      10086;
    # the domain name it will serve for
    server_name _; # substitute your machine's IP address or FQDN
    charset     utf-8;
    access_log /opt/jonnyan404/mrdoc-nginx-access.log;
    error_log  /opt/jonnyan404/mrdoc-nginx-error.log;
    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /opt/jonnyan404/MrDoc/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /opt/jonnyan404/MrDoc/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  unix:///opt/jonnyan404/mrdoc.sock;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}

Reference Link

原文地址:https://www.cnblogs.com/jonnyan/p/14715064.html