Apache HTTP is one of the most used web servers on the Internet for its various features, among which we highlight flexibility, stability and many more..
By default, the Apache web server takes instructions to listen to the incoming connection and link to port 80 of the equipment. In case of using a TLS configuration, the server will listen to secure connections on port 443.
If the goal is for the Apache web server to link and listen to web traffic on other ports in addition to the standard web ports, we will need to add a new instruction where the new listening port will be included..
How to modify HTTP port of Apache server on Linux
Step 1
In distributions based on Debian or Ubuntu, the configuration file to be edited is the file /etc/apache2/ports.conf and in the distributions based on RHEL or CentOS we will edit the file /etc/httpd/conf/httpd.conf.
nano /etc/apache2/ports.conf (Debian / Ubuntu) nano /etc/httpd/conf/httpd.conf (RHEL / CentOS)
Step 2
For this case we will use Ubuntu 17. When accessing this file we will see the following:
Step 3
As we can see, the default value on the LISTEN line is port 80. For this tutorial, we will configure the Apache HTTP server to listen to the connections on port 8081 of the device, therefore, we will add the line "LISTEN 8081" just below port line 80:
Step 4
We save the changes using the following key combination:
+ O Ctrl + O
We leave the editor using:
+ X Ctrl + X
Step 4
After adding the indicated line, it will be necessary to create or modify an Apache virtual host in the Debian or Ubuntu-based distributions to start the linking process, which will be helpful for the vhost requirements.
In the case of CentOS or RHEL-based distributions, the change is applied directly to the default virtual host.
In this case, we will open and edit the 000-default.conf file and there we will change the port to 8081 as follows:
nano /etc/apache2/sites-enabled/000-default.conf
Step 5
We set port 8081 on the “VirtualHost†line, save the changes and exit the editor.
Finally, we will apply the changes and allow Apache to link to the new port, for this we will restart the daemon and verify the local network socket table using the netstat or ss command. The port 8081 to listen must be shown in the server network table as we have defined above, we will execute the following:
systemctl restart apache2 netstat -tlpn | grep apache ss -tlpn | grep apache
Step 6
Now, to verify that the connection is correct, we will access from some browser using the following syntax:
http: // IP_address: 8081
Step 7
In Linux distributions based on CentOS / RHEL, we will install the
policycoreutils package to add the SELinux rules required for Apache to link to the new port and restart the Apache HTTP server to apply the changes. We execute the following:
yum install policycoreutils
Step 8
There we enter the letter and to confirm the download and installation. Then, we will add the SELinux rules for port 8081:
week port -a -t http_port_t -p tcp 8081 week port -m -t http_port_t -p tcp 8081
Note
In case of presenting an error with week, we must install the following:
yum install policycoreutils-python
Step 9
Now, we proceed to reset the Apache service:
systemctl restart httpd.service
Step 10
Then, we will execute the netstat or ss command to verify if the new port joins correctly and listens for incoming traffic, we will execute any of the following lines:
netstat -tlpn | grep httpd ss -tlpn | grep httpd
Step 11
Like Ubuntu, we can go to the browser and enter using the following syntax:
http: // IP_address: 8081
With this method, we can modify the default Apache port on Linux.