How to Comment in Apache Configuration Files?

Adding comments to your Apache configuration files is a straightforward process that helps document changes, explain complex routing rules, and temporarily disable directives without deleting them. In Apache HTTP Server configurations (such as httpdf.conf or apache2.conf), comments are created by placing a hash or pound sign (#) at the beginning of a line. This article covers the exact syntax for implementing these comments, the rules governing their placement, and best practices to avoid common configuration errors.

The Basic Comment Syntax

To write a comment in any Apache configuration file, you must start the line with the # character. Whenever the Apache server parses the configuration file, it completely ignores any line that begins with this symbol.

# This is a single-line comment in Apache
# The directive below is disabled for maintenance
# Listen 8080
Listen 80

Crucial Rules for Apache Comments

While the syntax is simple, Apache has strict rules regarding where comments can be placed. Failing to follow these rules can result in configuration syntax errors or unexpected server behavior.

Correct vs. Incorrect Usage Examples

Here is a look at how to properly structure your comments compared to what will cause your server to fail a configuration check.

Correct Placement:

<VirtualHost *:80>
    # ServerAdmin settings for the production site
    ServerAdmin webmaster@example.com
</VirtualHost>

Incorrect Placement (Will Cause Errors):

ServerAdmin webmaster@example.com # This inline comment will cause a syntax error

Verifying Your Changes

After adding or modifying comments in your configuration files, it is always best practice to test the syntax before restarting your web server. You can do this by running the following command in your terminal:

If your comments are placed correctly, the terminal will return a Syntax OK message, indicating it is safe to reload your Apache service.