English translation
Step 7: Configuring Virtual Hosts in Apache2
In the previous article, we explored the fundamentals of Apache2’s core configuration files and modified them. We introduced the basic concepts of httpd.conf or apache2.conf. Now, we’ll guide you through configuring Apache2 virtual hosts—enabling multiple websites to run simultaneously on a single server. This process involves creating separate configurations for each site, ensuring they operate independently.
What Is a Virtual Host?
A virtual host refers to the configuration of Apache2 on a single physical server so that it can serve multiple websites concurrently. In Apache, virtual hosts can be distinguished by IP address, domain name, or port number. The most common approach is name-based virtual hosting—where sites are differentiated solely by their domain names.
Advantages of Virtual Hosting
- Resource Sharing: Multiple websites share the same server resources, reducing infrastructure costs.
- Centralized Management: Simplifies administration of multiple sites from a unified environment.
- Flexibility: Enables per-site customization—for example, individual SSL settings, logging configurations, or access controls.
Configuring Virtual Hosts
First, ensure Apache2 is installed and running. Next, we’ll walk through setting up two sample virtual hosts. Assume your domains are example1.com and example2.com.
1. Create Directory Structure
Begin by creating the document root directories:
sudo mkdir -p /var/www/example1.com/public_html
sudo mkdir -p /var/www/example2.com/public_html
Then create simple test index pages:
echo "<h1>Welcome to Example1!</h1>" | sudo tee /var/www/example1.com/public_html/index.html
echo "<h1>Welcome to Example2!</h1>" | sudo tee /var/www/example2.com/public_html/index.html
2. Set File Permissions
Grant appropriate ownership and permissions so Apache can serve the content:
sudo chown -R www-data:www-data /var/www/example1.com/public_html
sudo chown -R www-data:www-data /var/www/example2.com/public_html
sudo chmod -R 755 /var/www
3. Configure Virtual Host Files
Create two virtual host configuration files in /etc/apache2/sites-available/:
sudo nano /etc/apache2/sites-available/example1.com.conf
Paste the following into example1.com.conf:
<VirtualHost *:80>
ServerAdmin webmaster@example1.com
ServerName example1.com
ServerAlias www.example1.com
DocumentRoot /var/www/example1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error_example1.log
CustomLog ${APACHE_LOG_DIR}/access_example1.log combined
</VirtualHost>
Similarly, create example2.com.conf:
sudo nano /etc/apache2/sites-available/example2.com.conf
And populate it with:
<VirtualHost *:80>
ServerAdmin webmaster@example2.com
ServerName example2.com
ServerAlias www.example2.com
DocumentRoot /var/www/example2.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error_example2.log
CustomLog ${APACHE_LOG_DIR}/access_example2.log combined
</VirtualHost>
4. Enable the Virtual Hosts
Activate both configurations using the a2ensite utility:
sudo a2ensite example1.com.conf
sudo a2ensite example2.com.conf
5. Restart Apache2
Apply the changes by restarting the service:
sudo systemctl restart apache2
6. Update /etc/hosts (Optional, for Local Testing)
To test locally without DNS setup, edit /etc/hosts:
sudo nano /etc/hosts
Add these lines:
127.0.0.1 example1.com
127.0.0.1 example2.com
7. Test the Virtual Hosts
Open your browser and navigate to http://example1.com and http://example2.com. You should see the respective welcome pages. Verify functionality by confirming that each domain loads its own content correctly.
Summary
At this stage, you’ve successfully configured two virtual hosts on your Apache2 server—each with its own DocumentRoot, ErrorLog, and CustomLog. In the next article, we’ll explore how to enable and disable Apache modules to extend or restrict server capabilities.
By completing this tutorial, you now have a functional multi-site environment—maximizing resource efficiency and scalability. Stay tuned for upcoming articles in this series, where we’ll dive deeper into advanced Apache2 configurations.
Continue