How to change Apache port in Ubuntu 16.04

Posted on July 22nd, 2019

The default HTTP port is 80, and the default HTTPS port is 443. It’s same whether you are using Apache or Nginx or any other web server available in the market. And, In most cases, you won’t update these ports because then you will have to pass the port number with the domain name to access your website. But, If you are only hosting API on your server, you might want to host it on a custom port to mitigate Bruteforce attacks. In this short tutorial, I will show you how to change the apache port in Ubuntu 16.04 systems.

It is a very easy task and you and you can perform it in minutes once you know how exactly Apache deals with the ports. Before we move on to actually update the ports, Let’s first learn how to select good port numbers.

Linux systems have thousands of ports available. And the first 1024 ports are reserved for the most common or authorized services. So, You should avoid port numbers lower than 1024. To check if the port number is in use by any other service running on the system, execute the following command.

$ grep PORT_NUMBER /etc/services

If some service is already using the PORT_NUMBER, you will get some output and you will not be able to use that port for Apache. If you do not get any output, The port number is good to use for Apache!

Change Apache Port in Ubuntu

We will continue with this guide considering that you already have Apache installed on your system. However, If Apache is not already installed, Execute the following set of commands to install Apache.

$ sudo apt-get update
$ sudo apt-get install apache2 -y

Once done, we can update the ports on which Apache will expect us to send requests. Apache has a dedicated configuration file to manage ports. The ports configuration file is located at /etc/apache2/ports.conf. It is a tiny configuration file with just a few lines of configuration. The default ports.conf file looks like the following.

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 80

<IfModule ssl_module>
	Listen 443
</IfModule>

<IfModule mod_gnutls.c>
	Listen 443
</IfModule>

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

The configuration file says Apache to listen on the port number 80 by default. And if the SSH or the gnutls modules are present, Apache will also listen on port number 443.

We can specify the new ports in this configuration file to make Apache listen on custom ports. In this example, Let’s say we want Apache to listen for HTTP requests on port number 8080 and HTTPS requests on 8443. Let’s update the configuration file as per our requirements.

To open the ports configuration file in the nano editor (The easiest one!), execute the following command.

$ sudo nano /etc/apache2/ports.conf

Now, replace Listen 80 with Listen 8080 and Listen 443 with Listen 8443 in the configuration file. The updated configuration file should look like the following.

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 8080

<IfModule ssl_module>
	Listen 8443
</IfModule>

<IfModule mod_gnutls.c>
	Listen 8443
</IfModule>

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

Once done, Save the updated configuration file by pressing CTRL+X followed by Y followed by the Enter key. Now, It’s time to update ports in our Apache Virtual Hosts.

If you have created extra virtual host configuration files on your server, Update the Virtual Host block to listen on the new port. A virtual host block starts with the line <VirtualHost *:80> or <VirtualHost *:443>.

Update <VirtualHost *:80> to <VirtualHost *:8080> and <VirtualHost *:443> to <VirtualHost *:8443> in all the virtual host files that exist on your server.

Once you have updated the ports in ports.conf file and all the virtual host files present on your server, restart the Apache service by executing the following command to apply the changes.

$ sudo service apache2 restart

If you do not face any errors while restarting the Apache daemon, Congratulations! The Apache ports are now updated and Apache will listen for HTTP and HTTPS requests on the new ports that we have specified in our ports.conf and Virtual Host files.

If you are following this guide practically, you will be able to access your site or Apache’s default page by appending :8080 at the end of the IP address or the domain name in the browser.

So, this is how you can update Apache ports in Ubuntu. If you are facing issues updating the ports, let us know in the comment section given below, we will help you solve the issue.

One Response to “How to change Apache port in Ubuntu 16.04”

  1. Martin Karari says:

    Good and precise instructions. Thanks

Leave a Reply