How to Fix Apache Virtual Host Conflict Error
In this tutorial, you'll learn about How to Fix Apache Virtual Host Conflict Error. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Apache serves the wrong website for a domain or shows the default site instead of the configured virtual host — the virtual host ordering is incorrect or multiple virtual hosts match the same ServerName.
The Problem
$ curl -H "Host: example.com" http://localhost
# Returns content from a different site or the default Apache page
Step-by-Step Fix
Step 1: Check virtual host ordering
# /etc/apache2/sites-available/000-default.conf
# The first virtual host for a port becomes the default
# Place your main site first
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example
</VirtualHost>
Step 2: Use explicit ServerName and ServerAlias
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example
<Directory /var/www/example>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Step 3: Disable conflicting sites
sudo a2dissite 000-default.conf
sudo a2ensite example.com.conf
sudo systemctl reload apache2
Step 4: Check enabled sites
ls -la /etc/apache2/sites-enabled/
Expected: Only the sites you want enabled.
Step 5: Test name-based virtual hosting
apachectl -S
Expected:
VirtualHost configuration:
*:80 example.com (/etc/apache2/sites-enabled/example.com.conf:1)
*:80 other.com (/etc/apache2/sites-enabled/other.com.conf:5)
Prevention Tips
- Always set explicit
ServerNameon every virtual host - Disable the default site (
000-default.conf) in production - Use
apachectl -Sto verify virtual host configuration - Keep one virtual host per file for clarity
Common Mistakes with vhost conflict
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world APACHE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro