I got a problem which I have been trying to fix for a few days now and I don't know what to do, have been looking for answers but all of those I found didn't help me.
I am kinda new here and I really hope that someone can help me. You can tell me which informations I need to give in hope for finding a solution.
$ systemctl status nginx.service
nginx.service - Startup script for nginx service
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Tue 2016-03-08 13:23:35 GMT; 2min 20s ago
Mar 08 13:23:33 startdedicated.com nginx[8315]: nginx: [emerg] bind() to ------------ f...e)
Mar 08 13:23:33 startdedicated.com nginx[8315]: nginx: [emerg] bind() to ----- f...e)
Mar 08 13:23:34 startdedicated.com nginx[8315]: nginx: [emerg] bind() to ----- f...e)
Mar 08 13:23:34 startdedicated.com nginx[8315]: nginx: [emerg] bind() to ----- f...e)
Mar 08 13:23:35 startdedicated.com nginx[8315]: nginx: [emerg] bind() to ----- f...e)
Mar 08 13:23:35 .startdedicated.com nginx[8315]: nginx: [emerg] still could not bind()
Mar 08 13:23:35 startdedicated.com systemd[1]: nginx.service: control process exited, code=...=1
Mar 08 13:23:35 startdedicated.com systemd[1]: Failed to start Startup script for nginx service.
Mar 08 13:23:35 startdedicated.com systemd[1]: Unit nginx.service entered failed state.
Mar 08 13:23:35 startdedicated.com systemd[1]: nginx.service failed.
I'm using RHEL 7.4 with NGINX 1.13.8 and if I do the same with sudo, it works Ok:
Just make sure whoever wants to use
nginx.service
has execute permissions to it.In my case, it's because of apache server is running somehow. So I stop apache then restart nginx. Work like a charm!
The cause of the issue is this, I already had
Apache
web server installed and actively listening on port80
on my local machine.Apache
andNginx
are the two major open-source high-performance web servers capable of handling diverse workloads to satisfy the needs of modern web demands. However,Apache
serves primarily as a HTTP server whereasNginx
is a high-performance asynchronous web server and reverse proxy server.The inability of
Nginx
to start was becauseApache
was already listening on port 80 as its default port, which is also the default port forNginx
.One quick workaround would be to stop
Apache
server by running the command belowAnd then starting up
Nginx
server by running the command belowHowever, this same issue will arise again when we try to start
Apache
server again, since they both use port80
as their default port.Here's how I fixed it:
Run the command below to open the default configuration file of Nginx in Nano editor
When the file opens in Nano editor, scroll down and change the default server port to any port of your choice. For me, I chose to change it to port
85
Also, scroll down and change the virtual host port to any port of your choice. For me, I also chose to change it to port
85
Then save and exit the file by pressing on your keyboard:
You may still be prompted to press Y on your keyboard to save your changes.
Finally, confirm that your configuration is correct and restart the
Nginx
server:You can now navigate to
localhost:nginx-port
(localhost:85
) on your browser to confirm the changes.Displaying the default Nginx start page
If you want the default Nginx start page to show when you navigate to
localhost:nginx-port
(localhost:85
) on your browser, then follow these steps:Examine the directory
/var/www/html/
which is the defaultroot
directory for bothApache
andNginx
by listing its contents:You will 2 files listed in the directory:
Run the command below to open the default configuration file of Nginx in Nano editor:
Change the order of the index files in the root directory from this:
to this (putting the default Nginx start page -
index.nginx-debian.html
in the 2nd position immediately afterindex
):Then save and exit the file by pressing on your keyboard:
You may still be prompted to press Y on your keyboard to save your changes.
Finally, confirm that your configuration is correct and restart the
Nginx
server:You can now navigate to
localhost:nginx-port
(localhost:85
) on your browser to confirm the changes.That's all.
I hope this helps
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: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:
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 executesudo 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.