linux7 nginx开机启动,Centos7下设置Nginx开机启动

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值