516

When I restart my Apache server using the command

sudo /etc/init.d/apache2 restart

I get the following error:

Restarting web server apache2
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
... waiting apache2:
Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName

Is the server using 127.0.1.1 instead of 127.0.0.1? What causes this error?

Deepu
  • 5,307

14 Answers14

619

13.04 and older

This is just a friendly warning and not really a problem (as in that something does not work).

If you go to:

/etc/apache2/apache2.conf

and insert:

ServerName localhost   

and then restart apache by typing into the terminal:

sudo systemctl reload apache2

the notice will disappear.

If you have a name inside /etc/hostname you can also use that name instead of localhost.


And it uses 127.0.1.1 if it is inside your /etc/hosts:

127.0.0.1 localhost
127.0.1.1 myhostname

Preferred method

Troubleshooting Apache

If you get this error:

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

then use a text editor such as "sudo nano" at the command line or "gksudo gedit" on the desktop to create a new file,

sudo nano /etc/apache2/conf.d/fqdn

or

gksu "gedit /etc/apache2/conf.d/fqdn"

then add

ServerName localhost

to the file and save. This can all be done in a single command with the following:

 echo "ServerName localhost" | sudo tee /etc/apache2/conf.d/fqdn

But on Ubuntu 14.04:

 echo "ServerName localhost" | sudo tee /etc/apache2/conf-available/fqdn.conf
 sudo a2enconf fqdn

Don't forget the ".conf" (without will not work).

Dan
  • 13,119
Rinzwind
  • 299,756
  • 3
    This is also mentioned in the Ubuntu LAMP setup documentation for Troubleshooting Apache. There they recommend creating a file /etc/apache2/conf.d/fqdn with ServerName localhost in it – icc97 Jun 29 '13 at 09:36
  • Very good answer - but I would also recommend putting it in the conf.d directory :) – cwd Sep 26 '13 at 22:42
  • 3
    /etc/apache2/conf.d/ does not exist after my install of lamp-server^ in 14.04 – redanimalwar May 03 '14 at 07:51
  • @redanimalwar For 13.10 and newer see @Starx's answer below. – Dan May 07 '14 at 07:21
  • 3
    For the very last step (creating sym-link in 14.04) I'd recommend sudo a2enconf fqdn though it should technically be the same. – nuala Jun 18 '14 at 15:04
  • @Rinzwind you explained how to fix the issue, but why is it occurring? – JohnMerlino Jul 26 '14 at 05:54
  • @JohnMerlino why? because it is part of setting apache up?! – Rinzwind Jul 26 '14 at 07:50
  • Why not configure the server FQDN in /etc/hosts ? – HorusKol Sep 11 '14 at 07:29
  • @HorusKol because it is no longer the preferred method. I would have added it in later but areeda already covered that method (and I would be stealing her answer). – Rinzwind Sep 11 '14 at 07:39
  • @Rinzwind Why is it not the preferred method? Wouldn't it be better to set this at a server configuration level rather than at the apache service configuration? – HorusKol Sep 11 '14 at 07:57
  • @Rinzwind i did this works to remove that warning, but after restart apache i see fail in terminal log. and i restart my system but nothing changed and again after restarting apache service apache2 restart , i see fail ! – Netmoon Oct 29 '14 at 15:14
  • i did get a 'cant bind to 0.0.0.0:80' error after trying this,,, but after i did a sudo killall apache2, then sudo service restart apache2, this worked. Thanks! – don bright Feb 26 '15 at 02:39
  • The first part (adding the information on the apache2.conf) worked for me in the Ubuntu 14.04 – Dennis Braga Jul 23 '15 at 13:34
  • ubuntu 10.04 + apache 2.2 do not have the fqdn file. How to solve this? – petertc Jan 15 '16 at 07:26
  • Upgrade your system since 10.04 is no longer an accepted Ubuntu. But .... I would assume the 1st part of the answer should work op Apache 2.2(?) – Rinzwind Jan 15 '16 at 08:47
  • Re "And it uses 127.0.1.1 if it is inside your /etc/hosts"; Is this only for ubuntu? Or are there other OS that do this too? – Pacerier Apr 11 '16 at 19:36
  • On Ubuntu 14.04 I edited existing file /etc/apache2/apache2.conf to add ServerName localhost and then did sudo service apache2 restart and it worked - no more warning. Before doing that, editing /etc/apache2/httpd.conf had no effect (I confirmed by removing the file in the line after adding it to apache2.conf and restarting the server) – therobyouknow Jan 15 '17 at 21:06
  • I confirmed the "But on Ubuntu 14.04" also worked on Ubuntu 16.04.1 LTS, none of the others worked. Warning went away. – JGlass Feb 07 '18 at 00:19
  • To emphasise - if you're on a remote server that has some default host, it will be something like ServerName example.com – jave.web Jan 19 '24 at 23:59
