Atlantic.Net Blog

How to Change Nginx and Apache Port in Linux

Hitesh Jethva
by Atlantic.Net (273 posts) under Dedicated Server Hosting, Tutorials
0 Comments

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 on the internet. It is used as a load-balancer, web reverse proxy, or as a POP and IMAP proxy server.

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

Prerequisites

  • A fresh Debian, Ubuntu, or CentOS server on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Debian, Ubuntu, or CentOS as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in, run the following command to update your base system with the latest available packages.

apt-get update -y

Or

dnf update -y

Change Apache Default Port

By default, the Apache webserver listens to all incoming connections on port 80. If you have installed an SSL certificate, then 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 following 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, then 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 port 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 Free To Use Cloud VPS

Free Tier Includes:
G3.2GB Cloud VPS 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


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server 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


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources