天天看點

centos7中設定nginx的systemctl啟動方式

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的權限儲存在目錄:

/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

-----------------------------------------要克服生活的焦慮和沮喪,得先學會做自己的主人-----------------------------------------