Apache 2 Warning Fix: “NameVirtualHost *:80 has no VirtualHosts” Explained and Resolved


2 views

When working with Apache 2 on Ubuntu, you might encounter this startup warning:

[warn] NameVirtualHost *:80 has no VirtualHosts

This occurs when Apache's configuration declares NameVirtualHost but lacks corresponding VirtualHost blocks for the specified port.

Your current setup has two potential issues:

<VirtualHost *>
    # Configuration here
</VirtualHost>

1. The wildcard * should specify a port (*:80 for HTTP)
2. NameVirtualHost directive (now deprecated in Apache 2.4) needs proper VirtualHost pairing

For Apache 2.4+, use this cleaner approach:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    
    <Location /mysite>
        # Your configuration
    </Location>
    
    <LocationMatch "/mysite/login">
        AuthType Basic
        AuthName "My Site"
        AuthUserFile /etc/sitepasswords/passwd
        Require valid-user
    </LocationMatch>
</VirtualHost>

If you must support older configurations:

NameVirtualHost *:80

<VirtualHost *:80>
    # Your configuration
</VirtualHost>

The <Location> blocks in your configuration are actually valid. The warning specifically relates to the VirtualHost declaration. Location directives are processed after the main VirtualHost context is established.

Here's a fully functional configuration that won't produce warnings:

<VirtualHost *:80>
    ServerName mysite.example.com
    DocumentRoot /var/www/mysite
    
    ErrorLog ${APACHE_LOG_DIR}/mysite_error.log
    CustomLog ${APACHE_LOG_DIR}/mysite_access.log combined
    
    <Directory /var/www/mysite>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    <Location /protected-area>
        AuthType Basic
        AuthName "Restricted Area"
        AuthUserFile /etc/apache2/.htpasswd
        Require valid-user
    </Location>
</VirtualHost>

After making changes:

sudo apache2ctl configtest
sudo systemctl restart apache2

The warning should disappear if you've properly configured at least one VirtualHost for port 80.


When you see the warning [warn] NameVirtualHost *:80 has no VirtualHosts during Apache startup, it indicates a configuration mismatch between your NameVirtualHost directive and actual VirtualHost blocks. While the server continues to function, this warning signals improper virtual host setup that could lead to unexpected behavior.

In Apache 2.4 (which Ubuntu typically uses), the NameVirtualHost directive is deprecated. The warning appears when:

  1. You have NameVirtualHost *:80 enabled (often in ports.conf)
  2. But no matching <VirtualHost *:80> blocks exist
  3. Or your VirtualHost declaration doesn't match the IP:port pattern

Your current configuration uses <VirtualHost *> which is too generic. For proper name-based virtual hosting, specify the port:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    
    <Location /mysite>
        # Configuration details here...
    </Location>

    <LocationMatch "/mysite/login">
        AuthType Basic
        AuthName "My Site"
        AuthUserFile /etc/sitepasswords/passwd
        Require valid-user
    </LocationMatch>
</VirtualHost>

For Apache 2.4+ configurations:

  1. Remove any NameVirtualHost directives (deprecated in 2.4)
  2. Ensure all VirtualHost blocks explicitly declare *:80 or specific IPs
  3. Include ServerName directives in each VirtualHost

While <Location> directives aren't directly causing this warning, they should typically be used within VirtualHost contexts. Consider this improved structure:

<VirtualHost *:80>
    ServerName api.example.com
    DocumentRoot /var/www/api
    
    <Directory "/var/www/api/mysite">
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    <LocationMatch "^/mysite/login">
        AuthType Basic
        AuthName "Restricted API"
        AuthBasicProvider file
        AuthUserFile /etc/apache2/.htpasswd
        Require valid-user
    </LocationMatch>
</VirtualHost>

After making changes:

  1. Run apachectl configtest to check syntax
  2. Look for remaining warnings with apachectl -S
  3. Monitor error logs: tail -f /var/log/apache2/error.log

For servers hosting multiple sites:

# /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example
    # Additional directives...
</VirtualHost>

# /etc/apache2/sites-available/test.com.conf
<VirtualHost *:80>
    ServerName test.com
    DocumentRoot /var/www/test
    # Additional directives...
</VirtualHost>

Remember to enable sites with a2ensite and reload Apache.