How to change the HTTP listening port in Apache

If you find that port 80 is a security risk on your network, you can change the Apache listening port to something that is not standard.

How to change the HTTP listening port in Apache
If you find that port 80 is a security risk on your network, you can change the Apache listening port to something that is not standard.

As almost every IT administrator knows, the default web server port is 80. And that’s great. But the point is that hackers know that port 80 is the standard and can easily target it. What if you didn’t have to make the Apache web server vulnerable to port 80 attacks? Since you can easily change the configuration so that your virtual Apache hosts listen to non-standard ports, it makes perfect sense to anyone who has problems with port 80 attacks.

The only drawback is that when you change the default port, all browsers must point to that specific port, otherwise any virtual host that uses the non-default port will not be displayed.

This is also a good trick to set up if you have a single Apache server that hosts sites that require different ports for different services. For example, if you have a site for customers on port 80 and you want to offer a site for customers on port 8081. With Apache, that’s not only possible, it’s simple.

Let me show you how you can change the port for Apache. I will demonstrate it on Ubuntu Server 18.10, but will also provide instructions on how the process will run on Red Hat and Fedora type servers.

SEE: Choose your Windows 7 exit strategy: four options (TechRepublic Premium)

What you need

All you need is an active LAMP server and a user account with sudo rights. The “L” part of LAMP can be Ubuntu, SUSE, Red Hat, Debian, Fedora Server or almost any Linux distribution.

How to add the new port

The first thing we do is add it to the standard listening Apache port. To do this, log in to your Linux server and run one of the following commands:

  • For Ubuntu / Debian-based servers – sudo nano /etc/apache2/ports.conf
  • For Red Hat-based servers – sudo nano /etc/httpd/conf/httpd.conf

In the currently opened file (Figure A) add Listen 8081 under the Listen 80 line.

Figure A

The ports.conf file in Ubuntu Server.

Save and close the file.

How to configure the virtual host

Now we have to configure our virtual host to listen to the new port. I will demonstrate it with the default virtual host, but you can adjust this to any virtual host that you have added to Apache. Open the virtual host file with the command:

sudo nano /etc/apache/sites-enabled/000-default.conf

For Red Hat-based systems, the virtual host files can be found in /etc/httpd/conf.d/.

At the top of the file you can see the start of the guideline:

Change that rule to:

Save and close the file.

Restart Apache with the command:

sudo systemctl restart apache2

Or for Red Hat based systems:

sudo systemctl restart httpd

You should now be able to refer a browser to http: // SERVER_IP: 8081 (where SERVER_IP is the IP address of the hosting server) to see the Apache welcome site (Figure B), or your virtual host’s welcome page.

Figure B

Extra work with Red Hat

If your distribution of choice is based on Red Hat, you may need to do some extra work. To link Apache to the new port, you must first install the policycoreutils application. Install the software with the command:

sudo yum install policycoreutils

After the software is installed, add the new SELinux rules for port 8081 with the following commands:

sudo semanage port -a -t http_port_t -p tcp 8081
sudo semanage port -m -t http_port_t -p tcp 8081

After you have completed the above commands, restart apache with the command:

sudo systemctl restart httpd

You should now be able to see your virtual host, via port 8081, which is served by your Red Hat-based Apache server.

And that is all there is to display websites on a non-standard port. Of course you do not have to use port 8081. In fact, you can use virtually any port you need – as long as it does not conflict with a port used by another service.

Open Source Weekly Newsletter

You do not want to miss our tips, tutorials and comments about the Linux operating system and open source applications.
Delivered Tuesday

Register today

Also see

Image: Apache

Follow AsumeTech on

More From Category

More Stories Today

Leave a Reply