Configuring Apache HTTP Server to Listen on Multiple Ports (80 & 8080) – Complete Guide


2 views

html

When configuring Apache to listen on multiple ports, there are several critical components that need proper setup in your httpd.conf or ports.conf file (depending on your OS/distribution). The most fundamental directive is the Listen instruction.

# Basic port listening configuration
Listen 80
Listen 8080

Before diving deeper into configuration, let's verify some common issues:

  1. Check if the ports are actually being listened to:
    sudo netstat -tulpn | grep 'apache\|httpd'
    
  2. Verify your configuration files are being loaded:
    apachectl -S
    
  3. Check for syntax errors:
    apachectl configtest
    

For more complex setups where you want different behavior on different ports:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html
</VirtualHost>

<VirtualHost *:8080>
    ServerName dev.example.com
    DocumentRoot /var/www/dev
</VirtualHost>

On Linux systems, you'll often need to adjust firewall settings:

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

# For iptables
sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT

If you're still experiencing timeout issues, check:

  • Apache error logs (/var/log/apache2/error.log or /var/log/httpd/error_log)
  • Ensure no other service is using port 8080 (like another web server or application)
  • Verify that your DocumentRoot is properly configured for both ports

When configuring Apache to listen on multiple ports, the Listen directive is fundamental but requires proper implementation. The default httpd.conf typically contains:

# Default configuration
Listen 80

To add port 8080, you need to either:

  1. Add a separate Listen 8080 directive
  2. Combine them: Listen 80 Listen 8080

Here's a working configuration snippet:

# In httpd.conf or ports.conf
Listen 80
Listen 8080

<VirtualHost *:80>
    DocumentRoot "/var/www/html"
    ServerName localhost
</VirtualHost>

<VirtualHost *:8080>
    DocumentRoot "/var/www/html"
    ServerName localhost
</VirtualHost>

If the configuration isn't working, check these potential issues:

  • Firewall restrictions: Run sudo ufw allow 8080 (Ubuntu) or configure Windows Firewall
  • SELinux policies: On CentOS/RHEL, execute sudo semanage port -a -t http_port_t -p tcp 8080
  • Port conflicts: Verify with netstat -tulnp | grep 8080

For different content on each port:

<VirtualHost *:80>
    DocumentRoot "/var/www/main-site"
    ServerName example.com
</VirtualHost>

<VirtualHost *:8080>
    DocumentRoot "/var/www/dev-site"
    ServerName dev.example.com
    ErrorLog "/var/log/apache2/dev-error.log"
    CustomLog "/var/log/apache2/dev-access.log" common
</VirtualHost>

After configuration:

  1. Test config: apachectl configtest
  2. Restart Apache: sudo systemctl restart apache2
  3. Verify listening ports: ss -ltnp | grep apache