Nginx修改默认80端口启动‘Job for nginx.service failed ’

1,085 阅读2分钟

本地机新安装的centos7,修改默认80端口,在开启nginx服务时出错“Unable to start service nginx: Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.\n”

systemctl status nginx

avatar

systemctl restart nginx 执行后

Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

解决方法:

关闭SELinux

修改/etc/selinux/config文件中的SELINUX="" 为 disabled ,然后重启。

如果不想重启系统,使用命令setenforce 0
setenforce 1 设置SELinux 成为enforcing模式

setenforce 0 设置SELinux 成为permissive模式

在lilo或者grub的启动参数中增加:selinux=0,也可以关闭selinux

vi /etc/selinux/config(也有人说是/etc/sysconfig/selinux文件,其实两个之间是链接关系,随便改其中一个,另一个也改了)

SELINUX=disable 禁用SeLinux

SELINUX=enforcing 启用SeLinux

关闭SELinux后,运行“systemctl restart nginx”,nginx正常启动

原因

SELinux是一个进程访问控制子系统,默认安装在 Fedora 和 Red Hat Enterprise Linux 上,它能使进程权限最小化。

SELinux对于进程只赋予最小的权限 :TE (Type Enforcement)――― 对于进程只赋予最小的权限 Te概念在 SELinux里非常的重要。它的特点是对所有的文件都赋予一个叫type的文件类型标签,对于所有的进程也赋予各自的一个叫 domain的 标签。Domain标签能够执行的操作也是由access vector在策略里定好的。 我们熟悉的apache服务器,httpd进程只能在httpd_t 里运行,这个httpd_t 的domain能执行的操作,比如能读网页内容文件赋予httpd_sys_content_t,密码文件赋予shadow_t,TCP的80端口赋予 http_port_t等等。如果在access vector里我们不允许 http_t来对http_port_t进行操作的话,Apache启动都启动不了。反过来说,我们只允许80端口,只允许读取被标为 httpd_sys_content_t的文件,httpd_t就不能用别的端口,也不能更改那些被标为httpd_sys_content_t的文件(read only)。 ——《百度百科》

百度百科上的资料太过高深(没看懂o(╥﹏╥)o),通俗讲应该是SELinux对nginx的启动进程进行了限制,只能使用默认的80端口;一旦你在nginx.conf中修改80位其他端口,启动服务/usr/sbin/nginx将无权调用配置文件/etc/nginx/nginx.conf,即错误中显示的“ExecStart=/usr/sbin/nginx -C /etc/nginx/nginx.conf (code=exited,status=1/FALIURE)”

所以,把SELinux关掉就好了或者设置成permissive。