How to run Nginx on a different port other than 80 - nginx

I cannot run Nginx because port 80 is already listening on a docker-proxy service.
tcp6 0 0 :::80 :::* LISTEN 13110/docker-proxy
I would like to run Nginx on port 8800 instead of the default port 80.
As such, I've edited the default file as below;
sudo nano /etc/nginx/sites-available/default
listen 8800 default_server;
listen [::]:8800 default_server;
listen localhost;
However, I still cannot get it working as expected even after restart.
What am I doing wrong and how can I resolve it?
Below is the error I get;
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2019-04-26 04:23:14 EDT; 13min ago
Docs: man:nginx(8)
Process: 16955 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Process: 16944 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 80941 (code=exited, status=0/SUCCESS)
Apr 26 04:23:11 ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 26 04:23:11 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:12 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:12 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:13 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:13 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:14 ubuntu nginx[16955]: nginx: [emerg] still could not bind()
Apr 26 04:23:14 ubuntu systemd[1]: nginx.service: Control process exited, code=exited status=1
Apr 26 04:23:14 ubuntu systemd[1]: nginx.service: Failed with result 'exit-code'.
Apr 26 04:23:14 ubuntu systemd[1]: Failed to start A high performance web server and a reverse proxy server.

You have to go to the /etc/nginx/sites-enabled/default
Edit that file and put (If you are willing to put "8800" as your port for nginx)
server { listen 8800; }
The start the server
sudo service nginx start
or
sudo service nginx restart if previously running.
Then access localhost:8800
Add a rule to iptables
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8800 -j ACCEPT
Restart IPtables
sudo service iptables restart;

It looks like the problem is in the line
listen localhost;
The "listen" directive can take a port number, a host,
or both. I believe in your case this line defaults
the port to 80, which would cause the problems you
are experiencing.
Also, beware if "default" is not the only file
in hosts_available. Each of these (each server block) will default
to using port 80 unless overridden there. Just
editing "default" would not be enough in that case.

I found this answer on Server Fault helpful: https://serverfault.com/questions/655067/is-it-possible-to-make-nginx-listen-to-different-ports
You can add ports to the one server block to keep things simple:
server {
listen 80;
listen 8000;
server_name example.org;
root /var/www/;
}

Related

nginx - Failed to restart process

I've got no errors in my config, as tested with nginx -t
root#proxy1:/var/www# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
However when I try running systemctl restart nginx, I get the following error.
root#proxy1:/var/www# 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.
Running systemctl status nginx.service returns
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2022-10-09 19:43:09 CEST; 48s ago
Docs: man:nginx(8)
Process: 1439643 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 1439658 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Oct 09 19:43:08 SERVERHOSTNAME.net nginx[1439658]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Oct 09 19:43:09 SERVERHOSTNAME.net nginx[1439658]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
Oct 09 19:43:09 SERVERHOSTNAME.net nginx[1439658]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 09 19:43:09 SERVERHOSTNAME.net nginx[1439658]: nginx: [emerg] bind() to 0.0.0.0:3582 failed (98: Address already in use)
Oct 09 19:43:09 SERVERHOSTNAME.net nginx[1439658]: nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
Oct 09 19:43:09 SERVERHOSTNAME.net nginx[1439658]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Oct 09 19:43:09 SERVERHOSTNAME.net nginx[1439658]: nginx: [emerg] still could not bind()
Oct 09 19:43:09 SERVERHOSTNAME.net systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
Oct 09 19:43:09 SERVERHOSTNAME.net systemd[1]: nginx.service: Failed with result 'exit-code'.
Oct 09 19:43:09 SERVERHOSTNAME.net systemd[1]: Failed to start A high performance web server and a reverse proxy server.
However all my sites running on nginx are functional.
I also get problems related to this while running certbot.
root#proxy1:/var/www# certbot
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
...
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for MYDOMAIN.net
nginx: [error] invalid PID number "" in "/run/nginx.pid"
Cleaning up challenges
nginx: [error] invalid PID number "" in "/run/nginx.pid"
nginx restart failed:
b''
b''
I'm not sure if it's a bug of Nginx, but you may try this (worked for me):
pkill -9 nginx
nginx -c /etc/nginx/nginx.conf
nginx -s reload
Source

