To configure Apache to listen on a custom port, you’ll need to make changes to its configuration files. Here are the steps:
Backup Configuration Files
Creating a backup copy of your Apache configuration file is a good practice before making any changes. Here’s how you can do it:
On Linux/Unix Systems
Use a command-line text editor like cp
(copy) or cp
with sudo
to create a backup:
sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf_backup
Replace /etc/apache2/apache2.conf
with the actual path to your Apache configuration file.
On Windows
Navigate to the directory where your Apache configuration file is located. Use the copy
command to create a backup:
copy httpd.conf httpd_backup.conf
Replace httpd.conf
with the actual name of your Apache configuration file.
After creating the backup, you’ll have a copy of the configuration file with the “_backup” suffix. You can use this backup to restore the configuration if needed, especially should anything go wrong.
Open the Apache Configuration File
Open the Apache configuration file in a text editor. The location of the configuration file can vary depending on your operating system and installation method. Common locations are:
- For CentOS/RHEL Linux systems
nano /etc/httpd/httpd.conf
- For Ubuntu/Debian Linux systems
nano /etc/apache2/apache2.conf
- For Windows Operating Systems
C:\Program Files\Apache Group\Apache2\conf\httpd.conf
Below is the sample Apache configuration file:
ServerRoot "/etc/apache2"
Listen 80
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
# ...
IncludeOptional sites-enabled/*.conf
Find the “Listen” Directive
In the Apache configuration file, look for a line that starts with the “Listen” directive. This directive specifies the port on which Apache listens for incoming connections.
Listen 80
Change the Port
Change the port number to your desired custom port. For example, if you want Apache to listen on port 8080:
Listen 8080
Update Virtual Hosts (Optional)
If you have virtual hosts configured, you also need to update their configurations to reflect the new port. Look for <VirtualHost>
sections in your configuration file and update the VirtualHost
directive with the new port.
<VirtualHost *:8080>
# Virtual host configuration
</VirtualHost>
Save the changes to the configuration file and close the text editor.
Restart Apache
After making changes, you need to restart the Apache server for the changes to take effect. The command to restart Apache varies by operating system:
sudo service apache2 restart
or
sudo systemctl restart apache2
Now, Apache should be listening on the custom port you specified. Access your web server by navigating to http://yourdomain.com:8080
(replace yourdomain.com
with your actual domain or IP address and 8080
with your custom port).