How to modify the listen port for Apache
The Apache HTTP Server, commonly referred to as Apache, is one of the most widely used web servers. By default, Apache listens on port 80 for HTTP and port 443 for HTTPS. However, there may be situations where you need to change these default ports, such as avoiding conflicts with other services or implementing specific network configurations.
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:
-
Open the terminal on your server.
-
Search for Listen directive in Apache's configuration files.
$ 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
-
Use your preferred text editor to open the Apache configuration file.
$ sudo vi /etc/apache2/ports.conf
-
Go to the line that starts with Listen, followed by the current port number.
Listen 80
-
Change the port number to the desired value.
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 the file and exit the text editor.
-
Edit related VirtualHost configurations to utilize the new port.
<VirtualHost *:8080> ##### snipped </VirtualHost>
-
Ensure that the new port is allowed in 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
-
Verify that Apache is now listening on the new port by accessing your website or server using the new 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