Change port nginx to 81 but nginx still bind in port 80

I'm using nginx and haproxy, i want haproxy bind to port 80 so I'm change nginx to port 81. But when i change port nginx to 81 then start nginx still bind in port 80 and got error. What solution for me.
Here my nginx config default in /etc/nginx/sites-enabled
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 81;
listen [::]:81;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
And my haproxy config
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
stats timeout 30s
user haproxy
group haproxy
daemon
# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
# Default ciphers to use on SSL-enabled listening sockets.
# For more information, see ciphers(1SSL). This list is from:
# https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
# An alternative list with additional directives can be obtained from
# https://mozilla.github.io/server-side-tls/ssl-config-generator/?server=haproxy
ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS
ssl-default-bind-options no-sslv3
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
frontend myfrontend
bind 127.0.0.1:80
rate-limit sessions 50
http-response set-header Cache-Control no-cache,\ max-age=600 if { capture.req.uri -m beg /api/ }
capture request header Host len 32
acl shipping-report.local hdr(host) -i shipping-report.local www.shipping-report.local/report
use_backend report_shipping if shipping-report.local
backend report_shipping
server report01 shipping-report.local:8000
and I got error
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Wed 2022-08-31 16:04:03 +07; 1min 0s ago
Docs: man:nginx(8)
Process: 30507 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=1/FAILURE)
Process: 26443 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
Process: 31737 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Process: 31721 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 30483 (code=exited, status=0/SUCCESS)
Thg 8 31 16:04:01 falcon-anhnkd systemd[1]: Starting A high performance web server and a reverse proxy server...
Thg 8 31 16:04:01 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Thg 8 31 16:04:01 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Thg 8 31 16:04:02 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Thg 8 31 16:04:02 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Thg 8 31 16:04:03 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Thg 8 31 16:04:03 falcon-anhnkd nginx[31737]: nginx: [emerg] still could not bind()
Thg 8 31 16:04:03 falcon-anhnkd systemd[1]: nginx.service: Control process exited, code=exited status=1
Thg 8 31 16:04:03 falcon-anhnkd systemd[1]: nginx.service: Failed with result 'exit-code'.
Thg 8 31 16:04:03 falcon-anhnkd systemd[1]: Failed to start A high performance web server and a reverse proxy server.
What solution for me I want to use both nginx and haproxy

nginx service failed (Result: exit-code) Job for nginx.service failed

I've been trying to apply an SSL to my droplet, but the nginx gives me an error when I try to execute the certbot.
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2018-10-19 13:50:49 UTC; 39s ago
Docs: man:nginx(8)
Process: 18860 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Process: 18850 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Oct 19 13:50:47 All-line nginx[18860]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 19 13:50:47 All-line nginx[18860]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Oct 19 13:50:48 All-line nginx[18860]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 19 13:50:48 All-line nginx[18860]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Oct 19 13:50:48 All-line nginx[18860]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 19 13:50:48 All-line nginx[18860]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Oct 19 13:50:49 All-line nginx[18860]: nginx: [emerg] still could not bind()
Oct 19 13:50:49 All-line systemd[1]: nginx.service: Control process exited, code=exited status=1
Oct 19 13:50:49 All-line systemd[1]: nginx.service: Failed with result 'exit-code'.
Oct 19 13:50:49 All-line systemd[1]: Failed to start A high performance web server and a reverse proxy server.
If You use certbot, this helped me. Updating certbot and updating of letsencrypt config files.

Nginx does not work on a domain name but OK on an IP address?

