Apache2 VHost Configuration: How to Use Multiple ServerName Directives for a Single Virtual Host (wiki.lan & wiki Example)


2 views

# Apache2 multiple ServerName configuration example
<VirtualHost *:80>
    ServerName wiki.lan
    ServerAlias wiki
    DocumentRoot /var/www/wiki
    ErrorLog ${APACHE_LOG_DIR}/wiki_error.log
    CustomLog ${APACHE_LOG_DIR}/wiki_access.log combined
    
    <Directory "/var/www/wiki">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

In Apache HTTP Server, the ServerName directive specifies the primary hostname for the virtual host, while ServerAlias allows you to define additional names that should match this virtual host. This distinction is crucial when you need multiple domain names to resolve to the same content.

For internal networks using the .lan suffix (common in many organizations), you can configure both the FQDN and short name to work:


# Handling both FQDN and short name
ServerName wiki.lan
ServerAlias wiki *.wiki.lan

If you need to support wildcard subdomains while maintaining the primary ServerName setup:


# Wildcard configuration example
ServerName main.wiki.lan
ServerAlias *.wiki.lan wiki

When working with multiple ServerNames/ServerAliases:

  • Always specify one primary ServerName
  • Use ServerAlias for all variants
  • Keep the main ServerName as the canonical version
  • Consider SEO implications for public-facing sites

After making changes:


# Test configuration and restart Apache
sudo apache2ctl configtest
sudo systemctl restart apache2

In Apache HTTP Server configuration, the ServerName directive specifies the primary hostname for a VirtualHost, while ServerAlias allows you to define additional hostnames that should match the same VirtualHost. This is particularly useful for internal networks with multiple domain naming conventions.

For your internal wiki setup where you want to serve both wiki and wiki.lan, here's the proper VirtualHost configuration:



    ServerName wiki
    ServerAlias wiki.lan
    DocumentRoot /var/www/wiki
    
    # Additional configuration directives
    
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    
    
    ErrorLog ${APACHE_LOG_DIR}/wiki_error.log
    CustomLog ${APACHE_LOG_DIR}/wiki_access.log combined

When configuring multiple hostnames for a single VirtualHost:

  • Always specify one primary ServerName
  • Use ServerAlias for all additional hostnames
  • List the most frequently used hostname as ServerName
  • For wildcard domains, you can use patterns like *.example.lan

After making changes to your VirtualHost file:


sudo apache2ctl configtest
sudo systemctl reload apache2

Then test from command line with:


curl -I http://wiki
curl -I http://wiki.lan

For more complex setups, you might consider:



    ServerName wiki
    ServerAlias wiki.lan wiki.internal wiki-dev.lan
    ServerAlias *.wiki.lan
    
    # SSL configuration if needed
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/wiki.crt
    SSLCertificateKeyFile /etc/ssl/private/wiki.key
    
    # Rest of configuration...

  • Don't specify multiple ServerName directives - only the last one will be used
  • Ensure DNS resolution works for all hostnames
  • Check for syntax errors after configuration changes
  • Remember to include both HTTP and HTTPS VirtualHosts if using SSL