How to Change Port Number in Apache

Apache is a popular web server used by millions of websites, blogs and organizations. It offers many features and supports high traffic server loads. By default, it runs on port 80 for HTTP and port 443 for HTTPS. But sometimes, you may need to run Apache server on different ports. In this article, we will learn how to change port number in Apache server.

Why Change Port Number in Apache

There are several reasons why you may need to change port number in Apache server.

1. Multiple Services – Apache server is commonly used along with NGINX server. In this configuration, NGINX is used as reverse proxy and Apache is used as web server. So NGINX needs to run on port 80/443. Therefore, you need to change Apache port number.
2. Better Security – Apache server is commonly used to host WordPress websites. There are millions of WordPress sites all over the world, and they are commonly targeted by hackers and bots. They know that most sites and blogs run on port 80 so it is a common target for online attacks. By changing Apache port number, you can make it difficult for automated bots and scripts to hack your site.
3. Testing Purposes – If you need to run Apache server in development or staging environment, then you may need to run it on a different port. This is because in most cases, the production server will be running on ports 80 and 443.
4. System Integration – You may already have certain software or processes sending data to a specific port, and need Apache server to listen to that port only. So you will need to run Apache on a different server.
5. Firewall Rules – Since ports 80 and 443 are well-known targets for online attacks, many organizations block them by default. In such cases, you need to change port number in Apache server.

How to Change Port Number in Apache

Now let us look at the steps to change port number in Apache server.

1. Locate Configuration File

We need to modify Apache configuration file for our purpose. Depending on your operating system and type of Apache installation, the location of its configuration file may vary.

  1. Windows – If you have installed Apache server on Windows system, then you will find config file at C:\Program Files\Apache Software Foundation\Apache2.4\conf\httpd.conf.
  2. XAMPP/WAMP – If you run Apache using other Windows software like XAMPP/WAMP then you will find config file located at C:\xampp\apache\conf\httpd.conf.
  3. Ubuntu/Debian – Config file is present at /etc/apache2/ports.conf
  4. RHEL/CentOS – In this case, the config file is located at /etc/httpd/conf/httpd.conf

2. Modify Listen Directive

Open configuration file using text editor such vi, nano, notepad, etc. Look for the Listen directive followed by port number. It should look like.

Listen 80 # for HTTP
OR
Listen 443 # for HTTPS

The above directive instructs Apache server to listen to port 80 and 443 respectively. Change the port numbers as per your requirement. Here is an example to listen to port 80 for HTTP and 8443 for HTTPS.

Listen 8080 # for HTTP
OR
Listen 8443 # for HTTPS

Save and close the file.

3. (Optional) Update ServerName & VirtualHost

In some cases, the ServerName directive in Apache configuration not only mentions the IP address but also the port number, like ServerName 127.0.0.1:80. In this case, you need to change the port number to ServerName 127.0.0.1:8080.

Similarly, if you have port number mentioned in VirtualHost tags of your virtual host config file, then you need to change that too. For example, if you have <VirtualHost 127.0.0.1:80>, change it to <VirtualHost 127.0.0.1:8080>.

Save and close the file.

4. Restart Server

Restart Apache Server to apply changes.

sudo systemctl restart apache2 # For Ubuntu/Debian
sudo systemctl restart httpd # For CentOS/RHEL

5. Update Firewall

Ports 80 and 443 may be open on your system’s firewall, when Apache was running on these ports. Now that it is running on a different port, you need to block these ports and open the new ports 8080 and 8443.

6. Test New Configuration

Open web browser and visit your website with new port number. For example, if your website is hosted at 54.43.32.21 then visit http://54.43.32.21:8080 to see if it works.

Conclusion

In this article, we have learnt how to run Apache server on different port. You can use these steps as per your operating system. If you run a high-traffic website on Apache server, then it is better to change port on Apache. It will automatically protect your site from most of automated bots that target port 80 and 443.

Also read:

How to Password Protect Directory in Apache
How to Speed Up Apache Web Server
Apache Deny Access to URL, Files & Directory

Leave a Reply

Your email address will not be published. Required fields are marked *