Posted on
Apache Web Server

Changing the default Apache port (80 → 8080)

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Changing the Default Apache Port on Linux: From 80 to 8080

As your website grows larger and your online needs evolve, you might find yourself needing to tweak some server settings. One common setting that many users need to modify is the Apache HTTP Server's default port number. By default, Apache listens for incoming connections on port 80, the standard port for HTTP traffic. However, there are scenarios in which changing this can be beneficial. For those running multiple websites, using firewalls, or handling network policies that restrict use of the default port, understanding how to change Apache's listening port to, say, 8080, can be essential.

Here's a detailed guide on how to change Apache’s default port from 80 to 8080 in a Linux environment.

Step 1: Installing Apache (if not already installed)

Before proceeding with changing the port, ensure that Apache is installed on your Linux server. You can install Apache using your Linux distribution’s package manager. For Ubuntu and Debian-based systems, use:

sudo apt update
sudo apt install apache2

For Red Hat or CentOS systems, you would use:

sudo yum install httpd

Step 2: Modify Apache Configuration Files

Apache’s configuration is determined by .conf files located in /etc/apache2 on Debian-based systems or /etc/httpd on Red Hat-based systems. The main configuration file is typically named httpd.conf or apache2.conf.

  1. Open the main Apache configuration file for editing:

    sudo nano /etc/apache2/apache2.conf   # For Debian/Ubuntu
    sudo nano /etc/httpd/conf/httpd.conf  # For CentOS/Red Hat
    
  2. Search for the line that specifies Listen 80. It tells Apache to listen on port 80. Change this to Listen 8080.

  3. Check for virtual host settings: Apache may also have Virtual Host settings in separate files such as 000-default.conf under /etc/apache2/sites-available/. If present, modify these too:

    <VirtualHost *:80>
    

    Change it to:

    <VirtualHost *:8080>
    

Step 3: Adjust Firewall Settings

After changing the port in Apache’s configuration files, you need to modify firewall rules to allow traffic on the new port (8080).

For ufw (Uncomplicated Firewall) used in Ubuntu:

sudo ufw allow 8080

For firewalld on CentOS or Red Hat:

sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

Step 4: Restart Apache

Any changes made in the configuration files take effect only after restarting the server.

sudo systemctl restart apache2   # Debian/Ubuntu
sudo systemctl restart httpd     # CentOS/Red Hat

Step 5: Test the New Setup

Finally, ensure that Apache is now hosting on the new port by accessing your server in a web browser:

http://your-server-ip:8080

You should see your website’s default page or whatever content is hosted under Apache.

Conclusion

Changing the default Apache port from 80 to 8080 or any other non-standard port can help you manage network traffic more effectively, adhere to specific security policies, or run multiple web servers on a single machine. The process involves editing Apache's configuration files, adjusting firewall rules, and restarting the Apache service to apply the changes. With these steps, you can ensure that your server meets your requirements, providing a more tailored and secure hosting environment. Remember, the principle of security by obscurity (like changing default ports) is not foolproof and should be a part of a broader, comprehensive security strategy.

Further Reading

For further understanding and related topics about Apache server configurations and Linux server management, consider exploring these resources:

  1. Apache Official Documentation: Detailed information on Apache configurations and tuning. https://httpd.apache.org/docs/

  2. DigitalOcean Tutorial on Apache: A practical guide to installing and managing Apache in a Linux environment. https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04

  3. Firewall Configuration for Apache: Understanding firewall rules for securing Apache on Linux. https://linuxize.com/post/how-to-setup-a-firewall-with-ufw-on-ubuntu-18-04/

  4. Using Virtual Hosts in Apache: How to configure multiple websites on a single server using Apache virtual hosts. https://httpd.apache.org/docs/2.4/vhosts/

  5. Linux Server Security Best Practices: Comprehensive guide to securing Linux servers in a production environment. https://www.cyberciti.biz/tips/linux-security.html

These resources will help deepen your understanding of web server management and security practices, essential for managing robust, secure servers.