
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Change Apache Port in Linux?
Apache HTTP server (often called Apache) is an open-source, cross-platform web server developed by Apache Software Foundation. It is one of the most popular web servers available used for hosting web sites and serving web content.
By default, Apache works on port 80/tcp for HTTP and 443/tcp for HTTPS. There are situations (like security considerations, port conflicts with other programs or need to run multiple web servers) where one may need to modify these default ports which the web server listens on.
Changing Apache Port on Linux
Read this tutorial to learn how to change HTTP port for Apache on Linux system (CentOS / Ubuntu). Here is a step-by-step guide -
Step 1 ? Open a terminal window or connect to the Linux system via SSH.
Step 2 ? On CentOS/RHEL based systems, Apache package is referred to as httpd whereas on Debian/Ubuntu systems, it is referred to as apache or apache2. Based on the package name, locate the web server's configuration file inside `/etc`.
Step 3 ? Find the line containing Listen 80 keyword. For CentOS/RHEL, the file to look for is /etc/httpd/conf/httpd.conf, while for Debian/Ubuntu, this configuration could either be under /etc/apache2/apache2.conf or /etc/apache2/ports.conf (search for the Listen keyword in possible configuration files).
# CentOS/RHEL $ sudo grep ^Listen /etc/httpd/conf/httpd.conf Listen 80 $ # Debian/Ubuntu $ sudo grep ^Listen /etc/apache2/ports.conf Listen 80 $
Step 4 ? There is also VirtaulHost configuration file, which exists by default, in case of Debian/Ubuntu, that also needs to be updated to match the port in main Apache port's configuration. Location for this VirtaulHost configuration file is /etc/apache2/sites-enabled/000-default.conf.
# Debian/Ubuntu $ sudo grep \<VirtualHost /etc/apache2/sites-enabled/000-default.conf <VirtualHost *:80> $
Step 5 ? On CentOS/RHEL, by default, we don't have virtual hosts configured. But in case there is one, you also need to modify the port in its respective VirtualHost configuration file. It should be located within conf.d directory matching with your domain.
In this tutorial, we'll assume no virtual host is configured for CentOS/RHEL case.
# CentOS/RHEL, if VirtualHost is configured $ sudo grep \<VirtualHost /etc/httpd/conf.d/mydomain.com.conf <VirtualHost *:80> $
Step 6 ? Edit the configuration file using vi or nano tool to modify the Listen port value to your desired port for HTTP.
# CentOS/RHEL $ sudo vi /etc/httpd/conf/httpd.conf # Debian/Ubuntu $ sudo vi /etc/apache2/ports.conf # VirtualHost configuration in Debian/Ubuntu $ sudo vi /etc/apache2/sites-enabled/000-default.conf
Step 7 ? In our case, we're modifying the value of 80 to 8080. The updated value should look something like below ?
# CentOS/RHEL $ sudo grep ^Listen /etc/httpd/conf/httpd.conf Listen 8080 $ # Debian/Ubuntu $ sudo grep ^Listen /etc/apache2/ports.conf Listen 8080 $ sudo grep \<VirtualHost /etc/apache2/sites-enabled/000-default.conf <VirtualHost *:8080> $
Step 8 ? If SELinux is enabled on your system, you need to allow SELinux to permit Apache on the new port.
# Allow SELinux to permit Apache on the new port $ sudo semanage port -a -t http_port_t -p tcp 8080 # If the port already exists, modify it instead $ sudo semanage port -m -t http_port_t -p tcp 8080
Step 9 ? If firewall is enabled on your OS, add the new port to the firewall rules for allowing the network traffic based on your specific OS and firewall package.
# CentOS/RHEL (using firewalld) $ sudo firewall-cmd --permanent --add-port=8080/tcp $ sudo firewall-cmd -reload # CentOS/RHEL (using iptables) $ sudo iptables -I INPUT -p tcp --dport 8080 -j ACCEPT $ sudo service iptables save # Debian/Ubuntu (using ufw) $ sudo ufw allow 8080/tcp
Step 10 ? Verify the syntax of Apache's modified configuration files using apachectl command. This should show Synatx OK if everything is configured correctly.
$ sudo apachectl configtest
Step 11 ? Finally, only if all configurations are good, restart the webserver service, i.e., httpd in case of CentOS/RHEL or apache2 for Debian / Ubuntu.
# CentOS/RHEL $ sudo systemctl restart httpd # Debian/Ubuntu $ sudo systemctl restart apache2
Step 12 ? Now you should be able to access your site on the new port from your browser. From CLI, you can test using curl (or wget) command, if it is installed.
$ curl http://localhost:8080 OR $ wget http://localhost:8080
Step 13 ? Common issues encountered could be syntax error is configuration file, firewall issues or port conflicts with another running service. You can check the status of Apache service and review logs for any errors. Further verify the port for Apache using netstat, ss or lsof command.
# CentOS/RHEL sudo systemctl status httpd # Debian/Ubuntu sudo systemctl status apache2 # Check Apache port $ sudo netstat -tulpn | grep apache OR $ sudo ss -tulpn | grep apache OR $ sudo lsof -i :8080
Conclusion
Working with web servers like Apache is a common activity in the job profile of a system administrator. Modifying the default port used by a service like Apache is one such common requirement.
We have shown how you can easily modify the HTTP port for Apache web server on a Linux system covering both CentOS/RHEL and Debian/Ubuntu type Linux distributions.