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.
Table of Contents
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.
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.
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.
Finally, open your Web browser (Chrome, Firefox, etc.) and enter http://localhost:88
.
That’s it to change Apache HTTP Port in Linux.
If you want to change the port in MySQL, then do check out this How to change MySQL port in Ubuntu Linux article.
Innovative tech mind with 12 years of experience working as a computer programmer, web developer, and security researcher. Capable of working with a variety of technology and software solutions, and managing databases.