69

NOTE: This is related to my question: "Apache 2.4 won't reload, any problem with my configuration?".

I'm trying to test a local site, locally. As I understand Apache 2 (and perhaps Apache as well) has something called VirtualHost. My little bit of understanding tells me that virtualhosting is a way where one server/IP address can serve multiple domains.

Anyway, I'm getting the following error when running Apache 2's configtest to see where I'm failing. I'm running Apache 2.4.10-1, and it seems there are a lot of changes which have happened between Apache 2.2 and Apache 2.4 which I'm not aware of.

$ sudo apache2ctl configtest
[sudo] password for shirish:
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK

This is the /etc/hosts file:

 $ cat /etc/hosts
127.0.0.1    localhost
127.0.1.1    debian mini

I also see an empty /etc/hosts.conf file. Perhaps the data in /etc/hosts needs to be copied to /etc/hosts.conf for the server to take cognizance?

My hostname:

$ hostname
debian

This is the configuration file of the site:

$ cat /etc/apache2/sites-available/minidebconfindia.conf
<VirtualHost mini:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/html/in2014.mini/website

    <Directory />
        Options +FollowSymLinks +Includes
        Require all granted
    </Directory>

    <Directory /var/www/html/in2014.mini/website/>
        Options +Indexes +FollowSymLinks +MultiViews +Includes
        Require all granted
    </Directory>
</VirtualHost>

I also read about binding to addresses and ports, but I haven't understood that well for multiple reasons. It doesn't give/share an example as to in which file those lines need to be put and what will come before and after. An example would have been much better.

I did that and restarted the server, but I still get the same error.

~$ sudo apache2ctl configtest
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK

It seems there are three configuration files in Debian that I need to know and understand.

/etc/apache2$ ls *.conf
apache2.conf  ports.conf

and

/etc/apache2/conf.d$ ls *.conf
httpd.conf

Apparently, apache2.conf IS the global configuration file while the httpd.conf is a user-configuration file. There is also ports.conf. Both apache2.conf and ports.conf are at the defaults except I have changed the loglevel of Apache from warn to debug.

I tried one another thing:

