背景生產環境中採用nginx + uwsgi + django 來部署web服務,這裏需要實現uwsgi的啓動和停止,簡單的處理方式可以直接在命令行中啓動和kill掉uwsgi服務,但爲了更安全、方便的管理uwsgi服務,配置uwsgi到systemd服務中,同時實現開啓自啓的功能;
另,鑑於supervisor不支持python3,沒采用supervisor來管理uwsgi服務;
具體配置方法如下:
step1. 創建配置文件
/etc/systemd/system/server_uwsgi.service
step2. 填入以下內容
[Unit]
Description=HTTP Interface Server
After=syslog.target
[Service]
KillSignal=SIGQUIT
ExecStart=/usr/bin/uwsgi --ini /path/uwsgi.ini
Restart=always
Type=notify
NotifyAccess=all
StandardError=syslog
[Install]
WantedBy=multi-user.target
step3. 將該服務加入到systemd中
systemctl enable /etc/systemd/system/server_uwsgi.service
然後就可以通過systemctl來控制服務的啓停
systemctl stop server_uwsgi.service 關閉uwsgi服務
systemctl start server_uwsgi.service 開啓uwsgi服務
systemctl restart server_uwsgi.service 重啓uwsgi服務
注意事項:
如果uwsgi配置文件中配置了 daemonize=/path/uwsgi.log (uwsgi服務以守護進程運行)
會導致sytemctl啓動時多次重啓而導致啓動失敗
需改爲 logto=/path/uwsgi.log