The Apache HTTP Server typically listens on port 80 for HTTP and port 443 for HTTPS. In some cases, you may need to change these default ports. This is often necessary to avoid conflicts with other services running on the same server or to meet specific network configuration requirements.

Changing the listen port for Apache is a straightforward process but requires administrative access to the server. It involves modifying the Apache configuration files and restarting the service to apply the changes.

Whether you are running Apache on a Linux, macOS, or Windows system, the steps to change the listen port are similar. Below, you'll find a guide tailored to a typical Linux-based Apache installation.

Steps to change listen port for Apache:

  1. Open the terminal on your server.
  2. Locate the Apache configuration file containing the Listen directive.
    $ sudo grep -nr "^Listen" /etc/{httpd,apache2}/
    [sudo] password for user: 
    grep: /etc/httpd/: No such file or directory
    /etc/apache2/ports.conf:5:Listen 80
  3. Edit the configuration file using your preferred text editor.
    $ sudo vi /etc/apache2/ports.conf
  4. Find the line with the Listen directive, followed by the current port number.
    Listen 80
  5. Replace the current port number with the desired new port number.
    Listen 8080

    Make sure no other services are using the port you have chosen. You can use the netstat command to check for conflicts.

  6. Save and close the configuration file.
  7. Edit related VirtualHost configurations to utilize the new port.
    <VirtualHost *:8080>
      ##### snipped
    </VirtualHost>
  8. Allow the new port through your firewall.
    $ sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
    $ sudo ufw allow 8080/tcp
  9. Add the new port to the list of allowed ports for HTTP traffic for SELinux.
    $ sudo semanage port -a -t http_port_t -p tcp 8080
  10. Restart the Apache service to apply the changes.
    $ sudo systemctl restart apache2 # Ubuntu, Debian, openSUSE and SLES
    $ sudo systemctl restart httpd # CentOS and Red Hat
  11. Confirm that Apache is now listening on the new port by accessing the server with the updated port number in the URL.
    $ curl -I 127.0.0.1:8080
    HTTP/1.1 200 OK
    Date: Fri, 01 Sep 2023 11:58:10 GMT
    Server: Apache/2.4.55 (Ubuntu)
    Last-Modified: Thu, 31 Aug 2023 09:37:27 GMT
    ETag: "29af-60434cacb2109"
    Accept-Ranges: bytes
    Content-Length: 10671
    Vary: Accept-Encoding
    Content-Type: text/html
Discuss the article:

Comment anonymously. Login not required.