Quick Summary: Apache Virtual Hosts allow a single Apache web server to host multiple domains (e.g., site1.com and site2.com) on one IP address. This is achieved by using Name-based Virtual Hosting, which directs incoming requests to the correct directory based on the hostname in the HTTP header.
What Is an Apache Virtual Host?
Apache Virtual Hosts are configuration blocks that tell the Apache web server how to handle requests for different domains. Instead of needing a separate server for every website, you can manage dozens or even hundreds of websites on a single VPS or dedicated server. This is the foundation of shared web hosting and is how the vast majority of websites on the internet are served.
There are two types of Virtual Hosts:
- Name-based Virtual Hosts β Multiple domains share a single IP address. Apache determines which site to serve based on the
Hostheader in the HTTP request. This is the most common and recommended approach. - IP-based Virtual Hosts β Each domain gets its own IP address. This was necessary before SNI (Server Name Indication) for SSL, but is rarely needed in 2026.
How to Create Apache Virtual Hosts (Step-by-Step)
Step 1: Prepare the Directory Structure
Create a dedicated directory for each website. The standard convention is to use /var/www/ as the parent directory:
- Create the site directory:
sudo mkdir -p /var/www/example.com/public_html - Create directories for logs:
sudo mkdir -p /var/www/example.com/logs - Set ownership:
sudo chown -R $USER:$USER /var/www/example.com/public_html - Set permissions:
sudo chmod -R 755 /var/www/example.com
Step 2: Create a Test Page
Create a simple index.html to verify the Virtual Host works. Add a heading that identifies which site this is, so you can confirm the correct Virtual Host is being served.
Step 3: Create the Virtual Host Configuration File
On Debian/Ubuntu, create a new configuration file in /etc/apache2/sites-available/. On RHEL/AlmaLinux/Rocky, add the configuration to /etc/httpd/conf.d/.
Key Configuration Directives
| Directive | Purpose | Example Value |
|---|---|---|
ServerName | Primary domain for this Virtual Host | example.com |
ServerAlias | Additional domains (www subdomain) | www.example.com |
DocumentRoot | Directory containing website files | /var/www/example.com/public_html |
ErrorLog | Path to error log file | /var/www/example.com/logs/error.log |
CustomLog | Path to access log file | /var/www/example.com/logs/access.log combined |
ServerAdmin | Admin email for error pages | admin@example.com |
Step 4: Enable the Site
On Debian/Ubuntu:
- Enable the site:
sudo a2ensite example.com.conf - Optionally disable the default site:
sudo a2dissite 000-default.conf - Test configuration syntax:
sudo apachectl configtest - Restart Apache:
sudo systemctl restart apache2
On RHEL-based systems, simply restart Apache after creating the configuration file: sudo systemctl restart httpd
Step 5: Configure DNS
Point your domain DNS A record to your server IP address. You can test locally before DNS propagation by editing /etc/hosts on your local machine.
Securing Virtual Hosts with SSL (Let's Encrypt)
Every website in 2026 must use HTTPS. The easiest way to add SSL:
- Install Certbot:
sudo apt install certbot python3-certbot-apache(Debian) orsudo dnf install certbot python3-certbot-apache(RHEL) - Generate certificates:
sudo certbot --apache -d example.com -d www.example.com - Certbot automatically creates SSL Virtual Host configuration and sets up auto-renewal
- Verify auto-renewal:
sudo certbot renew --dry-run
Advanced Virtual Host Configuration
Redirect HTTP to HTTPS
After installing SSL, redirect all HTTP traffic: add Redirect permanent / https://example.com/ to your port 80 Virtual Host.
Enable HTTP/2
- Enable the module:
sudo a2enmod http2 - Add
Protocols h2 h2c http/1.1to your SSL Virtual Host - Restart Apache
Virtual Host Types Comparison
| Scenario | Type | IP Requirement | SSL |
|---|---|---|---|
| Multiple sites, one IP | Name-based | 1 shared IP | Yes (SNI) |
| Isolated sites | IP-based | 1 IP per site | Yes |
| Wildcard subdomains | Name-based + *.domain | 1 shared IP | Yes (wildcard cert) |
| Reverse proxy to app | Name-based + ProxyPass | 1 shared IP | Yes |
Performance Tuning for Multiple Virtual Hosts
When hosting many sites on one server, performance tuning becomes critical:
- MPM Selection: Use
mpm_eventfor best performance with multiple sites. Check current MPM:apachectl -V | grep MPM - KeepAlive Settings: Enable
KeepAlive OnwithMaxKeepAliveRequests 100andKeepAliveTimeout 5 - Enable Compression:
sudo a2enmod deflateto compress text, HTML, CSS, JavaScript - Enable Caching: Use
mod_cacheandmod_expiresto set proper cache headers - Monitor Resources: Use
apachectl statusand check/var/log/apache2/error.logregularly
Frequently Asked Questions
Why is my Virtual Host showing a 403 Forbidden error?
A 403 Forbidden error is usually caused by incorrect file permissions or Apache not having access to the document root. Ensure the /var/www/ directory has 755 permissions, and that your Virtual Host includes a <Directory> block with Require all granted.
Can I use different IP addresses for each Virtual Host?
Yes, this is called IP-based Virtual Hosting. Assign multiple IPs to your interface and specify each in the VirtualHost directive. However, name-based hosting with SNI is preferred in 2026.
How many Virtual Hosts can Apache handle?
Apache can handle hundreds or thousands of Virtual Hosts. The limiting factor is server resources (RAM, CPU, disk I/O) rather than Apache's Virtual Host limit. Most shared hosts run 200-500 sites per server.
How do I test Virtual Hosts without changing DNS?
Edit /etc/hosts on your local machine: add YOUR.SERVER.IP example.com www.example.com. This overrides DNS for testing on your local machine only.
Related Resources
- Apache Fundamentals β Complete Apache guide
- Linux Web Server Setup β Full server setup
- AlmaLinux 9 + NGINX + PHP-FPM β Alternative web stack
- Browse all 205+ free IT cheat sheets