1.簡易安裝nginx:
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid
2.進入/usr/lib/systemd/system目錄下,編寫nginx.service文件:
如果是自己編譯安裝,則需要自己編寫nginx.service
[Unit] //服務說明
Description=nginx - high performance web server //服務描述
After=network.target remote-fs.target nss-lookup.target //服務類別
[Service] //服務
Type=forking //類型,forking代表後臺運行
ExecStart=/usr/local/nginx/nginx -c /usr/local/nginx/nginx.conf //啓動命令
ExecReload=/usr/local/nginx/nginx -s reload //重啓命令
ExecStop=/usr/local/nginx/nginx -s stop //停止命令
//PrivateTmp:true表示給服務分配獨立的空間
[Install] //服務安裝的相關設置,設置爲多用戶
WantedBy=multi-user.target
3.設置開機自動啓動:
systemctl enable nginx.service
回顯提示:Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
4.執行步驟3後,會在/etc/systemd/system/multi-user.target.wants/目錄下生成nginx.service的軟鏈接,multi-user.target.wants目錄對應2步驟的WantedBy制定的運行級別,此時可以執行相關命令操作nginx服務:
systemctl enable nginx.service 開機啓動nginx服務
systemctl disable nginx.service 禁止開機啓動nginx服務
systemctl is-enable nginx.service 查詢是否開機啓動nginx服務
systemctl start nginx.service 啓動nginx服務
systemctl stop nginx.service 停止nginx服務
systemctl reload nginx.service 重新加載nginx服務
systemctl status nginx.service 查看nginx服務狀態