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即可。