設置nginx開機自啟動(centos7.x)


第一步:進入到/lib/systemd/system/目錄

[root@localhost ~]# cd /lib/systemd/system/

第二步:創建nginx.service文件,並編輯

# vim nginx.service

內容如下:

[Unit]

Description=nginx service

After=network.target

 

[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s quit

PrivateTmp=true

   

[Install]

WantedBy=multi-user.target

[Unit]:服務的說明
Description:描述服務
After:描述服務類別
[Service]服務運行參數的設置
Type=forking是后台運行的形式
ExecStart為服務的具體運行命令
ExecReload為重啟命令
ExecStop為停止命令
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:[Service]的啟動、重啟、停止命令全部要求使用絕對路徑
[Install]運行級別下服務安裝的相關設置,可設置為多用戶,即系統運行級別為3

保存退出。

第三步:加入開機自啟動

# systemctl enable nginx

如果不想開機自啟動了,可以使用下面的命令取消開機自啟動

# systemctl disable nginx

第四步:服務的啟動/停止/刷新配置文件/查看狀態

# systemctl start nginx.service          啟動nginx服務
# systemctl stop nginx.service           停止服務
# systemctl restart nginx.service        重新啟動服務
# systemctl list-units --type=service     查看所有已啟動的服務
# systemctl status nginx.service          查看服務當前狀態
# systemctl enable nginx.service          設置開機自啟動
# systemctl disable nginx.service         停止開機自啟動

 

一個常見的錯誤

Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.

直接按照提示執行命令systemctl daemon-reload 即可。

 

# systemctl daemon-reload


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM