How to Change Apache Port in Linux

Share on Social Media

Discover how to change Apache port in Linux easily. Follow our simple guide to modify the default port and enhance your server’s security and configuration. #centlinux #linux #apache

Why Should you Change Apache Port?

Here are some reasons why you might want to change the Apache port:

  1. Avoiding Conflicts: If another service is already using the default port 80, changing the Apache port can prevent conflicts and ensure that both services run smoothly.
  2. Enhanced Security: Changing the default port makes it slightly more difficult for attackers to target your web server since port 80 is commonly targeted by bots and attackers.
  3. Multiple Web Servers: Running multiple instances of Apache or different web servers on the same machine requires using different ports for each server to operate without interference.
  4. Custom Network Configuration: Some network setups or hosting environments might require non-standard ports to accommodate specific rules or firewall settings.
  5. Testing and Development: Developers often change the Apache port to test applications on different environments or to run multiple projects simultaneously on the same machine.

Recommended Online Training: Apache Web Server

3771214 5435show?id=oLRJ54lcVEg&bids=1597309

Change Apache Port in Linux

Below is the code with added descriptions and instructions for each piece of code to change Apache port in Linux server:

1. Check Operating System and Kernel Version

cat /etc/os-release
uname -r
  • Description: This command checks the operating system’s release information and the current kernel version.
  • Purpose: To verify the Linux distribution and kernel version before proceeding with the installation, ensuring compatibility.

2. Update System Packages

dnf update -y
  • Description: Updates all the installed packages to their latest versions.
  • Purpose: Ensures that the system is up to date and that there are no security vulnerabilities.
How to Change Apache Port in Linux
How to Change Apache Port in Linux

3. Install Apache Server

dnf install -y httpd

4. Enable and Start Apache Server

systemctl enable --now httpd
  • Description: Enables Apache server to start at boot and immediately starts the Apache service.
  • Purpose: To ensure Apache server is running and will automatically start on system reboot.

5. Allow HTTP Traffic Through Firewall

firewall-cmd --permanent --add-service=http
firewall-cmd --reload
  • Description: Adds a permanent rule to the firewall to allow HTTP traffic, then reloads the firewall to apply the changes.
  • Purpose: To allow web traffic to reach the Apache server.

6. Create a Test Web Page

vi /var/www/html/index.html
  • Description: Opens the default web page directory in the vi text editor to create or edit a test HTML file.
  • Purpose: To create a simple web page for testing the Apache server.
<html><head><title>My Test Page</title></head>
<body>
<h2>My Test Page</h2>
</body>
</html>
  • Description: The HTML content to be placed in index.html.
  • Purpose: This basic HTML file will be served by Apache, allowing you to verify the server is working correctly.

7. Test Apache Server

curl http://localhost/
  • Description: Uses curl to fetch the default page from the Apache server.
  • Purpose: To verify that Apache is serving the test web page correctly.

8. Edit Apache Configuration to Change Port

vi /etc/httpd/conf/httpd.conf
  • Description: Opens the main Apache configuration file in the vi text editor.
  • Purpose: To change the port number Apache listens on (from default port 80 to a new port, e.g., 8800).
  • Instruction: Locate the line starting with Listen 80 and change it to Listen 8800 to set Apache to listen on port 8800.

9. Allow New Port Through Firewall

firewall-cmd --permanent --add-port=8800/tcp
firewall-cmd --reload
  • Description: Adds a permanent rule to the firewall to allow traffic on TCP port 8800, then reloads the firewall to apply changes.
  • Purpose: To permit HTTP traffic on the new port.

10. Install SELinux Management Utilities

dnf install -y policycoreutils-python-utils
  • Description: Installs the SELinux policy management utilities.
  • Purpose: Required to configure SELinux for the new port.

11. Configure SELinux to Allow Apache on New Port

semanage port -l | grep http_port
  • Description: Lists all the currently allowed HTTP ports in SELinux configuration.
  • Purpose: To check which ports are currently allowed for HTTP traffic.
semanage port -a -t http_port_t -p tcp 8800
  • Description: Adds TCP port 8800 to the SELinux configuration for HTTP traffic.
  • Purpose: To allow Apache to use the new port while SELinux is enabled.

12. Restart Apache Service

systemctl restart httpd
  • Description: Restarts the Apache service to apply the changes made in the configuration.
  • Purpose: Necessary to make Apache listen on the new port.

13. Test Apache on the New Port

curl http://localhost/
curl http://localhost:8800
  • Description: Uses curl to test the Apache server on both the default port (80) and the new port (8800).
  • Purpose: To verify that Apache is correctly serving content on the newly configured port.

If you are new to Linux and facing difficulty in working at Linux Bash prompt. We recommend that, you should read The Linux Command Line, 2nd Edition: A Complete Introduction by William Shotts.

Complete Video Guide to Change Apache Port in Linux

YouTube player

Conclusion

These instructions cover the steps to change the Apache web server’s listening port on a Linux system using the dnf package manager, commonly used on RHEL-based distributions like CentOS, Fedora, or Red Hat Enterprise Linux. Make sure to replace 8800 with any other port number if you prefer a different port.

Get professional Linux VPS setup and support on Amazon LightSail. Check out my services: Fiverr.com/ahmer2023.

Leave a Comment