How to Fix Apache2 “mixing * ports and non-* ports with NameVirtualHost” Configuration Error


1 views

When setting up multiple virtual hosts in Apache, you might encounter this frustrating error:

[error] VirtualHost *:80 -- mixing * ports and non-* ports with a NameVirtualHost address is not supported
[warn] NameVirtualHost *:80 has no VirtualHosts

This error typically occurs when there's a mismatch between your NameVirtualHost declaration and your VirtualHost directives. The key problems are:

  • Inconsistent port declarations between NameVirtualHost and VirtualHost blocks
  • Missing or incorrect ServerName directives
  • Conflicting configuration files loading in wrong order

Here's the proper way to configure named virtual hosts in Apache:

# In your main apache2.conf or ports.conf
NameVirtualHost *:80
Listen 80

# For each virtual host configuration file

    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined

1. Port Specification Mismatch:

# Wrong:
NameVirtualHost *


# Correct:
NameVirtualHost *:80

2. Multiple Declaration Issues:

# Check for duplicate declarations:
grep -r "NameVirtualHost" /etc/apache2/

3. Configuration File Loading Order:

# Ensure virtual hosts are loaded after main configuration
ls /etc/apache2/sites-enabled/

After making changes:

sudo apache2ctl configtest
sudo systemctl restart apache2

To verify your virtual hosts are properly recognized:

apache2ctl -S

For more complex setups with SSL and multiple ports:

NameVirtualHost *:80
NameVirtualHost *:443


    ServerName example.com
    # Redirect to HTTPS
    Redirect permanent / https://example.com/



    ServerName example.com
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/example.com.crt
    SSLCertificateKeyFile /etc/ssl/private/example.com.key
    DocumentRoot /var/www/example.com/secure_html


When working with Apache's virtual hosts, you might encounter this frustrating error during server startup:

[error] VirtualHost *:80 -- mixing * ports and non-* ports with a NameVirtualHost address is not supported
[warn] NameVirtualHost *:80 has no VirtualHosts

This typically occurs when there's a mismatch between your NameVirtualHost declaration and your VirtualHost configurations. Let me walk through the proper way to configure this.

First, let's clarify the essential components:

# In ports.conf or httpd.conf
NameVirtualHost *:80
Listen 80

Then for each virtual host:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example
    # Other directives...
</VirtualHost>

The error often stems from these scenarios:

# WRONG: Missing port specification
NameVirtualHost *

# WRONG: Mismatched port declaration
NameVirtualHost *:80
<VirtualHost 192.168.1.1:80>
    # ...
</VirtualHost>

Here's a proper setup for multiple sites:

# In /etc/apache2/ports.conf
NameVirtualHost *:80
Listen 80

# In /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@test.com
    ServerName test.com
    DocumentRoot /var/www/test.com/public_html
    # Additional configurations...
</VirtualHost>

After making changes:

  1. Run apache2ctl configtest to check syntax
  2. Use apache2ctl -S to view virtual host configuration
  3. Check error logs with tail -f /var/log/apache2/error.log

For complex setups with SSL:

NameVirtualHost *:443
<VirtualHost *:443>
    ServerName secure.example.com
    SSLEngine on
    SSLCertificateFile /path/to/cert.pem
    # SSL configurations...
</VirtualHost>

Remember to properly enable sites with a2ensite and restart Apache after configuration changes.