How to Change the Apache port in CentOS

August 7, 2024 / Servers, Hosting & Email

This article explains how to change the Apache port in CentOS. Changing the Apache port in CentOS is often necessary to resolve port conflicts, improve security, or accommodate specific application requirements.

Follow these steps:

  1. Edit the Apache Configuration File:
    1. Open the Apache configuration file using a text editor like vi or nano.
      sudo vi /etc/httpd/conf/httpd.conf
  2. Find and Change the Listen Directive:
    1. Look for the Listen directive in the file. It usually looks like this:
      Listen 80
    2. Change it to the desired port number. For example, to change it to port 8080:
      Listen 8080
  3. Update Virtual Hosts (if necessary):
    1. If you have virtual hosts configured, you also need to update their port numbers. Locate the virtual host configurations, which may look like this:
      <VirtualHost *:80>
          ...
      </VirtualHost>
    2. Change the port number to match the new port:
      <VirtualHost *:8080>
      ...
      </VirtualHost>
  4. Restart Apache:
    1. After making the changes, restart Apache to apply the new configuration.
      sudo systemctl restart httpd
  5. Update Firewall Rules (if necessary):
    1. If you are using a firewall, you need to update the firewall rules to allow traffic on the new port. For example, to allow port 8080 through the firewall:
      sudo firewall-cmd --permanent --add-port=8080/tcp
      sudo firewall-cmd --reload
  6. Verify the Changes:
    1. Ensure that Apache is listening on the new port by using the netstat or ss command:
      sudo netstat -tuln | grep 8080

      or

      sudo ss -tuln | grep 8080

By following these steps, you should be able to change the Apache ports on CentOS successfully.

Explore our in-depth guide on How to Modify the Apache port in WHM

Spread the love