188

I tried to restart my Apache server on CentOS 5.0 and got this message:

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

Here is the /etc/hosts file:

127.0.0.1    server4-245    server4-245.com    localhost.localdomain localhost
::1        localhost6.localdomain6 localhost6

Here is the /etc/sysconfig/network file:

NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=server4-245

I also have this in the Apache httpd.conf file:

ServerName localhost

However, I still get the first error message when I restart Apache.

kenorb
  • 155,785
  • 88
  • 678
  • 743
user1220351
  • 1,885
  • 2
  • 12
  • 6

12 Answers12

216

If you don't have httpd.conf in folder /etc/apache2, you should have apache2.conf - simply add:

ServerName localhost

Then restart the apache2 service.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
riegersn
  • 2,819
  • 3
  • 17
  • 19
  • 2
    Worked perfectly on Ubuntu 12.04 LTS running multiple virtual hosts. – Gor Jan 07 '13 at 00:43
  • 3
    Worked perfectly on Mac OS 10.7.5 running multiple virtual hosts. – DudeOnRock May 07 '13 at 00:41
  • 1
    Worked on Mac OS 10.7.5 running Virtualbox – iWizard May 20 '13 at 13:40
  • 3
    Worked perfectly on Centos 6.4. – Kel Solaar Jul 29 '13 at 21:10
  • 7
    Worked perfectly on Centos 6 (cloud version), though httpd.conf was in /etc/httpd/conf/ – gbarry Aug 31 '13 at 23:20
  • Worked perfectly on RHEL 6.2 – Deshan Jan 02 '14 at 07:11
  • Worked perfectly on Linux Mint 16 :) – ackuser Aug 18 '14 at 13:44
  • This caused redirection problems with virtual hosts for me. I have a default vhost that deals with spoof domain access and non-canonical access (e.g. to strip the www. off) which does not use `ServerName` (as per docs for the default vhost), but putting this in seemed to confuse it mightily. – artfulrobot Oct 21 '14 at 09:54
  • Worked on Centos 6.6. – jfmercer Jun 23 '15 at 18:10
  • On Dockerised Centos 6.4 had to use ServerName ${HOSTNAME} – danday74 Jan 22 '16 at 19:26
  • Worked perfectly on openSUSE Leap. – mtchuente Mar 29 '16 at 09:39
  • thanks worked for macOs m1 bigsur – Shakir Baba Oct 29 '21 at 17:09
  • Actually, this _might_ work OK today in Debian based distributions, like Ubuntu. It certainly worked back in 2013. But now we uses `a2en*` and `a2di*` commands. And they make links from `/e/a2/conf-available` to `e/a2/conf-enable`. So you edit files in `/e/a2/conf-available` and the server looks for enable ones in `/e/a2/conf-enable`. Same for `mod`-file etc. If you use other popular web servers, they use a similar mechanism on Debian based distributions. (Easier to copy and remove files/links then edit one huge one) – Anders Sep 27 '22 at 16:36
99

Your hosts file does not include a valid FQDN, nor is localhost an FQDN. An FQDN must include a hostname part, as well as a domain name part. For example, the following is a valid FQDN:

host.server4-245.com

Choose an FQDN and include it both in your /etc/hosts file on both the IPv4 and IPv6 addresses you are using (in your case, localhost or 127.0.0.1), and change your ServerName in your httpd configuration to match.

/etc/hosts:

127.0.0.1    localhost.localdomain localhost host.server4-245.com
::1          localhost.localdomain localhost host.server4-245.com

httpd.conf:

ServerName host.server4-245.com
Paul Stengel
  • 1,149
  • 8
  • 8
  • 6
    Note too that you'll want to modify your HOSTNAME entry in your /etc/sysconfig/network file to match your chosen name. – Paul Stengel Mar 02 '12 at 23:55
  • 2
    Hmm. I've got: `127.0.0.1 localhost gondor gondor.localdomain 192.168.0.3 gondor.localdomain` but it seems linux doesn't think that gondor.localdomain is an FQDN either. What's the issue with that? Do I have to have a TLD? – Adam May 10 '13 at 10:08
  • Hi I did try all of the above and I am still getting the same errors, I don't quite understand what I lack here. My FQDN is ak.local.com set in the same fashion as your answer but I am still getting the same issues. I am running on OSX 10.10 – AKFourSeven Jan 20 '15 at 18:43
  • 2
    I found that: 127.0.0.1 myserver myserver.mydom.com ... etc << does not work 127.0.0.1 myserver.mydom.com myserver ... etc << does work – Martin Dec 18 '15 at 13:17
  • 1
    As @Martin noticed, the FQN should be the _first_ name in each line in `/e/hosts` after the IP address. _Then_ you add the aliases, like the name without domain name, and other names that could be useful. You can always check the result with the command `getent hosts _IP-number/hostname_` to see what programs will see. It uses the same mechanism that C-programs uses (most programs). Could also try `ahosts` instead of `hosts`. See man pages for `getent(1)`. *Very* useful program for any administrator. – Anders Sep 27 '22 at 16:44
67

After the initial install of Apache server, I got the following error while restarting the Apache service on Ubuntu 12.04 (Precise Pangolin)

The solution is really simple. Just add the ServerName directive to /etc/apache2/httpd.conf:

sudo nano /etc/apache2/httpd.conf

Add: ServerName localhost

Finally restart the Apache server:

