Issues that happen when you change the hostname and reverse DNS (Part 2)

In the first part of this blog post series I discussed the effect that can happen when forgetting to edit a Postfix configuration after changing the hostname and reverse DNS.

In this article I will describe another effect that you might experience with Apache.

Apache redirecting a domain to the default vhost configuration

Apache continues to work normal after your hostname and reverse DNS change, as long as it’s not being restarted or reloading its configuration.

As soon as you enable a new site or must reload apache’s configuration for another reason, you will experience that apache is misbehaving. Specifically this happens if a vhost exists that has a server name that exactly matches your hostname. This leads to apache redirecting a domain to the default vhost configuration.

With apache2ctl (or in some linux systems, it’s just apachectl) you can list your current configuration.

# apache2ctl -S
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server newhost.slopjong.de (/etc/apache2/sites-enabled/000-default:2)
         port 80 namevhost newhost.slopjong.de (/etc/apache2/sites-enabled/000-default:2)
         port 80 namevhost newhost.slopjong.de (/etc/apache2/sites-enabled/newhost.slopjong.de:4)

You might wonder about what’s going on here especially because you probably didn’t change the default configuration after your Linux system has been installed. In this case the default vhost configuration looked as follows.

# cat /etc/apache2/sites-enabled/default
<VirtualHost *:80>
	ServerAdmin webmaster@localhost

	DocumentRoot /srv/http
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /srv/http/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log
	LogLevel warn
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

By quickly comparing this config with others, one sees that the ServerName is missing. The apache manual says that …

… if the first VirtualHost block does not include a ServerName directive, the reverse DNS of the relevant IP will be used instead. If this is not the server name you wish to use, a bogus entry (ServerName none.example.com) can be added to get around this behaviour.

Briefly, the first vhost configuration file that has always been loaded first was your default vhost while your reverse DNS was used as its server name.

After figuring this out the fix is as easy as adding an entry such as

ServerName *

and reloading the configuration files.

# /etc/init.d/apache2 reload

When we check the loaded configuration again we’ll see that the issue has gone.

# apache2ctl -S
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server * (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost * (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost newhost.slopjong.de (/etc/apache2/sites-enabled/new.slopjong.de:4)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>