Apache Virtual Host Configuration: How to Set Up Multiple Websites on Different Ports (80, 81, 82)


2 views

Before configuring Apache, let's verify port availability using these commands:

# Check if ports are listening
sudo netstat -tulnp | grep -E '80|81|82'

# Alternative using ss
sudo ss -tulnp | grep -E ':8[0-2]'

# Check firewall status (for RHEL/CentOS)
sudo firewall-cmd --list-ports
# For Ubuntu/Debian
sudo ufw status

For RHEL/CentOS systems with firewalld:

sudo firewall-cmd --permanent --add-port=81/tcp
sudo firewall-cmd --permanent --add-port=82/tcp
sudo firewall-cmd --reload

For systems using iptables (note the corrected syntax):

sudo iptables -A INPUT -p tcp --dport 81 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 82 -j ACCEPT
sudo service iptables save  # On RHEL/CentOS 6
# For persistent rules on Ubuntu/Debian:
sudo apt-get install iptables-persistent
sudo netfilter-persistent save

Edit your Apache configuration file (typically httpd.conf or ports.conf):

# First, ensure Apache listens on all required ports
Listen 80
Listen 81
Listen 82

Then create separate virtual host configurations:

<VirtualHost *:80>
    ServerName mydomain.com
    DocumentRoot /var/www1
    ErrorLog ${APACHE_LOG_DIR}/error_port80.log
    CustomLog ${APACHE_LOG_DIR}/access_port80.log combined
</VirtualHost>

<VirtualHost *:81>
    ServerName mydomain.com
    DocumentRoot /var/www2
    ErrorLog ${APACHE_LOG_DIR}/error_port81.log
    CustomLog ${APACHE_LOG_DIR}/access_port81.log combined
</VirtualHost>

<VirtualHost *:82>
    ServerName mydomain.com
    DocumentRoot /var/www3
    ErrorLog ${APACHE_LOG_DIR}/error_port82.log
    CustomLog ${APACHE_LOG_DIR}/access_port82.log combined
</VirtualHost>

If you can't access the URLs, try these diagnostic steps:

# Check Apache syntax
sudo apachectl configtest

# Verify SELinux context (RHEL/CentOS)
ls -Zd /var/www1/ /var/www2/ /var/www3/

# Check for correct permissions
sudo chown -R apache:apache /var/www1 /var/www2 /var/www3
sudo chmod -R 755 /var/www1 /var/www2 /var/www3

For SELinux issues on RHEL/CentOS:

sudo semanage port -a -t http_port_t -p tcp 81
sudo semanage port -a -t http_port_t -p tcp 82
sudo restorecon -Rv /var/www*/

For better organization, consider separate configuration files:

# In main Apache config:
IncludeOptional sites-enabled/*.conf

# Then create separate files:
# /etc/apache2/sites-available/mydomain_80.conf
# /etc/apache2/sites-available/mydomain_81.conf
# /etc/apache2/sites-available/mydomain_82.conf

# Enable them with (Debian/Ubuntu):
sudo a2ensite mydomain_80
sudo a2ensite mydomain_81
sudo a2ensite mydomain_82

First, verify which ports are currently listening on your Linux system:

netstat -tulpn | grep -E '80|81|82'

Or use the more modern ss command:

ss -tulnp | grep -E '80|81|82'

Edit your Apache configuration file (typically httpd.conf or ports.conf):

Listen 80
Listen 81
Listen 82

Add these configurations in your virtual hosts file or httpd.conf:

<VirtualHost *:80>
    DocumentRoot /var/www1
    ServerName mydomain.com
    <Directory /var/www1>
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:81>
    DocumentRoot /var/www2
    ServerName mydomain.com
    <Directory /var/www2>
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:82>
    DocumentRoot /var/www3
    ServerName mydomain.com
    <Directory /var/www3>
        Require all granted
    </Directory>
</VirtualHost>

For firewalld systems:

firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=81/tcp
firewall-cmd --permanent --add-port=82/tcp
firewall-cmd --reload

For iptables systems (corrected from your example):

iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 81 -j ACCEPT
iptables -A INPUT -p tcp --dport 82 -j ACCEPT
service iptables save

1. SELinux Contexts (if you're using SELinux):

semanage port -a -t http_port_t -p tcp 81
semanage port -a -t http_port_t -p tcp 82

2. Verify Configuration:

apachectl configtest

3. Restart Apache:

systemctl restart httpd   # For systemd
service httpd restart     # For SysVinit

After configuration, test with curl:

curl -I http://mydomain.com:81
curl -I http://mydomain.com:82

Or test from a web browser by directly accessing the URLs.