Mindfulness

Declutter Apache Modules

Updated · By the Relaxmid editors

Reading time
4 min
Sections
7
Topic
Mindfulness

Declutter Apache Modules means removing unused extensions from your server configuration to free up memory and reduce attack surface, but most people simply disable them in the config file without restarting, which leaves the code loaded in memory.

Apache loads modules at startup based on the configuration files. If you add a module to a file but do not restart the server, the change has no effect. To actually remove a module, you must edit the configuration, restart Apache, and verify the change took effect.

How to Find Which Modules Are Loaded

Before you can remove modules, you need to know which ones are active on your server. The most reliable way is to use the server status page if you have access to it, or to generate a module list from the command line.

  1. Open a terminal on the server.
  2. Run the following command to list all compiled-in modules that are currently active:
    apache2ctl -M
    

    This command outputs a space-separated list of module names that are currently loaded. For example, you might see rewrite_module (shared) or expires_module (shared).

  3. Look for modules that match the names of extensions you do not use. Common candidates include autoindex (directory listings), userdir (user home directories), or info (server information pages).

How to Disable or Configure Modules in the Main Config

Once you have identified a module to remove, you must edit the Apache configuration file. The exact location of this file depends on your operating system and how Apache was installed, but it is usually /etc/apache2/apache2.conf on Debian/Ubuntu or /etc/httpd/conf/httpd.conf on RHEL/CentOS.

  1. Open the main configuration file with a text editor as root:
    sudo nano /etc/apache2/apache2.conf
    
  2. Find the section that defines modules. It typically starts with a comment like # Load modules or LoadModule lines at the top of the file.
  3. Comment out the line that loads the module you want to disable by adding a hash symbol (#) to the beginning of the line. For example, to disable the autoindex module, change:
    LoadModule autoindex_module modules/mod_autoindex.so
    

    to:

    # LoadModule autoindex_module modules/mod_autoindex.so
    
  4. Save the file and exit the editor.

How to Restart Apache to Apply Changes

Modifying the configuration file does not take effect until you reload or restart the Apache service. The method you use depends on your operating system.

  1. On Debian/Ubuntu, use the following command to reload the configuration:
    sudo systemctl reload apache2
    

    This command reloads the configuration without dropping existing connections. If you see no output, the reload was successful.

  2. On RHEL/CentOS, use the following command:
    sudo systemctl reload httpd
    
  3. On some systems, you may need to restart the service completely to ensure the module is unloaded. Use:
    sudo systemctl restart apache2
    

    This will stop the service, start it again, and load the new configuration.

  4. Check the server status again with apache2ctl -M to confirm the module is no longer listed.

How to Check if a Module Is Needed by a Site

Disabling a module might break your websites if they rely on that module. Before you remove a module, verify that no configuration files or site-specific directives depend on it.

  1. Navigate to the site configuration directory. On Debian/Ubuntu, this is usually /etc/apache2/sites-available. On RHEL/CentOS, it is /etc/httpd/conf.d.
  2. Open each .conf file in that directory with a text editor.
  3. Use the search function in your editor to look for the module name. For example, search for mod_rewrite or mod_expires.
  4. If you find references to the module, you must keep it enabled or find an alternative way to achieve the same effect. For example, if a site uses mod_rewrite for pretty URLs, you cannot disable that module without rewriting your URLs.

How to Verify the Server Is Still Working After Decluttering

After disabling modules, it is important to confirm that your websites are still accessible and serving content correctly.

  1. Open a web browser on a machine that has access to the server.
  2. Enter the URL of your website. If the site loads without errors, the basic operation is intact.
  3. Check the server error logs for any issues. On Debian/Ubuntu, the logs are typically in /var/log/apache2/. On RHEL/CentOS, they are in /var/log/httpd/.
  4. Look for errors related to missing modules, such as [core:notice] AH00052: child pid ... exited with status 1 or permission denied errors when trying to load the module.

How to Handle Modules Loaded in VHost Files

Some modules are loaded in the main configuration file, while others are loaded in the virtual host (vhost) configuration files. You must check both locations to ensure you do not accidentally disable a module that is only needed for a specific site.

  1. Navigate to the vhost configuration directory as shown in the previous section.
  2. Open each .conf file and search for LoadModule lines inside it.
  3. If a module is only loaded in a vhost file, disabling it in the main configuration will not affect that site. You must edit the specific vhost file and comment out the LoadModule line there as well.

What to Do if You Disable the Wrong Module

If you disable a module that your site needs, the site will likely return an Internal Server Error or a blank page. You can quickly restore functionality by enabling the module again.

  1. Open the configuration file where you commented out the module.
  2. Remove the hash symbol (#) from the beginning of the line.
  3. Save the file and reload Apache with sudo systemctl reload apache2 or sudo systemctl reload httpd.
  4. Refresh your browser to see if the site loads correctly.

Related

General information for everyday wellbeing, not medical advice. If something affects your health, sleep or mood for more than a short while, talk to a qualified professional. Read more

Spotted an error? Tell us and we will correct it (corrections policy).