gunicorn开机自启

发布时间 2023-08-26 10:09:54作者: 昵称已经被使用

gunicorn设置开机自启

参考博客:

ubuntu配置gunicorn开机启动

可能是史上最全面易懂的 Systemd 服务管理教程!( 强烈建议收藏 )

需要开机运行项目,使用systemctl来控制gunicorn开机启动

systemctl配置文件

官方文档:systemd.service

/etc/systemd/system下增加文件project.service,文件名根据需要命名,以.service结尾,文件内容:

[Unit]
# 描述
Description=Gunicorn
# 在网络服务启动后再启动
After=network.target
 
[Service]
# 指定运行服务的用户
User=ubuntu
# 指定运行服务的用户组
Group=www-data
# 为服务指定环境变量
Environment="Path=/home/ubuntu/venv/bin"
# 项目文件目录
WorkingDirectory=/home/ubuntu/venv/wwwroot/HospitalReview
# gunicorn启动命令
ExecStart=/home/ubuntu/venv/bin/gunicorn --workers 4 -b 0.0.0.0:8080 main:app
# 错误重启
Restart=on-failure
 
[Install]
WantedBy=multi-user.target

启动命令

配置好配置文件后,需要执行命令启动服务

# 重新加载配置文件
sudo systemctl daemon-reload
# 开启服务
sudo systemctl start n20.service
# 查看服务状态
sudo systemctl status n20.service
# 设置开机启动
sudo systemctl enable n20.service