Skip to main content

Managing Apache Modules

1. Listing Loaded Modules

Command Description Example
apachectl -M Lists all loaded modules apachectl -M
httpd -M Alternative command to list loaded modules httpd -M

Example: Listing Loaded Modules

apachectl -M

2. Understanding Static vs Dynamic Modules

Module Type Description Compilation Load Time
Static Module Compiled directly into the Apache binary at build time Built into the core binary Loaded automatically when Apache starts
Dynamic Module Compiled as a separate shared library (.so file) and loaded at runtime Not part of the core binary Loaded on demand via configuration

Example: Loading a Dynamic Module

# Load mod_rewrite as a dynamic module
LoadModule rewrite_module modules/mod_rewrite.so

# Or using the relative path (if installed in default location)
LoadModule rewrite_module modules/mod_rewrite.so
Note: Static modules are not configurable at runtime and require a server restart to change. Dynamic modules can be enabled/disabled without restarting Apache.

3. Loading Dynamic Modules

Directive Description Default Notes
LoadModule Loads a dynamic module at startup None Must be specified before the module is used

Example: Loading a Dynamic Module

LoadModule rewrite_module modules/mod_rewrite.so
Note: The path to the module file is relative to the server root or absolute.
Warning: Loading modules at runtime is not supported in Apache 2.4+ without using mod_so.
Module File Extension OS Support Notes
.so Unix/Linux Shared object files for dynamic loading
.dll Windows Dynamically linked libraries

4. Enabling Modules on Debian/Ubuntu

Module Command Notes
rewrite sudo a2enmod rewrite Enables URL rewriting functionality
ssl sudo a2enmod ssl Enables SSL/TLS support
headers sudo a2enmod headers Allows manipulation of HTTP request and response headers
expires sudo a2enmod expires Configures HTTP caching with expiration headers
deflate sudo a2enmod deflate Enables content compression (gzip)
cache sudo a2enmod cache Enables HTTP caching (requires mod_cache)

Example: Enable mod_rewrite and restart Apache

sudo a2enmod rewrite
sudo systemctl restart apache2
Note: After enabling a module, you must restart Apache for changes to take effect.
Warning: Ensure the module is compatible with your Apache version before enabling.

5. Disabling Modules on Debian/Ubuntu

Module Status Command Notes
mod_rewrite Enabled a2dismod rewrite Disables the mod_rewrite module
mod_ssl Enabled a2dismod ssl Disables the mod_ssl module
mod_alias Enabled a2dismod alias Disables the mod_alias module

Example: Disable mod_rewrite

sudo a2dismod rewrite
Note: After disabling a module, you must restart Apache to apply changes.

Example: Restart Apache

sudo systemctl restart apache2

6. Loading Modules on RHEL/CentOS

On RHEL/CentOS systems, Apache modules can be managed using the httpd service and the modutil utility. The modules are typically located in /etc/httpd/modules/.

Command Description Notes
sudo httpd -M Lists all loaded modules Useful for verifying module status
sudo httpd -t -D DUMP_MODULES Dumps all available modules Shows all modules, whether loaded or not
sudo /usr/sbin/httpd -M Lists all loaded modules (alternative) Alternative path to the Apache binary

Example: Enabling a Module

sudo /usr/sbin/httpd -M | grep -i rewrite

Example: Loading a Module via Configuration

# Add to /etc/httpd/conf.d/rewrite.conf
LoadModule rewrite_module modules/mod_rewrite.so
Note: On RHEL/CentOS, modules are often managed via configuration files in /etc/httpd/conf.d/ rather than directly using the command line.
Warning: After enabling a module, always restart the Apache service to apply changes.
sudo systemctl restart httpd

7. Checking Module Dependencies

Module Dependencies Notes
mod_rewrite mod_headers, mod_ssl Required for URL rewriting and header manipulation
mod_ssl mod_nss, mod_setenvif SSL/TLS support requires additional modules for certificate handling and environment variables
mod_proxy mod_http, mod_rewrite Proxy functionality depends on HTTP handling and URL rewriting capabilities
mod_cache mod_disk_cache, mod_mem_cache Caching features can be implemented via disk or memory-based caches
mod_security mod_reqtimeout, mod_headers Security enhancements rely on request timeout and header management modules

Example: Checking Module Dependencies

apache2ctl -M | grep mod_rewrite
apache2ctl -M | grep mod_ssl
Note: Use apache2ctl -M to list all loaded modules and verify dependencies before enabling new ones.

8. Understanding Core Modules

