Apache Virtual Hosts

Virtual hosts let a single Apache server host multiple websites on the same IP address and port. Apache uses the HTTP Host header from the incoming request to determine which virtual host configuration to use. Name-based virtual hosting is the standard approach: multiple domain names point to the same IP, and Apache routes each request to the correct document root based on the domain in the Host header.

Virtual hosts explained

Name-based virtual hosting:
  Client request: GET / HTTP/1.1
                  Host: site1.com
                  ↓
  Apache checks VirtualHost configs in order:
    VirtualHost *:80 with ServerName site1.com → serve from /var/www/site1/
    VirtualHost *:80 with ServerName site2.com → serve from /var/www/site2/

  Both sites served from the SAME server/IP on port 80

Creating a virtual host

# Create document root for the site:
sudo mkdir -p /var/www/site1.com/public
sudo chown -R www-data:www-data /var/www/site1.com
echo '

Site 1 works

' | sudo tee /var/www/site1.com/public/index.html # Create virtual host config: sudo nano /etc/apache2/sites-available/site1.com.conf

/etc/apache2/sites-available/site1.com.conf

<VirtualHost *:80>
    ServerName site1.com
    ServerAlias www.site1.com
    DocumentRoot /var/www/site1.com/public
    ErrorLog ${APACHE_LOG_DIR}/site1-error.log
    CustomLog ${APACHE_LOG_DIR}/site1-access.log combined

    <Directory /var/www/site1.com/public>
        Options -Indexes +FollowSymLinks
        AllowOverride All       ← enables .htaccess files
        Require all granted
    </Directory>
</VirtualHost>
# Enable the site and test:
sudo a2ensite site1.com.conf
sudo a2dissite 000-default.conf    # Disable default site (optional)
sudo apache2ctl configtest
sudo systemctl reload apache2

# Verify:
curl -H "Host: site1.com" http://localhost

SSL virtual host configuration

# Get SSL certificate with Certbot:
sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache -d site1.com -d www.site1.com

# Certbot automatically creates and enables an SSL virtual host
# and adds a redirect from HTTP to HTTPS

# Or manually configure SSL (for custom certificates):
sudo nano /etc/apache2/sites-available/site1.com-ssl.conf

SSL virtual host block

<VirtualHost *:443>
    ServerName site1.com
    DocumentRoot /var/www/site1.com/public
    SSLEngine on
    SSLCertificateFile      /etc/ssl/certs/site1.com.crt
    SSLCertificateKeyFile   /etc/ssl/private/site1.com.key
    SSLCertificateChainFile /etc/ssl/certs/site1.com-chain.crt
</VirtualHost>

Running multiple sites

# Each site gets its own config file:
sudo a2ensite site1.com.conf
sudo a2ensite site2.com.conf
sudo a2ensite api.company.com.conf

# List all enabled sites:
ls /etc/apache2/sites-enabled/

# Apache processes virtual hosts in alphabetical filename order
# The first matching ServerName/ServerAlias wins
# If no match, the first VirtualHost in order is the default

# Enable required modules for most sites:
sudo a2enmod ssl rewrite headers
sudo systemctl reload apache2

Conclusion

Each site gets its own config file in sites-available/ with a unique ServerName, its own DocumentRoot with correct ownership (www-data:www-data), and separate log files. Enable it with a2ensite, test with apache2ctl configtest, then reload. The most common mistake is forgetting AllowOverride All in the Directory block when the application uses .htaccess for URL rewriting — without it, mod_rewrite rules in .htaccess are silently ignored.

FAQ

Is Apache Virtual Hosts important for Ubuntu administrators?+

Yes. It supports practical Ubuntu administration because it connects directly to server reliability, security, troubleshooting, or daily operations.

Should I practice this on a live server?+

Use a lab VM first. After you understand the command output and rollback path, apply the workflow carefully on real systems.

What should I do after reading this article?+

Run the practice commands, write down what each one shows, and continue to the next article in the Ubuntu roadmap.

Need help with Ubuntu administration?

Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.

Hire Me for Support