$ sudo apache2ctl -S
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
VirtualHost configuration:
127.0.1.1:80           debian (/etc/apache2/sites-enabled/minidebconfindia.conf:1)
*:80                   127.0.1.1 (/etc/apache2/sites-enabled/000-default.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33

Maybe somebody has more insight.

Peter Mortensen
  • 1,029
  • 1
  • 8
  • 10
shirish
  • 11,967
  • 27
  • 107
  • 190

5 Answers5

78

The file to edit:

/etc/apache2/apache2.conf

Command to edit file:

sudo nano /etc/apache2/apache2.conf

For a global servername you can put it at the top of the file (outside of virtual host tags).

The first line looks like:

ServerName myserver.mydomain.com

Then save and test the configuration with the following command:

apachectl configtest

You should get:

Syntax OK

Then you can restart the server and check you don't get the error message:

sudo service apache2 restart
Peter Mortensen
  • 1,029
  • 1
  • 8
  • 10
Mark N Hopgood
  • 885
  • 6
  • 6
  • 2
    This is the Debian correct answer. I tried `/etc/apache2/conf-available/fqdn.conf`, or `servername.conf` or `httpd.conf` to no avail. Debian `Jessie`, and `apache 2.4.10` – DrBeco Jul 21 '15 at 17:29
  • 4
    If you have `conf-available/` and `conf-enabled/`, create a file in `conf-available/` and use the command `a2enconf` to enable it. This way, dpkg will not annoy you about untracked changes when/if the `apache2-common` package updates `apache2.conf` – Kevin Aug 24 '15 at 14:28
  • The file in `conf-available` after this and an edit might be `/etc/apache2/conf-available/servername.conf` with content `ServerName myserver.mydomain.com` (tried on Raspberry Pi, Apache 2.4.10, Debian 8 (Jessie), Linux kernel 4.4.34). – Peter Mortensen Dec 27 '16 at 13:41
  • @PeterMortensen could you use the example shared so I can understand what you mean. – shirish Jan 04 '17 at 17:39
  • The file does not exist – Black Jul 30 '19 at 12:37
14

You must set ServerName directive:

ServerName localhost

In Debian, you can set it in /etc/apache2/conf.d/httpd.conf.

On macOS, you can set it in /private/etc/apache2/httpd.conf.

crmpicco
  • 549
  • 1
  • 8
  • 22
cuonglm
  • 150,973
  • 38
  • 327
  • 406
  • 7
    In Ubuntu, set it in /etc/apache2/apache2.conf – macki Dec 01 '14 at 06:28
  • 1
    Debian either: `apache2.conf`. – DrBeco Jul 21 '15 at 17:29
  • 1
    What versions of Apache/Debian does this work for? I don't think it works for Apache 2.4. It could be `/etc/apache2/apache2.conf` instead. – Peter Mortensen Dec 27 '16 at 13:01
  • it works, Apache 2.4 on Debian 8 – erajuan Feb 20 '17 at 02:35
  • Everything gets more complicated. Setting it near the top of /etc/apache2/apache2.conf works, there's a "Global configuration" label but it's just a comment. On a LAN with NAT your servername can be the machine's hosthname that's in /etc/hostname. If you use static IPs you can put your IP in there. Using Apache 2.4.25. apache2ctl is in /usr/sbin which is maybe not in all user's paths. – Alan Corey Dec 12 '20 at 18:29
  • I like this solution of using *localhost* best because I found that Apache assigns this "global" *ServerName* to the first *VirtualHost* it finds that does not contain a *ServerName*, which can be very confusing. – CODE-REaD Oct 20 '21 at 20:03
6

To locate your Apache configuration file, try running the following command:

apachectl -t -D DUMP_INCLUDES

Then edit that file and locate line with ServerName and uncomment it, so it looks like:

ServerName localhost
kenorb
  • 20,250
  • 14
  • 140
  • 164
3

I'm a few years late to the party ... 2023. I'm on Raspian, Apache2, I ran this command to find ServerName ...

find /etc/apache2 -type f -exec grep -s ServerName {} \;

I found it in sites-available/000-default.conf, in the VirtualHost configuration, nowhere else. I would think the correct file for ServerName would have it in place already, perhaps commented out, as it is where I found it.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
user3481644
  • 141
  • 4
  • `grep -R ServerName /etc/apache2/*` also works, no need to use `exec`. – dr_ Mar 23 '23 at 13:16
  • @dr_ Does using exec cause problems? – user3481644 Mar 26 '23 at 13:46
  • It's unnecessary here. Always do things the simplest possible way (but not simpler, as Einstein used to say). – dr_ Mar 27 '23 at 07:28
  • @dr_ Are you sure? I thought that was Ghandhi ... ;) – AdminBee Mar 28 '23 at 09:36
  • I understand the philosophy of simpler is better, but this is not a production script, it would be run probably one time, why worry about it? If this was to be run frequently, then optimization is warranted. – user3481644 Mar 28 '23 at 19:13
  • 1
    I'm gong to side with user3481644 here. The correct answer states that ServerName should be in the top-level file. It works just fine if you put the line there, but it isn't originally there, even commented out. This makes the original answer outdated. – Robert Rapplean Apr 17 '23 at 21:32
0

In apache24 config file, you will see the line ServerName:80:

  1. Leave it blank.
  2. Then below that line in the empty space write:
    ServerName www.name (of your official domain or website.com:80)
  3. Save the config file.
  4. Go to cmd, open it as administrator change dir to where apache24 is
  5. Then cd apache24 then cd bin then httpd.exe

You will see the problem is solved.

Kevdog777
  • 3,194
  • 18
  • 43
  • 64