Module Description Enabled by Default Configuration Directive
mod_rewrite URL rewriting engine for dynamic URL manipulation Yes RewriteEngine On
mod_alias Defines aliases for URLs and directory paths Yes Alias /images/ /var/www/images/
mod_authz_core Access control for directories and files Yes <Directory "/var/www">
mod_headers Manipulation of HTTP headers in requests and responses No Header set X-Custom-Header "value"
mod_ssl Secure HTTP communication using SSL/TLS No SSLEngine on
mod_proxy Reverse and forward proxy support No ProxyPass /api/ http://backend:8080/

Example: Enabling mod_rewrite

sudo a2enmod rewrite

Example: Using mod_alias

<VirtualHost *:80>
    Alias /images/ /var/www/images/
    <Directory "/var/www/images/">
        Options Indexes
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>
Note: Core modules are compiled into Apache and can be enabled or disabled using a2enmod and a2dismod commands on Debian/Ubuntu systems.

9. Compiling Custom Modules

Compiling custom Apache modules requires a compatible development environment and understanding of the Apache module API. Custom modules can extend Apache's functionality by adding new handlers, filters, or configuration directives.

Component Description Notes
Module Structure Each module must define a module structure containing function pointers and metadata. Defined in httpd.h
Build System Apache uses apxs (Apache Extension Tool) for building modules. Part of the apache2-dev package on Debian/Ubuntu
Configuration Modules must be loaded via LoadModule directive in httpd.conf. Can be added dynamically at runtime

Example: Basic Module Skeleton

#include <httpd.h>
#include <http_config.h>
#include <http_protocol.h>

module AP_MODULE_DECLARE_DATA example_module;

static int example_handler(request_rec *r) {
    if (strcmp(r->handler, "example")) return DECLINED;
    r->content_type = "text/plain";
    ap_rprintf(r, "Hello from Example Module!");
    return OK;
}

static void example_register_hooks(apr_pool_t *p) {
    ap_hook_handler(example_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

AP_DECLARE_MODULE(example) = {
    STANDARD20_MODULE_STUFF,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    example_register_hooks
};

Example: Compiling with apxs

apxs -c -i example_module.c
Note: Ensure that the Apache development headers and apxs are installed in your system before compiling custom modules.
Warning: Incorrect module code can crash the Apache server. Always test in a development environment first.

Compilation Steps Overview

  • Write module code using Apache's API
  • Use apxs to compile and install the module
  • Load the module in httpd.conf using LoadModule
  • Restart or reload Apache to apply changes

10. Installing Third-Party Modules

Installing third-party modules into Apache HTTP Server involves compiling the module source code against the Apache server's build environment. This process requires careful attention to version compatibility and build dependencies.

Step Description Notes
1. Obtain Source Code Download the source code of the third-party module from its official repository or distribution. Ensure the version is compatible with your Apache HTTP Server version.
2. Prepare Build Environment Install Apache development headers and tools required for compilation. On Debian/Ubuntu: apt-get install apache2-dev
3. Compile Module Use apxs or mod_lua to compile the module. Example command: apxs -i -a -c mod_foo.c
4. Load Module in Configuration Add LoadModule directive to Apache config. Example: LoadModule foo_module modules/mod_foo.so

Example: Compiling a Third-Party Module

cd /path/to/module/source
apxs -i -a -c mod_foo.c
Note: Ensure that the module's source code is compatible with your Apache version to avoid runtime errors.

11. Understanding Module Load Order

Module Load Priority Description
mod_rewrite 5 URL rewriting engine; loads after core modules
mod_ssl 10 SSL/TLS support; requires OpenSSL library
mod_proxy 15 Proxy support; often used with mod_rewrite
mod_authz_core 20 Core authorization; loads before auth modules
mod_mime 30 MIME type handling; essential for content negotiation
Note: Module load order affects functionality and can cause conflicts if not managed properly. Core modules load first, followed by standard modules in a defined sequence.

12. Troubleshooting Module Conflicts

Issue Description Solution
Module Loading Order Modules may fail to load if dependencies are not met or loaded in wrong order. Check LoadModule directives and ensure dependencies are loaded before dependent modules.
Conflicting Modules Two or more modules may define the same directive or handler, causing conflicts. Use httpd -M to list loaded modules and identify duplicates or conflicts.
Syntax Errors in Config Incorrect syntax in .conf files can prevent modules from loading properly. Validate configuration with httpd -t to catch syntax errors before restarting.
Version Mismatch Modules compiled for different Apache versions may not load correctly. Ensure all modules are built against the same Apache version using httpd -V.

Example: Checking Module Conflicts

httpd -M | grep -E "(rewrite|ssl)"
Note: Use httpd -S to display the parsed configuration and verify module usage.
Warning: Avoid mixing core and third-party modules without proper compatibility checks.

Was this article helpful?