The Apache “Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName” Foo on Ubuntu

If you are using Apache on Ubuntu, then you most likely ran into the annoying warning about determining the fully qualified domain name of your server. Something like this will show up in your logs, on start of the server or on log rotation:

apache2: Could not reliably determine the server's fully qualified domain name, using 10.10.0.40 for ServerName

Luckily there is an easy fix it. All you have to do is adding the ServerName directive to your Apache configuration and specify a good server name. Usually localhost will do fine, unless you are using the machine without virtual hosts, just as a base server.

A quick and dirty way would be adding the following line to /etc/apache2/httpd.conf or to /etc/apache2/apache2.conf and restart your Apache service.

ServerName localhost

And then restart your Apache service with the following command:

/etc/init.d/apache2 restart

Or on newer systems with:

service apache2 restart

Now, if you have a name in /etc/hostname, then you can use that instead. But you have to make sure that the name resolves to something real. If it is not in the DNS, then add it to your /etc/hosts and let it resolve to another loopback address. Here is a sample:

127.0.0.1 localhost
127.0.1.1 myhostname

But as I said, this is quick and dirty and there are much better ways of doing it. So lets take a look at the preferred method. This one differs depending on the Ubuntu version, because the default Apache version changed from 2.2 to 2.4.

Ubuntu 13.04 and older (Apache 2.2)

Apache 2.2 loads additional configuration files from /etc/apache2/conf.d. Just add a file to that directory and add the above mentioned ServerName directive and restart Apache.

Create the file:
vi /etc/apache2/conf.d/servername

Add the directive:
ServerName localhost

Restart Apache:
service apache2 restart
or
/etc/init.d/apache2 restart

Ubuntu 13.10 and newer (Apache 2.4)

Apache 2.4 has the additional configuration organized similar to the modules and sites. All configuration files need to be added to the directory /etc/apache2/conf-available and need to have the extension .conf. Each configuration file can be enabled with a2enconf and disabled with a2disconf.

Create the file:
vi /etc/apache2/conf-available/servername.conf

Add the directive:
ServerName localhost

Activate the configuration:
a2enconf servername

Restart Apache:
service apache2 restart

Apache Doesn’t Show Protected Folders… Foo

After setting up a nice download area with password protection I ran into an interesting issue. And I don’t know why I haven’t encountered that problem before. It’s not that I never set up Apache configurations or have never heard of mod_auth and mod_autoindex.

But now to the problem itself. I had a set up for a download area and added a sub-folder that needed password protection. Here is the configuration:

<Directory /srv/www/myroot/>
         Options Indexes FollowSymLinks MultiViews
         AllowOverride None
         Order allow,deny
         Allow from all
</Directory>
<Directory /srv/www/myroot/downloads/protected/>
         Options Indexes FollowSymLinks MultiViews
         AllowOverride None
         Order allow,deny
         Allow from all
         AuthType Basic
         AuthName "Download area for Foo Bar"
         AuthUserFile /etc/apache2/private/htpasswd
         Require user me myself
</Directory>

Looks good, doesn’t it? Well, not 100% good. The folder protected didn’t show up in the directory index. After some additional coffee, a couple of face palms and a quick browse through Google and the Apache documentation I found the culprit. The setting ShowForbidden in the options for mod_autoindex. If it is not set everything that is forbidden or needs authentication will not show up in the directory index. Here is the tweaked version of the configuration (change in bold):

<Directory /srv/www/myroot/>
         Options Indexes FollowSymLinks MultiViews
         IndexOptions +ShowForbidden
         AllowOverride None
         Order allow,deny
         Allow from all
</Directory>
<Directory /srv/www/myroot/downloads/protected/>
         Options Indexes FollowSymLinks MultiViews
         AllowOverride None
         Order allow,deny
         Allow from all
         AuthType Basic
         AuthName "Download area for Foo Bar"
         AuthUserFile /etc/apache2/private/htpasswd
         Require user me myself
</Directory>