Atlantic.Net Blog

How to Change Nginx and Apache Port in Linux

Apache is a free, open-source, widely used web server known for its stability, flexibility, and numerous features.

Nginx is an open-source, stable, and most popular web server. It is used as a load-balancer, web reverse proxy, or POP and IMAP proxy server.

In this post, we will show you how to change the Nginx and Apache default ports in Linux.

In This Article

Change Apache Default Port

By default, the Apache webserver listens to all incoming connections on port 80. If you have installed an SSL certificate, it will listen to all secure connections on port 443. In this section, we will show you how to change the Apache default port on CentOS, RHEL, Debian, and Ubuntu operating systems.

Change Apache Default Port on Debian/Ubuntu

On the Debian and Ubuntu operating systems, Apache stores port-related information in the /etc/apache2/ports.conf and /etc/apache2/sites-enabled/000-default.conf files.

First, edit /etc/apache2/ports.conf file:

nano /etc/apache2/ports.conf

Find the following line:

Listen 80

And replace it with the following line:

Listen 8080

Save and close the file when you are finished, then edit the Apache default virtual host configuration file /etc/apache2/sites-enabled/000-default.conf:

nano /etc/apache2/sites-enabled/000-default.conf

Find the following line:

<VirtualHost *:80>

And replace it with the next line:

<VirtualHost *:8080>

Save and close the file, then restart the Apache service to apply the configuration changes:

systemctl restart apache2

You can now verify the Apache listening port with the following command:

ss -antpl | grep 8080

You should get the following output:

LISTEN    0         511                      *:8080                   *:*        users:(("apache2",pid=2908,fd=4),("apache2",pid=2907,fd=4),("apache2",pid=2906,fd=4))

Change Apache Default Port on CentOS/RHEL

On the CentOS and RHEL operating systems, Apache stores port-related information in /etc/httpd/conf/httpd.conf file.

You can edit it with the following command:

nano /etc/httpd/conf/httpd.conf

Find the following line:

Listen 80

And replace it with the following line:

Listen 8080

Save and close the file, then restart the Apache service to apply the changes:

systemctl restart httpd

You can now verify the Apache listening port with the following command:

ss -antpl | grep 8080

Output:

LISTEN 0      128                *:8080             *:*    users:(("httpd",pid=5994,fd=4),("httpd",pid=5993,fd=4),("httpd",pid=5992,fd=4),("httpd",pid=5989,fd=4))

Change Nginx Default Port

By default, the Nginx web server listens to all incoming connections on port 80. If you have installed an SSL certificate, it will listen to all secure connections on port 443. In this section, we will show you how to change the Nginx default port on CentOS, RHEL, Debian, and Ubuntu operating systems.

Change Nginx Default Port on Debian/Ubuntu

On Ubuntu and Debian-based distributions, Nginx stores port-related configuration in the /etc/nginx/sites-enabled/default file.

You can edit it with the following command:

nano /etc/nginx/sites-enabled/default

Find the following lines:

        listen 80 default_server;
        listen [::]:80 default_server;

And replace them with the following lines:

        listen 8080 default_server;
        listen [::]:8080 default_server;

Save and close the file, then restart the Nginx service to apply the changes:

systemctl restart nginx

You can now verify the Nginx listening port with the following command:

ss -antpl | grep 8080

You should get the following output:

LISTEN    0         511                0.0.0.0:8080             0.0.0.0:*        users:(("nginx",pid=1596,fd=6),("nginx",pid=1595,fd=6))                        
LISTEN    0         511                   [::]:8080                [::]:*        users:(("nginx",pid=1596,fd=7),("nginx",pid=1595,fd=7))                        

Change Nginx Default Port on CentOS/RHEL

On CentOS and RHEL operating systems, Nginx stores port-related configuration in /etc/nginx/nginx.conf file.

nano /etc/nginx/nginx.conf

Find the following lines:

        listen       80 default_server;
        listen       [::]:80 default_server;

And replace them with the following lines:

        listen       8080 default_server;
        listen       [::]:8080 default_server;

Save and close the file, then restart the Nginx service to apply the changes:

systemctl restart nginx

You can now verify the Nginx listening port with the following command:

ss -antpl | grep 8080

You should see the following output:

LISTEN 0      128          0.0.0.0:8080       0.0.0.0:*    users:(("nginx",pid=6589,fd=8),("nginx",pid=6588,fd=8))
LISTEN 0      128             [::]:8080          [::]:*    users:(("nginx",pid=6589,fd=9),("nginx",pid=6588,fd=9))

Conclusion

In the above guide, you learned how to change the Apache and Nginx default ports on Debian, Ubuntu, CentOS, and RHEL operating systems. You can now set your desired port on Apache and Nginx web servers; try it on your dedicated server from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year