centos7 是用Systemd进行系统初始化的,Systemd是linux系统最新的初始化系统,
Systemd服务文件以.service结尾。如果用yum 命令安装的,yum会自动创建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]:服务运行参数设置
Type:forking 是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
- 设置开机启动
systemctl enable nginx.service
- 启动nginx服务
systemctl start nginx.service
- 停止开机启动
systemctl disable nginx.service
- 查看服务当前状态
systemctl status nginx.service
- 重启服务
systemctl restart nginx.service
- 查看已启动服务
systemctl list-units --type=service
- 出现问题:
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即可。