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

Pros and Cons of using Non Default Apache Port

Pros of Changing Apache Port in Linux

  1. Avoid Conflicts with Other Services
    Changing the Apache port allows you to avoid conflicts with other services or applications that might be using the default HTTP port (80). This is particularly useful if you are running multiple web servers or services on the same machine.
  2. Increased Security through Obscurity
    Changing the default port can help reduce the risk of automated attacks that target well-known ports like 80 and 443. By using a non-standard port, you make it slightly more difficult for attackers to find your web server.
  3. Running Multiple Web Servers
    If you need to run multiple Apache instances on the same system, changing the port for each instance can help you organize your environment. This is beneficial for testing, development, or hosting multiple websites with different configurations.
  4. Avoid Port Blocking or Restrictions
    Some networks or firewalls may block access to common HTTP ports (such as 80 or 443). By using an alternate port, you may bypass such restrictions and allow access to your web server through an open port.
  5. Testing or Development Purposes
    For testing or staging environments, it may be necessary to use a non-default port to simulate different network configurations or test how your application behaves when accessed through a different port.

Cons of Changing Apache Port in Linux

  1. Access Confusion for Users
    When you change the Apache port, users must specify the port in the URL (e.g., http://example.com:8800). This can confuse users and reduce the ease of access, especially if they are accustomed to the default HTTP port (80).
  2. Firewall Configuration
    After changing the port, you must ensure that your firewall is configured to allow traffic on the new port. This may require additional setup and could lead to connectivity issues if the firewall isn’t properly updated.
  3. Compatibility Issues
    Some network appliances, load balancers, or reverse proxies might be configured to expect Apache to run on the default ports (80 and 443). Changing the port may lead to compatibility issues, requiring additional adjustments in the network configuration.
  4. Potential for Overlooking Port Access
    Administrators may forget to open the new port in the firewall or fail to update relevant security settings, which could result in users being unable to access the web server.
  5. Possible SEO or External Link Impact
    Changing the port can affect how search engines index your site and can disrupt existing external links, since many external tools and websites expect the standard ports. This could potentially lead to reduced visibility or indexing issues.

Overall, while changing the Apache port offers flexibility and security, it requires careful planning and proper configuration to avoid connectivity problems and ensure smooth operation.

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.

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:

How to Change Apache Port in Linux
How to Change Apache Port in Linux

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.

Recommended Training: Apache Web Server from Vipin Gupta

3771214 5435

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.

Read Also: Change Apache Document Root in Linux

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.

Complete Video Guide to Change Apache Port in Linux

YouTube player

Conclusion

These instructions provide a comprehensive guide to changing the Apache web server’s listening port on a Linux system, specifically for those using the dnf package manager. This package manager is commonly found in RHEL-based distributions such as CentOS, Fedora, and Red Hat Enterprise Linux (RHEL).

By following these steps, you can easily modify the default port that Apache listens on. For example, in this guide, we use port 8800, but feel free to replace it with any other port number of your choosing, depending on your needs or the availability of the port in your network environment. Make sure that the selected port is not in use by other services and is within the appropriate range for your system.

Looking for a Linux server expert? I provide top-tier administration, performance tuning, and security solutions for your Linux systems. Explore my Fiverr profile for details!

Frequently Asked Questions (FAQs)

1. How do I change the Apache port in Linux?
To change the Apache port, you need to modify the Listen directive in the Apache configuration file. Open /etc/httpd/conf/httpd.conf or /etc/apache2/ports.conf (depending on your distribution) and change the port number in the Listen directive. For example, Listen 8080 to change to port 8080.

2. After changing the port, do I need to restart Apache?
Yes, after modifying the port in the Apache configuration file, you must restart Apache for the changes to take effect. Use the command sudo systemctl restart apache2 or sudo systemctl restart httpd based on your distribution.

3. Can I change the Apache port to any number?
You can change the port to any number, but ensure the port is not already in use by another service, and it must be within the valid port range (1-65535). Ports below 1024 typically require root privileges.

4. How do I check if the new port is in use by Apache?
You can check if Apache is listening on the new port by using the command sudo netstat -tuln | grep <port_number>. If Apache is correctly configured, it will display the port in the output.

5. Do I need to adjust my firewall settings after changing the Apache port?
Yes, you may need to update your firewall settings to allow traffic on the new port. For example, use sudo ufw allow <port_number>/tcp to open the new port if you are using UFW on Ubuntu.

Looking for something?

Leave a Reply