Apache virtual hosts allow you to host multiple websites on a single server. Here's a concise guide to configure them:

1. Basic Configuration

Use the NameVirtualHost directive to enable virtual hosting:

NameVirtualHost *:80

Then define virtual hosts in <VirtualHost> blocks:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example
</VirtualHost>

📌 Note: Replace example.com with your domain and adjust DocumentRoot accordingly.

2. Key Directives

  • ServerName: Specifies the domain name
  • DocumentRoot: Sets the root directory for the site
  • Directory: Configures access permissions
  • ErrorLog/CustomLog: Logs errors and requests

3. Example Setup

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/example
    <Directory /var/www/html/example>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

📸 Apache_Virtual_Host_Example

4. Best Practices

✅ Use unique ServerName for each host
✅ Organize configurations in separate files (e.g., 000-default.conf, example.com.conf)
✅ Test changes with apachectl configtest

For advanced configurations, check our Apache Virtual Hosts Configuration Guide.
📦 Apache_Server | 📝 Virtual_Host_Configuration