Centos7下設置Nginx開機啟動

centos7 是用Systemd進行系統(tǒng)初始化的,Systemd是linux系統(tǒng)最新的初始化系統(tǒng),
Systemd服務文件以.service結(jié)尾。如果用yum 命令安裝的,yum會自動創(chuàng)建nginx.service
直接用命令:

systemcel enable nginx.service

就可設置開機啟動
通過源碼編譯安裝的,需要手動建立nginx.service文件

vi /lib/systemd/system/nginx.service

[Unit]
Description=nginx
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

Description:描述服務
After:描述服務類別
[Service]:服務運行參數(shù)設置
Type:forking 是后臺運行的形式
ExecStart為服務的具體運行命令
ExecReload為重啟命令
ExecStop為停止命令
PrivateTmp=True表示給服務分配獨立的臨時空間

  • 設置開機啟動
systemctl enable nginx.service
  • 啟動nginx服務
systemctl start nginx.service
  • 停止開機啟動
systemctl disable nginx.service
  • 查看服務當前狀態(tài)
systemctl status nginx.service
  • 重啟服務
systemctl restart nginx.service
  • 查看已啟動服務
systemctl list-units --type=service
  • 出現(xiàn)問題:
systemctl start nginx
-- Unit nginx.service has begun starting up.
...
systemd[3279]: nginx.service: Failed to execute command: Permission denied
...
systemd[3279]: nginx.service: Failed at step EXEC spawning /home/dev/local/nginx/sbin/nginx: Permission denied
/etc/init.d/nginx start
Failed to start SYSV: NGINX is an HTTP(S) server

說明:直接通過/home/dev/local/nginx/sbin/nginx命令可以啟動,但是通過腳本命令無法啟動
以上2個問題可能是SELINUX問題:

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

修改SELINUX=disabled即可。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容