Set Up Apache Virtual Hosts on Ubuntu VPS: Multi-Site Tutorial

By Raman Kumar

Share:

Updated on May 02, 2026

Set Up Apache Virtual Hosts on Ubuntu VPS: Multi-Site Tutorial

Why Apache Virtual Hosts Matter for VPS Users

Running multiple websites on a single Ubuntu VPS becomes straightforward once you understand Apache virtual hosts. This configuration lets you host example.com, blog.example.com, and shop.example.com all from one server while keeping their files and configurations separate.

Many hosting customers start with shared hosting, then upgrade to VPS when they need more control or performance. Hostperl VPS hosting gives you root access to configure Apache exactly how your business needs it.

Virtual hosts work by matching incoming HTTP requests to specific directory paths and configuration rules. When someone visits your domain, Apache checks the Host header and serves content from the appropriate directory.

Prerequisites and Planning Your Setup

You'll need root access to an Ubuntu VPS with Apache installed. Most VPS providers offer Ubuntu 22.04 LTS or 24.04 LTS images with Apache pre-installed or available through apt.

Plan your directory structure before starting. The standard approach places each site under /var/www/ with separate folders like /var/www/example.com/ and /var/www/blog.example.com/.

DNS records must point your domains to your VPS IP address. Create A records for each domain and subdomain you plan to host.

Most DNS changes take 5-15 minutes to propagate, though some providers cache longer.

Check that Apache is running and enabled:

sudo systemctl status apache2
sudo systemctl enable apache2

Create Directory Structure for Multiple Sites

Start by creating directories for each website you plan to host. This example sets up two sites: example.com and blog.example.com.

sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/blog.example.com/public_html
sudo mkdir -p /var/www/example.com/logs
sudo mkdir -p /var/www/blog.example.com/logs

Set proper ownership so your web user can manage files:

sudo chown -R www-data:www-data /var/www/example.com
sudo chown -R www-data:www-data /var/www/blog.example.com

Create test pages to verify your setup works:

echo '<h1>Welcome to example.com</h1>' | sudo tee /var/www/example.com/public_html/index.html
echo '<h1>Welcome to blog.example.com</h1>' | sudo tee /var/www/blog.example.com/public_html/index.html

Set Up Apache Virtual Hosts on Ubuntu VPS

Apache stores virtual host configurations in /etc/apache2/sites-available/. Create a configuration file for your main domain:

sudo nano /etc/apache2/sites-available/example.com.conf

Add this basic virtual host configuration:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog /var/www/example.com/logs/error.log
    CustomLog /var/www/example.com/logs/access.log combined
</VirtualHost>

The ServerName directive tells Apache which domain this configuration handles. ServerAlias lets you serve the same content for www.example.com and example.com.

Enable the site and test your configuration:

sudo a2ensite example.com.conf
sudo apache2ctl configtest

If the config test passes, reload Apache:

sudo systemctl reload apache2

Add Additional Virtual Hosts

Create a second virtual host for your blog subdomain:

sudo nano /etc/apache2/sites-available/blog.example.com.conf

Use this configuration:

<VirtualHost *:80>
    ServerName blog.example.com
    DocumentRoot /var/www/blog.example.com/public_html
    ErrorLog /var/www/blog.example.com/logs/error.log
    CustomLog /var/www/blog.example.com/logs/access.log combined
</VirtualHost>

Enable the blog site:

sudo a2ensite blog.example.com.conf
sudo systemctl reload apache2

Test both sites by visiting them in your browser. You should see different content for example.com and blog.example.com.

Managing multiple sites becomes easier with proper logging separation. Each virtual host creates its own access and error logs, making troubleshooting much simpler when issues arise.

Configure SSL for Virtual Hosts

Production websites need SSL certificates. Let's Encrypt provides free certificates that work perfectly with Apache virtual hosts.

Install Certbot:

sudo apt update
sudo apt install certbot python3-certbot-apache

Request certificates for both domains:

