Try to run the following two commands:
sudo fuser -k 80/tcp
sudo fuser -k 443/tcp
Then execute
sudo service nginx restart
If that worked, your hosting provider might be installing Apache on your server by default during a fresh install, so keep reading for a more permenant fix. If that didn't work, keep reading to identify the issue.
Run nginx -t
and if it doesn't return anything, I would verify Nginx error log. By default, it should be located in /var/log/nginx/error.log
.
You can open it with any text editor:
sudo nano /var/log/nginx/error.log
Can you find something suspicious there?
The second log you can check is the following
sudo nano /var/log/syslog
When I had this issue, it was because my hosting provider was automatically installing Apache during a clean install. It was blocking port 80.
When I executed sudo nano /var/log/nginx/error.log
I got the following as the error log:
2018/08/04 06:17:33 [emerg] 634#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/08/04 06:17:33 [emerg] 634#0: bind() to [::]:80 failed (98: Address already in use)
2018/08/04 06:17:33 [emerg] 634#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
What the above error is telling is that it was not able to bind nginx to port 80 because it was already in use.
To fix this, you need to run the following:
yum install net-tools
sudo netstat -tulpn
When you execute the above you will get something like the following:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1762/httpd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1224/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1528/sendmail:acce
tcp6 0 0 :::22 :::* LISTEN 1224/sshd
You can see that port 80 is blocked by httpd (Apache). This could also be port 443 if you are using SSL.
Get the PID of the process that uses port 80 or 443. And send the kill command changing the <PID>
value:
sudo kill -2 <PID>
Note in my example the PID value of Apache was 1762
so I would execute sudo kill -2 1762
Aternatively you can execute the following:
sudo fuser -k 80/tcp
sudo fuser -k 443/tcp
Now that port 80 or 443 is clear, you can start Nginx by running the following:
sudo service nginx restart
It is also advisable to remove whatever was previously blocking port 80 & 443. This will avoid any conflict in the future. Since Apache (httpd) was blocking my ports I removed it by running the following:
yum remove httpd httpd-devel httpd-manual httpd-tools mod_auth_kerb mod_auth_mysql mod_auth_pgsql mod_authz_ldap mod_dav_svn mod_dnssd mod_nss mod_perl mod_revocator mod_ssl mod_wsgi
Hope this helps.
systemctl status nginx.service
andjournalctl -xe
? – Thomas Sablik