Virtual Hosts (vHosts) in Apache allow you to run multiple websites on a single server by defining separate configurations for each domain. Whether you’re hosting multiple domains or subdomains, knowing how to configure vHost Apache efficiently is essential for Linux administrators. This beginner-friendly guide will walk you through the process step by step.

Table of Contents
- What is a Virtual Host in Apache?
- Prerequisites to configure vHost Apache
- Step 1: Install Apache Web Server (If Not Already Installed)
- Step 2: Create Directory for Each Website
- Step 3: Create Virtual Host Configuration Files
- Step 4: Enable Virtual Host Files
- Step 5: Test Apache Configuration
- Step 6: Restart Apache
- Step 7: Update Your Local Hosts File (For Testing)
- Step 8: Verify Configuration
- Optional Step: Enable HTTPS with Let’s Encrypt
- Conclusion
What is a Virtual Host in Apache?
A Virtual Host allows you to host multiple websites on a single server by mapping specific domain names to unique document roots. This helps manage website configurations efficiently and is particularly useful for shared hosting environments.
Prerequisites to configure vHost Apache
Before configuring vHosts in Apache, ensure that:
- You have Apache installed on your Linux system. If already not installed Please follow this tutorial to install the apache. | How to Install Apache Web Server on Ubuntu (Step-by-Step Guide)
- You have sudo or root access.
- You have domain names pointed to your server’s IP address.
Step 1: Install Apache Web Server (If Not Already Installed)
To install Apache, run:
sudo apt update sudo apt install apache2 -y
Enable and start Apache:
sudo systemctl start apache2
sudo systemctl enable apache2
Step 2: Create Directory for Each Website
Create separate directories to store website files:
sudo mkdir -p /var/www/example1.com/public_html
sudo mkdir -p /var/www/example2.com/public_html
Set permissions and ownership for these directories:
sudo chown -R $USER:$USER /var/www/example1.com/public_html
sudo chown -R $USER:$USER /var/www/example2.com/public_html
sudo chmod -R 755 /var/www
Add sample content for testing:
echo "<h1>Welcome to Example1.com</h1>" | sudo tee /var/www/example1.com/public_html/index.html
echo "<h1>Welcome to Example2.com</h1>" | sudo tee /var/www/example2.com/public_html/index.html
Step 3: Create Virtual Host Configuration Files
Navigate to the Apache configuration directory:
cd /etc/apache2/sites-available
Create configuration files for each domain:
sudo vi example1.com.conf
Press i to go into insert mode then Add the following configuration:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example1.com
ServerAlias www.example1.com
DocumentRoot /var/www/example1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file (Press Esc to exit from insert mode and save and exit from editor by :wq! then Enter).
Repeat for the second domain:
sudo vi example2.com.conf
Add the corresponding configuration:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example2.com
ServerAlias www.example2.com
DocumentRoot /var/www/example2.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Step 4: Enable Virtual Host Files
Enable the configuration files using the a2ensite
command:
sudo a2ensite example1.com.conf
sudo a2ensite example2.com.conf
Disable the default configuration (optional):
sudo a2dissite 000-default.conf
Step 5: Test Apache Configuration
Check for syntax errors:
sudo apache2ctl configtest
or
sudo apache2ctl -t
You should see:
Syntax OK
Step 6: Restart Apache
Apply the changes by restarting Apache:
sudo systemctl restart apache2
Step 7: Update Your Local Hosts File (For Testing)
If your domain names are not yet live, map them to your server’s IP in your local machine’s hosts file:
sudo nano /etc/hosts
Add the following lines:
your-server-ip example1.com www.example1.com
your-server-ip example2.com www.example2.com
Save and close the file.
Step 8: Verify Configuration
Open a web browser and navigate to:
http://example1.com
http://example2.com
You should see the sample content for each site.
Optional Step: Enable HTTPS with Let’s Encrypt
To secure your websites with SSL, install Certbot:
sudo apt install certbot python3-certbot-apache -y
Obtain and install SSL certificates:
sudo certbot --apache
Follow the prompts to configure HTTPS for your domains.
Read Also | Top 11 Open Source Hosting Control Panels for Effective Server Management
Conclusion
Configuring Virtual Hosts in Apache is an essential skill for hosting multiple websites on a single server. By following this guide, you’ve learned how to configure vHost Apache in Linux, from setting up directories and creating configuration files to enabling and testing virtual hosts.
With this setup, you can efficiently manage multiple domains, improve organization, and optimize server performance. For enhanced security, consider enabling HTTPS with Let’s Encrypt.
Keep exploring more Apache configurations and server optimizations to enhance your hosting experience. Stay tuned to Magnetbyte for more in-depth tutorials and expert tech insights!