Bob's Blog

Web开发、测试框架、自动化平台、APP开发、机器学习等

返回上页首页

Systemd管理gunicorn服务



以前做python的web server用了uwsgi还不错,后来换了gunicorn也相当巴适。

在新的linux服务器上,为了方便启动和管理web服务进程,于是用了systemd。

在systemd中每个系统服务被称为一个服务单元unit,服务单元又可以区分为service、socket、target、path、snapshot、timer等多种不同类型,最常见的是以.service结尾的文件来定义服务单元的信息,并由systemctl来做各种操作。

像以前古老时候,维持服务的方式有手动(进入虚拟环境,启动gunicorn),或者用shell文件来启动,乃至在crontab里定时检查,或者在initd中配置。但systemd比这些方便得多。

首先进入目录/usr/lib/systemd/system/,创建一个文件,取名类似于gunicorn.service,编辑如下内容:

[Unit]
Description=Python Web Server
Wants=network-online.target

[Service]
Type=forking
PIDFile=/path/gunicorn.pid   # gunicorn的pid
User=root
WorkingDirectory=/web_service_folder_path  # 工作目录路径
ExecStart=/path/venv/bin/gunicorn -c configuration/guni-cfg.py API.wsgi:application   # 用虚拟环境启动命令,指定配置文件,指定django等项目的wsgi
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
KillMode=mixed
TimeoutStopSec=5
RestartSec=3s
Restart=always

[Install]
WantedBy=multi-user.target

其实ExecStart也可以直接用gunicorn的命令,不用指定配置文件,只是配置项较多时,命令行难免过长,所以指定文件,而配置文件内容可以是:

from multiprocessing import cpu_count
import os
current_dir = os.path.dirname(os.path.abspath(__file__))
gun_log_dir = os.path.join(current_dir, "gunicorn_log")
os.makedirs(gun_log_dir, exist_ok=True, mode=0o775)


bind = ["127.0.0.1:8001"]
daemon = True
workers = cpu_count()
threads = 2
worker_class = "gevent"
worker_connections = 65535
keepalive = 60
timeout = 30
graceful_timeout = 10
capture_output = True
loglevel = "debug"
pidfile = os.path.join(gun_log_dir, 'gunicorn.pid')
accesslog = os.path.join(gun_log_dir, 'access.log')
errorlog = os.path.join(gun_log_dir, 'error.log')

接着运行systemctl命令,启动,并随开机运行,指定.service文件的时候可以不用给全名

systemctl start gunicorn
systemctl enable gunicorn

之后便可开机即有服务,异常退出了也能自动重启服务。

上一篇:  记录桌面端应用的自动化的一些坑和解决方式

共有0条评论

添加评论

暂无评论