How to Change Apache HTTP Port in Linux

Apache Web Server is a free and open-source cross-platform web server application to deliver content through the internet.

Apache has various functionality known as modules to increase the features of Apache without the need for a restart.

In Apache, the default non-secure HTTP connection uses port 80, and the TLS configuration serves the data over port 443.

Today, you will learn how to change the default Apache HTTP port to your custom port in a few steps.

Prerequisites

  • Apache Web Server on your system
  • Web Browser to check the result (Chrome, Firefox, etc.)
  • Your 2-minute time

How to Install Apache in Linux (Skip if it exists)

Due to a very common web server, Apache is already included in the most popular Linux distribution repository.

To install Apache in Linux, ensure a proper internet connection with a terminal open on the screen and execute the below command.

Note: Installation requires system changes, having a root user or sudo account should be a must to gain the privileges.

$ sudo apt install apache2  [On Debian/Ubuntu]
$ sudo dnf install httpd    [On CentOS/Fedora]

After the installation process is complete, start the server daemon process using the below command.

$ sudo systemctl start apache2   [On Debian/Ubuntu]
$ sudo systemctl start httpd     [On CentOS/Fedora]

As you have installed Apache in your respective Linux system, you can jump to the next step to continue changing the default Apache HTTP port in Linux.

Modifying the Configuration Files

Changing the default Apache HTTP port requires modifications in the configuration files. This configuration file location might differ from distribution to distribution.

For Debian/Ubuntu distributions, the Apache Web Server configuration file required to be modified is located at /etc/apache2/ports.conf

For CentOS/Fedora distributions, the Apache Web Server configuration file required to be modified is located at /etc/httpd/conf/httpd.conf

When you change your port number in Apache for Debian/Ubuntu-based distributions, you need to also change the port number in the virtual host configuration file located at /etc/apache2/sites-enabled/000-default.conf

Changes in RHEL-based distributions such as Fedora and CentOS are directly reflected in the virtual host.

Change Apache HTTP Port in Linux

Before starting the process of changing the default port, stop your currently running server using the below command.

$ sudo systemctl stop apache2  [On Debian/Ubuntu]
$ sudo systemctl stop httpd    [On CentOS/Fedora]

Verify the process is stopped using the below command.

$ systemctl status apache2  [On Debian/Ubuntu]
$ systemctl status httpd    [On CentOS/Fedora]

To change the default port (80) for HTTP, modify the below Apache configuration file depending on the type of distribution you were using, using the text editor (nano, vim).

$ sudo nano /etc/apache2/ports.conf     [On Debian/Ubuntu]
$ sudo nano /etc/httpd/conf/httpd.conf  [On CentOS/Fedora]

Below is the output of the above ports.conf and httpd.conf configuration files.

Once the configuration file is opened, find the Listen 80 string within the file and replace 80 with something else. For me, it’s 88 port, as shown below.

On Debian/Ubuntu (skip RHEL, CentOS, and Fedora users)

When you change your port number in Apache for Debian/Ubuntu-based distributions, you need to also change the port number in the virtual host configuration file located at /etc/apache2/sites-enabled/000-default.conf using the below command.

$ sudo nano /etc/apache2/sites-enabled/000-default.conf  [On Debian/Ubuntu]

Below is the output of the above command.

virtual host configuration for debian-based distributions
virtual host configuration for Debian-based distributions

After you open the file, you will find the below line.

<VirtualHost *:80>

Change it to your custom port, same as used in ports.conf. For me, it’s 88 port.

<VirtualHost *:88>

Below is the output of the above configuration file after the changes.

modified virtual host configuration for debian-based distributions
modified virtual host configuration for Debian-based distributions

After the configuration is saved with the modification, start or restart the Apache server using the below command.

$ sudo systemctl restart apache2  [On Debian/Ubuntu]
$ sudo systemctl restart httpd    [On CentOS/Fedora]

Now Apache is bound to the new port 88. You check your local network socket table using the netstat command to find the Apache port as listed below.

$ sudo netstat -tlpn | grep apache

Below is the output of the above command.

check local network socket table using the netstat command
Check the local network socket table using the netstat command

Finally, open your Web browser (Chrome, Firefox, etc.) and enter http://localhost:88.

That’s it to change Apache HTTP Port in Linux.

Leave a Reply