CentOS7將Nginx添加系統服務的方法步驟

https://www.jb51.net/article/157141.htm

導語

經過編譯安裝以及解決問題,Nginx 已經運行正常,但是此時 Nginx 並沒有添加進系統服務。接下來會將 Nginx 添加進系統服務並且設置開機啓動。

查看服務

首先查看 Nginx 的服務狀態,輸入 systemctl status nginx,結果如下

沒有找到相關的服務,下一步就是添加系統服務。

添加系統服務

/usr/lib/systemd/system 目錄中添加 nginx.service,根據實際情況進行修改,詳細解析可查看下方參考資料中的文章。內容如下

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target


添加完成後再來看下[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target

設置開機自動啓動

首先測試一下,重啓然後來查看 Nginx 服務

確實沒有啓動。輸入 systemctl start nginx 啓動

可以使用 systemctl 啓動,說明之前添加的 nginx.service 沒有問題。然後輸入 systemctl enable nginx 設置開機啓動

最後重啓檢查下是否設置成功

沒有問題,到此關於 Nginx 的編譯安裝完成,接下來是 PHP 的安裝。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章