197

13.10 or newer

As of Apache 2.4 - which is available by default as of 13.10 - you cannot use the method about adding a config file in the conf.d directory.

Apache no longer uses the conf.d directory. All the configuration files are stored inside /etc/apache2/conf-available directory and all the configuration files should now have a .conf extension.

In order to solve this message in Apache 2.4, we have to create a configuration file inside the conf-available directory. For example servername.conf.

sudo vi /etc/apache2/conf-available/servername.conf

And inside this we just need to add one line

ServerName localhost

You can combine the previous two commands in one with:

echo "ServerName localhost" | sudo tee /etc/apache2/conf-available/servername.conf

If you want to use a domain name or any other name depending on the requirement its fine, just replace localhost with whatever you need. Next you have to enable this configuration. For this, you need to run the following command:

sudo a2enconf servername

a2enconf is a command to enable a configuration file in Apaches 2.4. Also note that servername on the above command is from the name of the configuration file servername.conf. If your configuration file was ngenericserver.conf then you would have to write sudo a2enconf ngenericserver.

After this reload the server and the above message will no longer bug you.

sudo service apache2 reload

or

sudo apache2ctl graceful

Now after this you will see that the message will not be shown again and the problem will be fixed.

Starx
  • 5,263
  • 4
    Does anybody know how to make Apache say which config file it's having the problem with? I don't know why developers never think to put this in their error messages - it hardly takes much code! – John Y Jul 08 '14 at 10:08
  • If I have an Internet fqdm for this server, is this the place to put it? ie should I substitude myhost.org in place of localhost? – CPBL Aug 14 '14 at 16:54
  • 1
    @CPBL This will be the default server name for all sites. If you only have one site, then that's all you need to do. If you have multiple sites on your server, you will also need to add the servername directive for each site in their respective virtual host configuration. – Dan Oct 07 '14 at 17:12
  • 1
    Works on 14.04 LTS. – Parto Jun 29 '15 at 06:34
  • 1
    Your quoted link seems dead, please fix. – Fredrick Gauss Aug 27 '15 at 07:56
  • This helped me, Thanks! – Danniel Little Jan 15 '20 at 20:20
  • where are the a2* scripts such as a2enconf when building from source? – Brian Wiley Dec 14 '20 at 22:08
  • I found here if you are building from source you have to edit the PREFIX/conf/httpd.conf where PREFIX is where you installed it and edit the servername variable – Brian Wiley Dec 14 '20 at 22:39
37

Apache2 can also get the FQDN from a properly configured system hostname rather than hardcoded into the apache config file. Hardcoding it will also cause confusion if the hostname changes. You actually don't need any ServerName directives in httpd.conf or any apache config files.

Put the following in /etc/hosts:

# IPv4 and IPv6 localhost aliases
127.0.0.1 hostname.domainname.com  hostname  localhost
::1       hostname.domainname.com  hostname  localhost

where hostname.domainname.com is the FQDN of your machine.

Along with a properly configured hostname in /etc/hostname/ or with hostnamectl, this will also help other services on your machine run properly (i.e. the login prompt, showing This is hostname.domainname.com instead of This is hostname.unknown_domain.)

Andrew Mao
  • 1,721
  • 1
  • 18
  • 19
  • 6
    +1 for "a properly configured system hostname". I wanted to configure the server properly. This also fixed my error in the log [warn] RSA server certificate CommonName (CN) <fqdn> does NOT match server name!? – transistor1 Apr 16 '14 at 03:21
  • Do you think this should work with Apache 2.2.22? I am still getting "Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName" – David Winiecki Apr 12 '15 at 05:08
  • Is hostname localhost supposed to be myubuntuservername localhost? Or the string literal hostname localhost? You weren't clear in your answer – Jonathan Apr 05 '17 at 16:25
  • 3
    This should be the accepted answer. Seems that you may need to associate the FQDN to the static IP address rather than 127.0.0.1, though. – Skippy le Grand Gourou May 12 '17 at 14:25
  • 1
    This was the best answer for me on Ubuntu 18.04 I wish documentation was consistent though fully qualified domain name is not the same terminology here or anywhere. if my fully qualified domain name was google.com and my servername was google, my entry in /etc/hosts would be 127.0.0.1 google.com google I could replace the localhost line and ad local host to the end of this line if I wish. I used this: https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 – TheArchitecta Jan 17 '19 at 01:38
32
  • Open a terminal
  • Open the /etc/apache2/httpd.conf file:

    sudo editor /etc/apache2/httpd.conf # [1]
    
  • By default, it would be blank. Simply add the following line:

    ServerName localhost
    
  • Save the file and exit

  • Restart the server

    sudo service apache2 restart
    

