前面两篇文档,我们介绍了,gunicorn直接启动django项目和带配置文件启动django项目两种方式。
但是呢,如果重启了系统,我不是得每次都得收到执行一次gunicorn命令,可不可以改成开机自动启动,把gunicorn当作想nginx一样的服务?
答案当然是可以的,不过这时候需要我们对linux的systemd这个东西有一定程度的认识。
1 gunicorn相关目录配置,如下表
gunicron | 在【Ubuntu 18.04】下所涉及的目录或者文件 | 目录内容 | 说明 |
数据目录 | /var/www/certificate_system | ![]()
| django项目目录,从开发者电脑上传过来的 |
配置文件目录 | /etc/gunicorn/ | ![]()
| 手动添加 |
日志文件目录 | /var/log/gunicorn/ | ![]()
| 访问日志和错误日志,启动gunicorn时自动生成 |
进程文件 | /usr/local/bin/gunicorn | ![]()
| 通过apt install gunicorn安装后,自动就有 |
进程ID文件 | /run/gunicorn.pid | ![]()
| 启动gunicorn时自动生成 |
systemd配置文件 | /lib/systemd/system/gunicorn.service | ![]()
| 手动添加 |
2 服务配置文件gunicorn.service内容如下
wenca@wenca-ubuntu:/lib/systemd/system$ sudo systemctl cat gunicorn
# /lib/systemd/system/gunicorn.service
===============================================================================
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
Type=notify
RuntimeDirectory=gunicorn
WorkingDirectory=/var/www/certificate_system
ExecStart=/usr/local/bin/gunicorn myproject.wsgi -c /etc/gunicorn/gunicorn.conf
ExecReload=/bin/kill -s HUP $MAINPID
#ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/gunicorn.pid
KillMode=mixed
TimeoutStopSec=5
PrivateTmp=true
[Install]
WantedBy=multi-user.target
3 启动gunicorn服务
sudo systemctl daemon-reload
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
sudo systemctl status gunicorn
sudo systemctl restart gunicorn