在安装完Nginx后服务启动失败的解决办法

问题:

在我安装完Nginx后,想要启动服务,但是启动失败,并报出如下提示:

systemctl start nginx  //启动服务
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.     //提示信息

问题分析

然后我就按照问题提示的命令查看了一下,然后就输出如下内容:

journalctl -xe
1月 01 20:14:07 localhost.localdomain nginx[7788]: nginx: configuration file /etc/nginx/nginx.conf test is successf
1月 01 20:14:07 localhost.localdomain nginx[7791]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already 
1月 01 20:14:07 localhost.localdomain nginx[7791]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in 
1月 01 20:14:08 localhost.localdomain nginx[7791]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already 
1月 01 20:14:08 localhost.localdomain nginx[7791]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in 
1月 01 20:14:08 localhost.localdomain nginx[7791]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already 
1月 01 20:14:08 localhost.localdomain nginx[7791]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in 
1月 01 20:14:09 localhost.localdomain nginx[7791]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already 
1月 01 20:14:09 localhost.localdomain nginx[7791]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in 
1月 01 20:14:09 localhost.localdomain nginx[7791]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already 
1月 01 20:14:09 localhost.localdomain nginx[7791]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in 
1月 01 20:14:10 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
1月 01 20:14:10 localhost.localdomain nginx[7791]: nginx: [emerg] still could not bind()
1月 01 20:14:10 localhost.localdomain systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit nginx.service has failed.
-- 
-- The result is failed.
1月 01 20:14:10 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.
1月 01 20:14:10 localhost.localdomain systemd[1]: nginx.service failed.
1月 01 20:14:10 localhost.localdomain polkitd[6139]: Unregistered Authentication Agent for unix-process:7781:265264

从输出的提示信息上可以看到”0.0.0.0:80 failed”字样,大概可能跟端口有关,然后再使用如下命令查看一下端口进程:
在安装完Nginx后服务启动失败的解决办法_第1张图片
通过监听端口,发现80端口已经被占用了,怪不得Nginx服务起不来,因为httpd和Nginx默认的监听端口一样。

解决办法

想要让Nginx服务启动起来有两种方法:
方法1: 停止之前的httpd服务

systemctl stop httpd

方法2: 修改Nginx服务的默认监听端口

vim /etc/nginx/nginx.conf
server {
        listen       8080 default_server;
        listen       [::]:8080 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

然后再次启动Nginx服务就没有问题了

systemctl start nginx
systemctl enable nginx

已经监听nginx
在安装完Nginx后服务启动失败的解决办法_第2张图片

你可能感兴趣的:(服务,实用技巧,Linux操作系统)