When configuring multiple VirtualHosts in Apache on the same port (typically port 80), you might encounter this warning:
[warn] _default_ VirtualHost overlap on port 80, the first has precedence
This occurs when Apache can't properly distinguish between different virtual hosts listening on the same IP-port combination. The symptom you're seeing - where all domains route to the first VirtualHost - confirms the configuration isn't working as intended.
The solution you found (uncommenting NameVirtualHost *:80
) is correct for older Apache versions (2.2.x and earlier). However, in newer Apache versions (2.4.x), this directive is deprecated as name-based virtual hosting is enabled by default.
Your configuration appears correct, but let's examine a complete working example:
# Ensure this is in your main apache config (httpd.conf or apache2.conf)
Listen 80
<VirtualHost *:80>
DocumentRoot /var/www/html/site1
ServerName www.site1.com
ServerAlias site1.com
# Additional directives...
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/site2
ServerName www.site2.com
ServerAlias site2.com
# Additional directives...
</VirtualHost>
After making changes, always:
- Check configuration syntax:
apachectl configtest
- Restart Apache:
systemctl restart apache2
(orservice httpd restart
on CentOS) - Verify using:
curl -v http://www.site1.com
andcurl -v http://www.site2.com
If domains still route to the wrong site:
- Clear browser cache or use incognito mode
- Check
/etc/hosts
file if testing locally - Verify DNS records point to your server
- Ensure no conflicting .htaccess files
For CentOS 5 specifically, you might need additional steps:
# In /etc/httpd/conf/httpd.conf
NameVirtualHost *:80
# Then in /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80>
DocumentRoot /var/www/html/site1
ServerName www.site1.com
ServerAlias site1.com
ErrorLog /var/log/httpd/site1_error.log
CustomLog /var/log/httpd/site1_access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/site2
ServerName www.site2.com
ServerAlias site2.com
ErrorLog /var/log/httpd/site2_error.log
CustomLog /var/log/httpd/site2_access.log combined
</VirtualHost>
For production environments:
- Consider using separate IP addresses for critical sites
- Implement SSL certificates (which naturally separate hosts via SNI)
- Set up proper logging for each virtual host
- Use
<Directory>
directives for security
When configuring multiple VirtualHosts in Apache, you might encounter the warning:
[warn] _default_ VirtualHost overlap on port 80, the first has precedence
This typically occurs when:
- Multiple VirtualHosts are defined for the same IP/port combination
- The NameVirtualHost directive isn't properly configured
- There's no default VirtualHost catch-all
Here's the proper way to set up multiple VirtualHosts on port 80:
# Essential directive for name-based virtual hosting
NameVirtualHost *:80
# Default catch-all VirtualHost (should come FIRST)
DocumentRoot /var/www/default
ServerName default.example.com
# First site configuration
ServerName www.site1.com
ServerAlias site1.com
DocumentRoot /var/www/html/site1
ErrorLog ${APACHE_LOG_DIR}/site1_error.log
CustomLog ${APACHE_LOG_DIR}/site1_access.log combined
# Additional directives
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
# Second site configuration
ServerName www.site2.com
ServerAlias site2.com
DocumentRoot /var/www/html/site2
ErrorLog ${APACHE_LOG_DIR}/site2_error.log
CustomLog ${APACHE_LOG_DIR}/site2_access.log combined
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
- The NameVirtualHost *:80 directive must appear before any VirtualHost blocks
- The first VirtualHost block acts as the default for requests that don't match any ServerName/ServerAlias
- Each VirtualHost must have a unique ServerName (ServerAlias can be shared if needed)
- Always restart Apache after configuration changes:
sudo systemctl restart apache2
If you're still experiencing issues, try these diagnostic commands:
# Check Apache configuration syntax
apachectl configtest
# View loaded VirtualHost configurations
apachectl -S
# Verify name resolution
nslookup www.site1.com
nslookup www.site2.com
For production environments, consider these additional measures:
- Implement separate log files for each VirtualHost
- Set up proper directory permissions
- Configure SSL certificates for HTTPS support
- Use
directives for modular configurations