sudo /etc/init.d/apache2 restart
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rizon
  • 8,127
  • 1
  • 27
  • 17
  • 3
    Within CentOS 7, the path will be /etc/httpd/conf/httpd.conf – Debashis Apr 27 '16 at 12:01
  • Debian based distributions uses one configuration file per virtual server in Apache2. That is `/etc/apache2/conf-available` and `/etc/apache2/conf-enable` tells which is available and which is enable. You also uses commands lie `a2ensite webs` to enable `webs`-webserver via `/e/a/c-a/webs.conf`. See `/usr/share/doc/apache2/README.Debian.gz` (use `zcat`) for more `a2en*` and `a2di*` commands. – Anders Sep 27 '22 at 16:28
  • But obviously this isn't valid on RedHat based Linux distributions. :-) Just added for Debian based distribution users to find the right source and instructions. – Anders Sep 27 '22 at 16:31
15

Make sure you're editing the right httpd.conf file, then the error about unreliable server's domain name should be gone (this is the most common mistake).

To locate your httpd.conf Apache configuration file, run:

apachectl -t -D DUMP_INCLUDES

Then edit the file and uncomment or change ServerName line into:

ServerName localhost

Then restart your apache by: sudo apachectl restart

kenorb
  • 155,785
  • 88
  • 678
  • 743
11

So while this is answered and accepted it still came up as a top search result and the answers though laid out (after lots of research) left me scratching my head and digging a lot further. So here's a quick layout of how I resolved the issue.

Assuming my server is myserver.myhome.com and my static IP address is 192.168.1.150:

  1. Edit the hosts file

    $ sudo nano -w /etc/hosts
    
    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    
    127.0.0.1 myserver.myhome.com myserver
    
    192.168.1.150 myserver.myhome.com myserver
    
    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    ::1 myserver.myhome.com myserver
    
  2. Edit httpd.conf

    $ sudo nano -w /etc/apache2/httpd.conf
    
    ServerName myserver.myhome.com
    
  3. Edit network

    $ sudo nano -w /etc/sysconfig/network HOSTNAME=myserver.myhome.com
    
  4. Verify

    $ hostname
    
    (output) myserver.myhome.com
    
    $ hostname -f
    
    (output) myserver.myhome.com
    
  5. Restart Apache

    $ sudo /etc/init.d/apache2 restart
    

It appeared the difference was including myserver.myhome.com to both the 127.0.0.1 as well as the static IP address 192.168.1.150 in the hosts file. The same on Ubuntu Server and CentOS.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2416772
  • 119
  • 1
  • 5
9

In httpd.conf, search for "ServerName". It's usually commented out by default on Mac. Just uncomment it and fill it in. Make sure you also have the name/ip combo set in /etc/hosts.

sparkyspider
  • 13,195
  • 10
  • 89
  • 133
  • Thanks, this was helpful. httpd wasn't picking up my fqdn in /etc/hosts, and my hostname seemed configured properly. – Banjer Jul 24 '12 at 14:54
4

In the Apache httpd.conf file:

ServerName: 127.0.0.1
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

There are two ways to resolve this error:

  1. Include /etc/apache2/httpd.conf

    Add the above line in file /etc/apache2/apache2.conf

  2. Add this line at the end of the file /etc/apache2/apache2.conf:

    ServerName localhost

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jimit Shah
  • 653
  • 1
  • 7
  • 16
2

I've resolved the fully qualified domain name message on different occasions by adding my server hostname to the /etc/apache2/httpd.conf file and to the /etc/apache2/apache2.conf file.

Type hostname -f in your terminal. This query will return your hostname.

Then edit the /etc/apache2/httpd.conf file (or create it if it does not exist for some reason) and add ServerName <your_hostname>.

Alternatively, I have also been able to eliminate the message by adding ServerName <your_hostname> to the /etc/apache2/apache2.conf file.

If all goes well, when you restart Apache, the message will be gone.

John Creamer
  • 9,704
  • 1
  • 14
  • 8
2

Most answers suggest to just add ServerName localhost to /etc/apache2/apache2.conf.

But quoting Apache documentation :

The presence of this error message also indicates that Apache httpd was unable to obtain a fully-qualified hostname by doing a reverse lookup on your server's IP address. While the above instructions will get rid of the warning in any case, it is also a good idea to fix your name resolution so that this reverse mapping works.

Therefore adding such a line to /etc/hosts is probably a more robust solution :

192.0.2.0  foobar.example.com  foobar

where 192.0.2.0 is the static IP address of the server named foobar within the example.com domain.

One can check the FQDN e.g. with

hostname -A

(shortcut for hostname --all-fqdn).

Community
  • 1
  • 1
Skippy le Grand Gourou
  • 6,976
  • 4
  • 60
  • 76
  • 1
    To look up names, like any program does, you could also run this on Linux machines `getent hosts hostname` or `getent hosts 192.0.20.0`. If you want all IP addresses, not just IPv6, try `getent ahost 192.0.20.0`. This uses the same API as C programs uses (that is most programs). – Anders Sep 27 '22 at 16:17
2

Turns out that I had this problem and it was because I used "tabs" to indent lines instead of spaces. Just posting, in case it helps anyone.

Richard
  • 5,584
  • 1
  • 19
  • 22
1

If you've edited /etc/apache2/httpd.conf with the ServerName localhost you may be editing the wrong file. All answers I found were pointing towards that standard httpd.conf. After some foraging, I found a good answer here.

To locate the right httpd.conf file use

apachectl -t -D DUMP_INCLUDES

I found mine was actually /usr/local/etc/httpd/httpd.conf.

Use your preferred editor to comment out the line (i.e. remove the # before) starting with ServerName, and replace the domain name for the appropriate one – local environments should work with

ServerName localhost

I hope this helps more people who may be stuck.