How to Configure ServerName in Apache?

Configuring the ServerName directive in Apache is a foundational step in web server administration, ensuring that your server correctly identifies itself and routes web traffic to the appropriate website. This guide provides a straightforward overview of how ServerName works, where to locate it in your configuration files, and the exact steps required to set it up for both global server identification and individual Virtual Hosts.

Understanding the ServerName Directive

The ServerName directive tells the Apache HTTP Server its own hostname and port. It is primarily used for two critical functions:

Step-by-Step Configuration

To configure ServerName, you will need administrative access (sudo privileges) to your server’s terminal.

1. Locate Your Configuration File

Depending on your operating system, the main Apache configuration file or the Virtual Host files are located in different directories:

2. Configure Global ServerName

If you do not set a global ServerName, Apache will generate a harmless but annoying warning when restarting: “Could not reliably determine the server’s fully qualified domain name”.

To fix this, open your main configuration file and add or modify the directive at the global level:

ServerName your-server-ip-or-domain.com

3. Configure ServerName for Virtual Hosts

To map a specific domain name to a specific website on your server, place the ServerName directive inside the corresponding <VirtualHost> block. You can also use ServerAlias to match variations like the “www” subdomain.

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/example.com/public_html
    
    ServerName example.com
    ServerAlias www.example.com

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

4. Test and Restart Apache

Before applying the changes, always check your Apache configuration syntax for errors to avoid crashing your web server.

Run the syntax test command:

If the output says Syntax OK, safely restart the Apache service to apply your new settings: