1. Overview

In web applications, the server often needs to listen on more than a single TCP port. This can give improved performance and better flexibility for the application.

In this tutorial, we’ll learn how to configure the Apache web server to listen on two different ports.

2. Modify Port Config File

The first step is to update the port configuration file /etc/apache2/ports.conf.

Let’s look at this file to see the default setup:


Listen 80

<IfModule ssl_module>

We can see the Listen 80 statement, which means that by default, the application listens on port 80.

To bind the second port to our application, we add a line Listen <port_number>. For example, for port 8888, we’ll need to add Listen 8888 line:


Listen 80 
Listen 8888 

<IfModule ssl_module> 

As we can see, our Apache web server now listens on both ports 80 and 8888.

3. Update Virtual Host File

The next step is to make sure that the Virtual Host file is set up to listen to the new port.

The Virtual Host file stores the virtual host configurations. This file allows the user to control web page settings for different hostnames and ports. In particular, it provides an option to use the web server for multiple hostnames and multiple ports.

For this tutorial, we only need to add the second port to this file.

The Virtual Host configuration is located in the file /etc/apache2/sites-enabled/000-default.conf. Let’s look at the file to see the default settings:

<VirtualHost *:80>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

At the top, there is a <VirtualHost *:80> statement, which means that our application uses a virtual host on port 80.

To make our page work for both ports 80 and 8888, we need to add *:8888 after port 80:

<VirtualHost *:80 *:8888> 

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

As a result, the Virtual Host file now contains both listened ports.

4. Reload the Apache Service

In the sections above, we updated the configuration files to make our server listens on the second port.

So now, we need to make the server apply these settings. For that, we should restart the Apache service:

$ sudo service apache2 restart

We may need to enter the password to be able to proceed because this command requires sudo access.

5. Verify Everything Works

Once the server is restarted, it’s time to check if everything works as expected. We should now be able to see that the Apache service is listening on the two ports.

For that, we can use the netstat command, which is a great tool for displaying network connection details:

$ sudo netstat -nltp | grep apache
tcp6       ...      ...       :::8888         ...        LISTEN      1907/apache2        
tcp6       ...      ...       :::80           ...        LISTEN      1907/apache2   

We ran netstat with the following options:

  • -l shows listening ports
  • -n displays the port numbers
  • -t filters for TCP ports only
  • -p shows the process names, which helps us identify the Apache process

The grep command is used to filter the results for the Apache ports only.

As we can see, Apache is listening on two ports. On the top line, we can see port 8888, while the bottom line is for port 80.

6. Conclusion

In this article, we’ve learned how to make the Apache server listen on two different ports.

For that, we firstly updated the ports.conf configuration file. Secondly, we learned how to modify the Virtual Host file. Then, we saw how to restart the Apache service.

And finally, we looked at the netstat command to check if our server listens on two ports as expected.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.