[1] Launch default editor, see sudo update-alternatives --config editor

muru
  • 197,895
  • 55
  • 485
  • 740
green
  • 14,306
  • after executing last command the following error occur

    ** Restarting web server apache2 /usr/sbin/apache2ctl: 87: ulimit: error setting limit (Operation not permitted)

    /usr/sbin/apache2ctl: 87: ulimit: error setting limit (Operation not permitted) (13)Permission denied: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down Unable to open logs Action 'start' failed. The Apache error log may have more information. [fail] ** @green7

    – Deepu Feb 14 '13 at 13:52
  • try 'sudo service apache2 restart' – waterloo2005 Apr 15 '13 at 03:09
27

In new version of apache2 you just following command like this:

sudo nano /etc/apache2/apache2.conf

Add the following new line end of file:

ServerName localhost

Then restart apache2:

sudo service apache2 restart

It's done.

empugandring
  • 371
  • 3
  • 4
7

I find it is a bit better to create a new file in /etc/conf.d that to modify either apache2.conf or httpd.conf.

It's a personal preference that keeps my configuration changes separated from the distribution package. So updates are less complicated.

I create the file /etc/apache2/conf.d/AAserverName and it contains only:

ServerName myhost.mycomain.tld

The other suggestions certainly work also.

user.dz
  • 48,105
areeda
  • 71
  • 1
  • 1
3

In Ubuntu 16.04:

sudo -i

echo 'ServerName localhost' > /etc/apache2/conf-available/server-name.conf
a2enconf server-name
Elder Geek
  • 36,023
  • 25
  • 98
  • 183
2

On Ubuntu 16.04:

Add ServerName localhost to the file httpd.conf usin gthe following command.

sudo vi  /etc/apache2/httpd.conf

Then include this line Include httpd.conf at the end of this file

sudo vi /etc/apache2/apache2.conf

Check for syntax errors again

sudo apache2ctl configtest

You should now get

Syntax OK

You can now restart the server gracefully by reloading the configuration

sudo service apache2 reload

or kill the process and start again

sudo service apache2 restart
1

Add ServerName localhost

To

 sudo leafpad /etc/apache2/apache2.conf

 sudo leafpad /etc/apache2/httpd.conf

It's not an error.. It's just a friend reminder

Serem
  • 11
  • 1
1

Specifying ServerName localhost in your configuration files outside of the virtual host sections is the way to do this.

Some other answers suggest that you should modify /etc/apache2/httpd.conf. This file gets overwritten when apache gets upgraded from apt. For Apache configuration that you don't want to get overwritten, you should create a new file. Here is the "Debian way" to make this configuration change:

# create the configuration file in the "available" section
echo "ServerName localhost" | sudo tee /etc/apache2/conf-available/servername.conf
# enable it by creating a symlink to it from the "enabled" section
sudo a2enconf servername
# restart the server
sudo service apache2 restart

This is basically the same as Stark's answer, but in an easy to copy and paste script form. I had originally posted this in a question that was marked as duplicate: https://askubuntu.com/a/432408

0

On Ubuntu 11.10, I saw this message, along with a hung boot occurring because my disk was full. One of the log files had gone rogue. Probably there was not actually a problem with Apache, but this message was the last clue given before the boot hung.

To fix the problem, I had to boot into recovery mode and remove the wayward log file.

0

Run the following command:

apachectl -t -D DUMP_INCLUDES

to determine path to your httpd.conf configuration file, then edit that file and uncomment/modify the line which specifies the value for ServerName option, e.g.

ServerName localhost

For the web server, use the registered DNS name (e.g. example.com).

If your host doesn't have a registered DNS name, enter its IP address here.

kenorb
  • 10,347
0

If you're using bash and want minimal dependencies (and just want a one-liner for your entrypoint script if you are using Docker like me), these two options should work.

If you want localhost:

echo "ServerName localhost" >> /etc/apache2/apache2.conf

If you want the existing hostname:

echo "ServerName $(cat /etc/hostname)" >> /etc/apache2/apache2.conf

This uses the bash redirection operators to append the string to the end of the file. Echo automatically inserts the newline, so you are all set.

0

On Ubuntu 16.04:

Add ServerName localhost to the file httpd.conf usin gthe following command.

sudo vi  /etc/apache2/httpd.conf

Then include this line Include httpd.conf at the end of this file

sudo vi /etc/apache2/apache2.conf

Check for syntax errors again

sudo apache2ctl configtest

You should now get

Syntax OK

You can now restart the server gracefully by reloading the configuration sudo service apache2 restart