今天想要启动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.
我想着是不是我的防火墙没关,但是我现在并不需要去访问这台主机,所以它应该是可以启动的啊。getenforce也是Disabled。
重复安装nginx也会导致启动不了nginx服务,但报错信息不是如此的简洁的一句话。或者是配置文件马虎的少了个分号或是就是写错了,但是我是刚安装的nginx且没有更改配置文件!
根据提示,我习惯性的先查了下日志:
# journalctl -xe Jun 19 14:41:35 server.com nginx[6568]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)Jun 19 14:41:36 server.com nginx[6568]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)Jun 19 14:41:36 server.com nginx[6568]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)Jun 19 14:41:36 server.com polkitd[828]: Unregistered Authentication Agent for unix-process:6556:1917557 (system bus name :1.177, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, Jun 19 14:41:36 server.com systemd[1]: nginx.service: control process exited, code=exited status=1Jun 19 14:41:36 server.com nginx[6568]: nginx: [emerg] still could not bind()Jun 19 14:41:36 server.com 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.
真的是老长一串了,但是仔细看看里面有几行不一样的语句:
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
貌似明白了什么。再看一下状态:
# systemctl status nginx.service ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Tue 2018-06-19 15:10:38 CST; 13min ago Process: 7544 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE) Process: 7542 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 7539 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Jun 19 15:10:37 server.com nginx[7544]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) Jun 19 15:10:37 server.com nginx[7544]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Jun 19 15:10:37 server.com nginx[7544]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) Jun 19 15:10:38 server.com nginx[7544]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Jun 19 15:10:38 server.com nginx[7544]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) Jun 19 15:10:38 server.com systemd[1]: nginx.service: control process exited, code=exited status=1 Jun 19 15:10:38 server.com nginx[7544]: nginx: [emerg] still could not bind() Jun 19 15:10:38 server.com systemd[1]: Failed to start The nginx HTTP and reverse proxy server. Jun 19 15:10:38 server.com systemd[1]: Unit nginx.service entered failed state. Jun 19 15:10:38 server.com systemd[1]: nginx.service failed.
发现也出现了相同的话,再执行/usr/sbin/nginx,就更明显了
# /usr/sbin/nginx nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] still could not bind()
查看一下端口进程
# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 766/redis-server 0.
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1192/sshd
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 2605/sshd: root@pts
tcp6 0 0 :::80 :::* LISTEN 1363/httpd
tcp6 0 0 :::22 :::* LISTEN 1192/sshd
好吧,我的80端口被占用了,nginx用的就是80端口。怪不得起不起来呢。
所以如果我想要启动nginx服务有两种方法:
第一种方法:
停止占用我80端口的服务,执行systemctl stop httpd命令。再执行systemctl start nginx.service就成功了。
# systemctl stop httpd.service # systemctl restart nginx.service # systemctl status nginx.service ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Tue 2018-06-19 15:29:19 CST; 19s ago Process: 7825 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 7822 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 7820 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 7828 (nginx) CGroup: /system.slice/nginx.service ├─7828 nginx: master process /usr/sbin/nginx └─7829 nginx: worker process
第二种方法:
既然80端口被占用,你又不想停止httpd服务,那就更改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配置文件,找到server模块里的listen,将数字80改为其它的65535之内的数字(我改的是8080,比较好记还没啥后缀问题)。然后再执行systemctl start nginx,就可以启动起来了。