Today we will continue our Apache web server series and talk about how to change the port in Apache. Some people consider it as a security measure too.
The default port for Apache web server is 80, We will try to change it to 8080 at first.
Let’s open its main configuration file by “vim /etc/httpd/conf/httpd.conf” command and look for the port.

We can see “Listen 80” that shows Apache web server is working on port 80 right now. We will change it to ‘8080‘ by making “Listen 8080“.

Next, We will exit the vim editor and restart the Apache services by “systemctl restart httpd” command.

We have to see the current port of httpd, We can check that by “netstat -tnulp | grep httpd” command.

As we can see our httpd port is changed to 8080, This is super simple to do but now we will change the port to a customize port of our choice that is “777“.
We have to open the Apache configuration file one more time.

We will again restart httpd service by “systemctl restart httpd” command.
This time we will get error because “777” port for Apache is not aligned with SElinux.

Let’s check our error by the mentioned command “journalctl -xeu httpd.service“.

Now we have to add our port in SElinux but before that we will check all the ports available in default for httpd by “semanage port -l” command.

There is no ‘777‘ port is allowed in SELinux for port in Apache.
We have to add ‘777‘ port in SELinux by “semanage port -a -t http_port_t -p tcp 777” command.

This command has -a for adding, -t for type, -p for port and semanage helps to manage the SELinux.
Now we can again check all the ports of httpd service.

We can see ‘777‘ port is now added in the list.
Let’s start our httpd service again by “systemctl start httpd” command.

Now we didn’t get any error and our service has started successfully.
That’s all about how to change the port in Apache.
You may like our another post about Apache.
FAQ on How to change the port in Apache
Q. Why would I need to change the Apache port number?
A. You may want to change Apache’s port if:
Port 80 is already in use by another service.
You want to run multiple web servers on the same machine.
You need to secure or isolate your site by running it on a non-default port.
Q. What is the default port in Apache?
A. Apache uses port 80 for HTTP and port 443 for HTTPS by default.
Q. Can I make Apache listen on multiple ports?
A. Yes. Add multiple Listen entries:
Listen 80
Listen 8080
Q. How do I verify which ports Apache is listening on?
A. Use the following command:
netstat -tuln | grep apache
Discover more from Root Learning
Subscribe to get the latest posts sent to your email.