基于 .env 下的 Flask + gunicorn + systemctl 服务部署配置如下
1、创建 Flask 服务管理脚本. 具体示例如下,
$ cd /etc/systemd/system/
$ vim application-alias.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
WorkingDirectory=/applicaton-path/
EnvironmentFile=/applicaton-path/.env
ExecStart=/applicaton-path/venv/bin/gunicorn \
--worker-class=gevent \
--workers 7 \
--error-logfile /var/log/application-alias/error.log \
--bind 127.0.0.1:8000 run:app \
--access-logfile /var/log/application-alias/access.log \
--worker-connections 1500 \
--backlog 2048 \
--timeout 30 \
--graceful-timeout 30 \
--keep-alive 3
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2、启动 application-alias 项目服务
1)、重新加载所有服务配置,如下命令:
$ systemctl daemon-reload
2)、启动服务、赞同服务、重启服务、查看服务运行状态
$ sudo systemctl status application-alias.service
gunicorn.service - gunicorn daemon
Loaded: loaded (/etc/systemd/system/application-alias.service; disabled; vendor preset: enabled)
Active: active (running) since Sun 2021-09-26 17:20:51 CST; 12min ago
Main PID: 21784 (gunicorn)
Tasks: 8 (limit: 4915)
CGroup: /system.slice/gunicorn.service
├─21784 /applicaton-path/venv/bin/python3 /applicaton-path/venv/bin/gunicorn --worker-class=gevent --workers 7 --error
├─21812 /applicaton-path/venv/bin/python3 /applicaton-path/venv/bin/gunicorn --worker-class=gevent --workers 7 --error
├─21813 /applicaton-path/venv/bin/python3 /applicaton-path/venv/bin/gunicorn --worker-class=gevent --workers 7 --error
├─21814 /applicaton-path/venv/bin/python3 /applicaton-path/venv/bin/gunicorn --worker-class=gevent --workers 7 --error
├─21815 /applicaton-path/venv/bin/python3 /applicaton-path/venv/bin/gunicorn --worker-class=gevent --workers 7 --error
├─21817 /applicaton-path/venv/bin/python3 /applicaton-path/venv/bin/gunicorn --worker-class=gevent --workers 7 --error
├─21818 /applicaton-path/venv/bin/python3 /applicaton-path/venv/bin/gunicorn --worker-class=gevent --workers 7 --error
└─21819 /applicaton-path/venv/bin/python3 /applicaton-path/venv/bin/gunicorn --worker-class=gevent --workers 7 --error
$ sudo systemctl stop application-alias.service
$ sudo systemctl start application-alias.service
$ sudo systemctl restart application-alias.service
3、Systemctl 可以管理其他Java、php 等服务,只要时有服务进程管理都可以用改Systemctl 管理
1)启动Java 服务脚本例如下
[Unit]
Description=foundation
After=syslog.target
[Service]
User=root
ExecStart=/usr/bin/java -jar /application-alias.jar
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
What is Systemctl? An In-Depth Overview
|