The Apache HTTP Server typically listens on port 80 for HTTP and port 443 for HTTPS. Some environments or existing services might necessitate adjusting these ports to avoid conflicts.
In Linux, macOS, and Windows environments, changing the listen port for Apache requires root or administrative privileges to edit the relevant configuration files. A mismatch between service and firewall rules can disrupt connectivity, so thorough configuration is crucial.
The Listen directive determines which port Apache uses, and updates to firewall settings or SELinux policies may also be required to ensure the service is accessible. Careful planning and validation can help maintain uninterrupted operations when altering default ports.
Steps to change the listen port for Apache:
- Open the terminal on your server.
- 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
Related: Location for Apache configuration
- Edit the configuration file using your preferred text editor.
$ sudo vi /etc/apache2/ports.conf
- Find the line with the Listen directive, followed by the current port number.
Listen 80
- 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.
- Save and close the configuration file.
- Edit related VirtualHost configurations to utilize the new port.
<VirtualHost *:8080> ##### snipped </VirtualHost>
- Allow the new port through your firewall.
$ sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT $ sudo ufw allow 8080/tcp
- 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
- 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
- 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

Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
Comment anonymously. Login not required.