Django+Gunicorn+caddy部署

假设django的项目名mysite应用名myapp
且django的项目放置在ubuntu的/home/目录下

配置gun的ini文件:

在/etc/systemd/system/目录创建一个名为gunicorn.service的服务单元文件:

[Unit]
Description=btcmai
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/mysite
ExecStart=/usr/local/bin/gunicorn --workers 2 --bind unix:/home/mysite/mysite.sock mysite.wsgi:application

[Install]
WantedBy=multi-user.target

huey自动启动配置文件

创建/etc/systemd/system/huey.service

[Unit]
Description=Huey
After=network.target gunicorn.service

[Service]
User=root
Group=root
WorkingDirectory=/home/mysite
ExecStart=/usr/bin/python3 /home/mysite/manage.py run_huey -w 40 -k greenlet
Restart=always
ExecStartPre=/bin/sleep 20

[Install]
WantedBy=multi-user.target

配置caddy

c.3kj.online {
    tls /home/ssl/3kj.online.pem /home/ssl/3kj.online.pem.key
    file_server
    root * /home/mysite/myapp/jingtai/
    
    #设置一个匹配zhuanfa的路由
    @zhuanfa {
         path / /logout /send_emailcode /muxiaoliang123/admin* /*/*/ /*/*/register /*/*/login /*/*/active /*/*/ceshi
    }
    
    #将匹配的路由转发给bottle这里转发到两个端口用于负载均衡
    handle @zhuanfa {
        reverse_proxy unix//home/mysite/mysite.sock
    }
}

#重新加载systemd配置
systemctl daemon-reload

#启动huey
sudo systemctl start huey

#重启gun
sudo systemctl restart gunicorn

#让huey开机自启动
sudo systemctl enable huey

#验证服务单元是否正在运行
sudo systemctl status huey

#停止huey
sudo systemctl stop huey

#查看错误日志信息
sudo journalctl -u gunicorn.service -r
sudo journalctl -u huey.service -r

sudo journalctl -u huey.service -r
sudo journalctl -u huey.service -r -S "YYYY-MM-DD HH:MM:SS" -U "YYYY-MM-DD HH:MM:SS"