centos7中設置nginx的systemctl啟動方式
1.建立服務文件
(1)文件路徑
vim /usr/lib/systemd/system/nginx.service
(2)服務文件內容
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/opt/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
(3)文件內容解釋
[Unit]:服務的說明
Description:描述服務
After:描述服務類別
[Service]服務運行參數的設置
Type=forking是后台運行的形式
ExecStart為服務的具體運行命令
ExecReload為重啟命令
ExecStop為停止命令
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:啟動、重啟、停止命令全部要求使用絕對路徑
[Install]服務安裝的相關設置,可設置為多用戶
(4)使文件生效
systemctl daemon-reload
2.保存目錄
(1)以754的權限保存在目錄:
Chmod +754 /usr/lib/systemd/system
(2)設置開機自啟動
systemctl enable nginx.service
(3)啟動nginx服務
systemctl start nginx.service
(4)停止開機自啟動
systemctl disable nginx.service
(5)查看服務當前狀態
systemctl status nginx.service
(6)重新啟動服務
systemctl restart nginx.service
(7)查看所有已啟動的服務
systemctl list-units --type=service
3.命令集合
systemctl is-enabled servicename.service #查詢服務是否開機啟動
systemctl enable *.service #開機運行服務
systemctl disable *.service #取消開機運行
systemctl start *.service #啟動服務
systemctl stop *.service #停止服務
systemctl restart *.service #重啟服務
systemctl reload *.service #重新加載服務配置文件
systemctl status *.service #查詢服務運行狀態
systemctl --failed #顯示啟動失敗的服務
注:*代表某個服務的名字,如http的服務名為httpd