Why do I get this page of This site can’t be reached but it seems no errors on my terminal?
When I access the server via IP address it is fine:
But with a domain name, I get this:
$ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2017-04-06 04:19:23 BST; 7h ago
Process: 7248 ExecStart=/usr/bin/nginx -g pid /run/nginx.pid; error_log stderr; (code=exited, status=0/SUCCESS)
Main PID: 7250 (nginx)
Tasks: 2 (limit: 512)
CGroup: /system.slice/nginx.service
├─7250 nginx: master process /usr/bin/nginx -g pid /run/nginx.pid; error_log stderr
└─7251 nginx: worker process
Apr 06 04:19:23 localhost systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 06 04:19:23 localhost systemd[1]: Started A high performance web server and a reverse proxy server.
Apr 06 04:20:00 localhost nginx[7248]: 2017/04/06 04:20:00 [error] 7251#7251: *6 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 183.171
Apr 06 07:14:44 localhost nginx[7248]: 2017/04/06 07:14:44 [error] 7251#7251: *506 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 195.2
Apr 06 08:27:06 localhost nginx[7248]: 2017/04/06 08:27:06 [error] 7251#7251: *574 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 138.2
Apr 06 11:04:58 localhost nginx[7248]: 2017/04/06 11:04:58 [error] 7251#7251: *835 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 183.1
Apr 06 11:17:27 localhost nginx[7248]: 2017/04/06 11:17:27 [error] 7251#7251: *841 open() "/usr/share/nginx/html/robots.txt" failed (2: No such file or directory), client: 66.249
Apr 06 11:31:40 localhost nginx[7248]: 2017/04/06 11:31:40 [error] 7251#7251: *845 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 183.1
Apr 06 11:32:56 localhost
This seems to be the error:
*6 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 183.171
What does this mean? It is not related to the domain name!
This is part of my config:
server {
listen 80;
server_name example.co.uk www.example.co.uk;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
server {
listen 443 ssl;
server_name example.co.uk www.example.co.uk;
ssl_certificate /etc/letsencrypt/live/example.co.uk/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.co.uk/privkey.pem;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location ~ /.well-known {
allow all;
}
}
server {
listen 8888 ssl;
server_name example.co.uk www.example.co.uk;
ssl_certificate /etc/letsencrypt/live/example.co.uk/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.co.uk/privkey.pem;
location ~ /.well-known {
allow all;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:3838;
}
}
Test the code:
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
It was all working fine before but now suddenly stopped.
Any ideas?
I follow this guide to install Nginx btw on my Arch Linux.
The steps I take:
$ sudo pacman -Syu
$ sudo pacman -S nginx
Start Nginx service and enable it to start at boot using commands:
$ sudo systemctl start nginx
$ sudo systemctl enable nginx
EDIT:
I now have uninstalled Ngnix and reinstalled it, but it is not working anymore even on a fresh start without any config to the /etc/nginx/nginx.conf. What I get is This site can’t be reached' on browsers.
Why!??
I checked with netstat and I can see that the port 80 is in the list:
$ sudo netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 5361/mongod
tcp 0 0 0.0.0.0:5355 0.0.0.0:* LISTEN 3666/systemd-resolv
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10701/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3667/sshd
tcp 0 0 0.0.0.0:3838 0.0.0.0:* LISTEN 32434/shiny-server
tcp6 0 0 :::5355 :::* LISTEN 3666/systemd-resolv
tcp6 0 0 :::3030 :::* LISTEN 17325/node /usr/loc
tcp6 0 0 :::22 :::* LISTEN 3667/sshd
tcp6 0 0 :::3000 :::* LISTEN 17325/node /usr/loc
udp 0 0 0.0.0.0:5355 0.0.0.0:* 3666/systemd-resolv
udp6 0 0 :::5355 :::* 3666/systemd-resolv
EDIT 2:
I created an access log. But I don't see any major errors there:
$ more -f /etc/nginx/logs/access.log
183.171.92.177 - - [06/Apr/2017:12:38:09 +0100] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36" "-"
183.171.92.177 - - [06/Apr/2017:12:38:09 +0100] "GET /favicon.ico HTTP/1.1" 404 571 "http://xxx.xxx.xxx.xx/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36" "-"
Find what is going on from the Nginx access logs! Before checking it, ensure that you are logging enough. You can use the following log_format.
log_format main '$host - $remote_addr - $remote_user - [$time_local] - $document_root - $document_uri - $request - $status - $body_bytes_sent - $http_referer';
Restart your Nginx service and make a few requests. You need to find out if the request is even reaching the server. Access logs should give you a clear indication of what's going on.
Since you don't have a dns server in place, to send a request to your server with the specified domain name, you need to add an entry in the /etc/hosts (this path depends on your local machine system) file of the client machine.
You need to add the following line in that file,like so:
sudo nano /private/etc/hosts
...and:
# The client /etc/hosts file
<ip-of-your-server> www.yourdomainname.com

Nginx will not start (Address already in use)

I have a problem with nginx. I tried different solutions, but for me nothing work.
That is my error:
4 root#BANANAS ~ # sudo service nginx restart :(
Restarting nginx: nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
nginx: [emerg] still could not bind()
nginx.
Can you help me?
Probably other process is using specified port:
sudo netstat -tulpn
Get the PID of the process that already using 443. And send signal with kill command.
sudo kill -2 <PID>
sudo service nginx restart
Aternatively you can do:
sudo fuser -k 443/tcp
Make sure you dont use old syntax:
server {
listen :80;
listen [::]:80;
}
The above syntax will cause
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Correct syntax:
server {
listen 80;
listen [::]:80 ipv6only=on;
}
or
server {
listen [::]:80;
}
Both above syntax will achieve the same thing, listening on both ipv4 and ipv6.
Another way (from my experience) is just force quit the process that running on that port 443
sudo fuser -k 443/tcp
or if you running on port 80 just change the port to
sudo fuser -k 80/tcp
Hope it helps someone who had the same issue
Alternative using lsof:
Find the PID & kill the process which running on port 443
sudo kill -9 $(lsof -t -i :443)
Thank you for the answer.
After running
sudo netstat -tulpn
I realised that I had apache2 running on port 80. This was probably done after I used Certbot to install SSL on the server.
I removed Apache2 and the server was up and running.
apt remove apache2
This did the trick! Thank you again.
First, we have to check how many services run on port 80. To check that, you could run the following command:
sudo netstat -plant | grep 80
This would show you which service exactly is listening on port 80 and then you can make a decision whether you want to have that service as is or have Nginx instead.
If it is Apache, you will need to decide whether you want to use Apache or Nginx.
If you only want to have Nginx, you need to stop Apache first:
sudo systemctl stop apache2 && sudo systemctl start nginx
I received the above error due to accidentally repeating the listen directive twice within the same server block as follows:
server {
listen [::]:443 ssl ipv6only=off;
listen 443 ssl;
...
}
changing it to:
server {
listen 443 ssl;
...
}
or
server {
listen [::]:443 ssl ipv6only=off;
...
}
resolved the problem when restarting the server: sudo service nginx start
When I killed the nginx process bind to 80 & 443 ports, the process always reappeared with new PID.
It helped me to temporarily comment this line in /etc/nginx/nginx.conf, restart nginx and then uncomment the line back:
worker_processes auto;
In my case, running Ubuntu 20.04, I bounced the server, and Apache2 was set to automatically start.
$ sudo lsof -i -P -n | grep LISTEN
This showed Apache2 running on port 80, and was causing a conflict. I shut it down with
$ sudo systemctl stop Apache2
And then started nginx with
$ sudo systemctl start nginx
If using a virtual machine, you may just need to restart the virtual machine. I was having the same issues yesterday and I could not get Nginx to start using any of the methods in this post. So I started the virtual machine up this morning, checked the running processes, and I see Nginx running on port 80. I even viewed the status of Nginx yesterday with systemctl and it said failed, but today it is active. Not sure what happened here, but it could be worth a try.

Resources