sudo certbot --apache -d example.com -d www.example.com
sudo certbot --apache -d blog.example.com

Certbot automatically modifies your virtual host files to add SSL configuration. It creates new VirtualHost blocks listening on port 443 and redirects HTTP traffic to HTTPS.

Your updated configuration file will look like this:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>

Set up automatic renewal to prevent certificate expiration:

sudo systemctl enable certbot.timer
sudo systemctl start certbot.timer

Advanced Virtual Host Configuration Options

Real-world hosting scenarios often require more complex configurations. Here are several useful patterns for production sites.

You can restrict directory access using Directory blocks:

<Directory /var/www/example.com/public_html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

For WordPress sites, enable .htaccess support:

<Directory /var/www/example.com/public_html>
    AllowOverride All
</Directory>

Add custom headers for security:

<VirtualHost *:443>
    # ... existing configuration ...
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-Content-Type-Options "nosniff"
</VirtualHost>

Enable the headers module first:

sudo a2enmod headers
sudo systemctl reload apache2

Troubleshooting Common Virtual Host Issues

When virtual hosts don't work as expected, start with these diagnostic steps.

Check if your sites are enabled:

sudo a2ensite --list

Verify Apache is loading your configuration without errors:

sudo apache2ctl configtest

View recent Apache error logs:

sudo tail -f /var/log/apache2/error.log

The most common issue is DNS misconfiguration. Verify your domain points to the correct IP:

dig +short example.com
dig +short blog.example.com

If you see the default Apache page instead of your site content, the default site might be taking precedence. Disable it:

sudo a2dissite 000-default
sudo systemctl reload apache2

File permission problems cause 403 Forbidden errors. Ensure Apache can read your files:

sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www/example.com

For SSL issues, check certificate status:

sudo certbot certificates

Our support team at Hostperl VPS hosting helps customers troubleshoot Apache configurations daily. Most virtual host problems stem from typos in configuration files or incorrect DNS settings.

Managing Virtual Hosts at Scale

As your hosting needs grow, consider these management strategies.

Use consistent naming conventions for configuration files. Name them after the primary domain: example.com.conf, blog.example.com.conf, shop.example.com.conf.

Create templates for common site types. Most business websites need similar security headers, logging, and SSL configuration. Copy working configurations and modify the ServerName and DocumentRoot.

Monitor disk usage across all sites:

du -sh /var/www/*

Set up log rotation to prevent logs from consuming too much disk space:

sudo nano /etc/logrotate.d/apache2-vhosts

Add this configuration:

/var/www/*/logs/*.log {
    daily
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    postrotate
        systemctl reload apache2
    endscript
}

Agencies and freelancers find virtual hosts essential for managing client websites efficiently. You can isolate each client's files while running everything on a single, well-configured server.

Ready to set up professional multi-site hosting? Hostperl VPS hosting provides the root access and performance you need for Apache virtual hosts. Our New Zealand-based support team helps with configuration questions and provides migration assistance when you're ready to upgrade from shared hosting.

Frequently Asked Questions

How many virtual hosts can I run on one VPS?

The limit depends on your VPS resources and traffic patterns. Most 1GB VPS instances handle 10-20 low-traffic sites comfortably. Monitor memory usage and CPU load as you add sites.

Can I use different PHP versions for different virtual hosts?

Yes, but this requires additional configuration with PHP-FPM pools. Each virtual host can specify a different PHP handler. This is useful when maintaining legacy applications alongside modern sites.

What happens if two virtual hosts have the same ServerName?

Apache uses the first matching virtual host configuration. Always use unique ServerName values. Use ServerAlias for alternative domain names that should serve the same content.

Should I disable the default Apache site?

Generally yes. The default site can interfere with your virtual host configurations. Disable it with sudo a2dissite 000-default once your sites are working properly.

How do I backup virtual host configurations?

Copy the entire /etc/apache2/sites-available directory and your web root directories under /var/www. Include SSL certificates from /etc/letsencrypt if using Let's Encrypt.