python学习笔记:部署django代码到gunicorn【ubutnu18.04】(3)

    前面两篇文档,我们介绍了,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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值