English translation
Step 7: Configuring Virtual Hosts in Apache2
AI Article Decision Snapshot
Turn the lesson into workflow, model, budget, and security checks before choosing tools.
Use this quick snapshot before leaving the article. It keeps the next search tied to practical AI software, model/API, cost, privacy, and implementation questions.
Workflow fit
Identify the real job behind the article: coding, research, document review, support, analytics, content, or internal automation.
Model or tool decision
Decide whether the next step is a software shortlist, an AI tool comparison, an API platform choice, or a model benchmark.
Budget and usage signal
Estimate seats, API calls, prompt volume, retries, review time, and fallback work before assuming the workflow is cheap.
Security and privacy review
Check whether source code, customer data, private documents, prompts, logs, or embeddings will enter the AI workflow.
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.
Apply This Lesson
Turn this article into AI software, model, API, and security decisions.
English Article FAQ
Use this article as evidence before choosing AI tools
How should I use this AI Tutorials article?
Use it as the implementation or learning layer, then connect the idea to AI software buyer guides, tool comparisons, benchmarks, API choices, and security checks before making a production decision.
Is this English article different from the Chinese original?
The English edition is localized for global AI readers while preserving the original diagrams, screenshots, prompts, code examples, and source context from the Chinese article.
What should I read after Step 7: Configuring Virtual Hosts in Apache2?
Continue with AI Software Buyer Guides, AI Tools Workbench, Best AI Coding Agents, AI Model Benchmarks, OpenAI vs Anthropic API, or LLM Security Tools depending on the decision you need to make.
Can this article alone choose an AI product or model?
No. Treat the article as evidence and context, then validate fit with pricing, privacy requirements, integration effort, benchmark results, workflow tests, and fallback planning.
Continue