How To Change Apache HTTP Port in Linux
We show you how you can change an HTTP port in Linux Apache.
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 computer. In case of using a TLS configuration, the server will listen for secure connections on port 443.
If the objective is for the Apache web server to link and listen to web traffic in other ports in addition to the standard web ports, it will be necessary to add a new instruction where the new listening port will be included.
Modify HTTP Port of Apache Server
In distributions based on Debian or Ubuntu, the configuration file to edit 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)
For this case, we will use Ubuntu 17. When accessing this file, we will see the following:
As we can see, the default value in the line LISTEN is port 80.
For this tutorial, we will configure the Apache HTTP server so that it listens to the connections in port 8081 of the devices.
Therefore, we will add the line “LISTEN 8081” just under the port 80 line:
Save the changes using the following key combination Ctrl + O and exit the editor using Ctrl + X.
After adding the indicated line, it will be necessary to create or modify a virtual Apache host in distributions based on Debian or Ubuntu to initiate the link process, which will be helpful for the vhost requirements.
In the case of distributions based on CentOS or RHEL, the change is applied directly to the default virtual host.
In this case, we will open and edit the file 000-default.conf, and there we will change the port to 8081 in the following way:
nano /etc/apache2/sites-enabled/000-default.conf
We set port 8081 on the “VirtualHost” line, save the changes and exit the editor.
Finally, we are going to apply the changes and allow Apache to link with the new port, for this we will restart the daemon and verify the local network socket table using the netstat or ss command.
Port 8081 to listen to should be displayed in the server network table as defined above; we will execute the following:
systemctl restart apache2 netstat -tlpn| grep apache ss -tlpn| grep apache
Now, to verify that the connection is correct, we will access from a browser using the following syntax:
http://IP_address:8081
In CentOS / RHEL-based Linux distributions, we are going to install the policycoreutils package to add the required SELinux rules so that Apache can link to the new port and restart the Apache HTTP server to apply the changes. We execute the following:
yum install policycoreutils
There we enter the letter and to confirm the download and installation. Then, we will add the SELinux rules for port 8081:
semanage port -a -t http_port_t -p tcp 8081 semanage port -m -t http_port_t -p tcp 8081
Now, we proceed to reset the Apache service:
systemctl restart httpd.service
Then, we will execute the command netstat or ss to verify if the new port joins correctly and listens to the incoming traffic, we will run some of the following lines:
netstat -tlpn| grep httpd ss -tlpn| grep httpd
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 port of Apache in Linux.