在 Ubuntu 上使用 Nginx+ uWSGI 部署Django項目,在實際生成環境中需要系統自動啟動這兩項服務,Ubuntu默認自啟動Nginx,需要對uwsgi設置為系統自啟動。
Ubuntu1804采用systemctl來管理開機啟動的腳本,對於uwsgi服務要設置成系統服務來進行自啟動。
1、首先創建 uwsgi.service
文件
目錄位置:/etc/systemd/system/
sudo vim /etc/systemd/system/uwsgi.service
Unit]
Description=uWSGI server
After=network.target
[Service]
#用戶名
User=user_name
#項目目錄
WorkingDirectory=/home/username/projectname
#需要的環境變量配置文件 #Environment=/home/username/projectname/env.cfg
#服務啟動的代碼,可用which命令查看uwsgi的具體位置User=username ExecStart=/home/username/.local/bin/uwsgi --ini uwsgi.ini
#用戶組
Group=www-data
[Install]
#指明會跟隨系統啟動而啟動該服務
WantedBy=multi-user.target
2、啟動、停止、重啟服務命令 :
sudo systemctl start uwsgi.service
sudo systemctl stop uwsgi.service
sudo systemctl restart uwsgi.service
3、加入系統自啟動:
sudo systemctl daemon-reload
sudo systemctl enable uwsgi.service
執行此命令后在/etc/systemd/system/multi-user.target.wants目錄下生成uwsgi.sevice的鏈接文件,
Ubuntu1804系統的自啟動項目均在此目錄下。重啟系統后,可看到uwsgi服務已啟動。
4、總結:
1、建立service文件,必須保證文件編輯正確,因為不返回執行結果;注意注釋格式(最好去掉),建議使用vi編輯,有的編輯器會帶有空格;
2、建立鏈接文件或復制文件到/etc/systemd/system/目錄中;
3、通過systemctl enable uwsgi.service命令在/etc/systemd/system/multi-user.target.wants目錄下生成uwsgi.sevice的鏈接文件。