記錄下這次實踐,避免以後遺忘
確保服務器引導時自動啓動Gunicorn,如果Gunicorn崩潰則自動重啓
#gunicorn可執行文件所在位置
/root/venv/pythonTDD-env/bin/gunicorn
#Systemd腳本保存位置,所寫的腳本要以.service結尾
/etc/systemd/system/
編寫腳本:pythonTDD.service
[Unit]
Description=Gunicorn server for pythonTDD
[Service]
Restart=on-failure #指明在進程崩潰時自動重啓進程
User=tu #指明啓動進程的用戶身份
#項目的工作目錄,manage.py所在的目錄
WorkingDirectory=/project/pythonTDD/PythonTDD/superlists
#要執行的進程
ExecStart=/root/venv/pythonTDD-env/bin/gunicorn --bind unix:/tmp/pythonTDD.socket superlists.wsgi:application
[Install]
#告訴systemd,在引導時啓動這個服務
WantedBy=multi-user.target
瞭解systemd可以看這篇文章:
centos7的systemd服務詳解
#必須先執行這個命令,讓systemd加載新的配置文件
(pythonTDD-env) [root@bogon superlists]# systemctl daemon-reload
#這個命令讓systemd在引導時加載服務
(pythonTDD-env) [root@bogon superlists]# systemctl enable pythonTDD.service
Created symlink from /etc/systemd/system/multi-user.target.wants/pythonTDD.service to /etc/systemd/system/pythonTDD.service.
#這個命令啓動服務
(pythonTDD-env) [root@bogon superlists]# systemctl start pythonTDD.service