🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now β†’
Menu

Categories

Apache Virtual Hosts: Hosting Multiple Websites on One Linux Server (2026)

Apache Virtual Hosts: Hosting Multiple Websites on One Linux Server (2026)

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.

Apache Virtual Hosts hosting multiple websites on one server

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 Host header 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:

  1. Create the site directory: sudo mkdir -p /var/www/example.com/public_html
  2. Create directories for logs: sudo mkdir -p /var/www/example.com/logs
  3. Set ownership: sudo chown -R $USER:$USER /var/www/example.com/public_html
  4. 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

DirectivePurposeExample Value
ServerNamePrimary domain for this Virtual Hostexample.com
ServerAliasAdditional domains (www subdomain)www.example.com
DocumentRootDirectory containing website files/var/www/example.com/public_html
ErrorLogPath to error log file/var/www/example.com/logs/error.log
CustomLogPath to access log file/var/www/example.com/logs/access.log combined
ServerAdminAdmin email for error pagesadmin@example.com

Step 4: Enable the Site

On Debian/Ubuntu:

  1. Enable the site: sudo a2ensite example.com.conf
  2. Optionally disable the default site: sudo a2dissite 000-default.conf
  3. Test configuration syntax: sudo apachectl configtest
  4. 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:

  1. Install Certbot: sudo apt install certbot python3-certbot-apache (Debian) or sudo dnf install certbot python3-certbot-apache (RHEL)
  2. Generate certificates: sudo certbot --apache -d example.com -d www.example.com
  3. Certbot automatically creates SSL Virtual Host configuration and sets up auto-renewal
  4. 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

  1. Enable the module: sudo a2enmod http2
  2. Add Protocols h2 h2c http/1.1 to your SSL Virtual Host
  3. Restart Apache

Virtual Host Types Comparison

ScenarioTypeIP RequirementSSL
Multiple sites, one IPName-based1 shared IPYes (SNI)
Isolated sitesIP-based1 IP per siteYes
Wildcard subdomainsName-based + *.domain1 shared IPYes (wildcard cert)
Reverse proxy to appName-based + ProxyPass1 shared IPYes

Performance Tuning for Multiple Virtual Hosts

When hosting many sites on one server, performance tuning becomes critical:

  • MPM Selection: Use mpm_event for best performance with multiple sites. Check current MPM: apachectl -V | grep MPM
  • KeepAlive Settings: Enable KeepAlive On with MaxKeepAliveRequests 100 and KeepAliveTimeout 5
  • Enable Compression: sudo a2enmod deflate to compress text, HTML, CSS, JavaScript
  • Enable Caching: Use mod_cache and mod_expires to set proper cache headers
  • Monitor Resources: Use apachectl status and check /var/log/apache2/error.log regularly

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

Share this article:
Dargslan Editorial Team (Dargslan)
About the Author

Dargslan Editorial Team (Dargslan)

Collective of Software Developers, System Administrators, DevOps Engineers, and IT Authors

Dargslan is an independent technology publishing collective formed by experienced software developers, system administrators, and IT specialists.

The Dargslan editorial team works collaboratively to create practical, hands-on technology books focused on real-world use cases. Each publication is developed, reviewed, and...

Programming Languages Linux Administration Web Development Cybersecurity Networking

Stay Updated

Subscribe to our newsletter for the latest tutorials, tips, and exclusive offers.