Enabling Apache Modules via CLI in RHEL/CentOS: a2enmod Equivalent and Configuration Best Practices


3 views

While Debian-based systems use a2enmod and a2dismod utilities for module management, RHEL/CentOS takes a more direct approach through configuration files. The key configuration files are:

/etc/httpd/conf.modules.d/00-base.conf
/etc/httpd/conf.modules.d/00-ssl.conf

To enable a module (for example, rewrite_module):

# Check if module is loaded
httpd -M | grep rewrite_module

# Enable by uncommenting in relevant config
sudo sed -i '/LoadModule rewrite_module/s/^#//g' /etc/httpd/conf.modules.d/00-base.conf

# Alternative: add load directive
echo "LoadModule rewrite_module modules/mod_rewrite.so" | sudo tee -a /etc/httpd/conf.modules.d/00-base.conf

Some modules require separate packages. For example, to enable PHP support:

sudo yum install php php-mysql
sudo systemctl restart httpd

For better organization, create separate files in /etc/httpd/conf.modules.d/:

# Create custom module config
sudo tee /etc/httpd/conf.modules.d/10-custom.conf << 'EOL'
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so
EOL

Always verify module loading after changes:

# Syntax check
sudo apachectl configtest

# List loaded modules
httpd -M

# Check for errors
sudo journalctl -u httpd -f

Create your own a2enmod equivalent:

#!/bin/bash
# a2enmod for RHEL
MODULE=$1
CONF_FILE="/etc/httpd/conf.modules.d/00-base.conf"

if grep -q "$MODULE"_module "$CONF_FILE"; then
    sudo sed -i "/$MODULE"_module"/s/^#//g" "$CONF_FILE"
else
    echo "LoadModule ${MODULE}_module modules/mod_${MODULE}.so" | sudo tee -a "$CONF_FILE"
fi

sudo systemctl restart httpd

Unlike Debian/Ubuntu's a2enmod/a2dismod utilities, RedHat Enterprise Linux (RHEL) and CentOS handle Apache modules differently through direct configuration file editing. The process involves modifying the main configuration file or module-specific configuration.

First identify your Apache configuration directory (typically /etc/httpd/):

httpd -V | grep SERVER_CONFIG_FILE

Modules are usually stored in these locations:

/etc/httpd/conf.modules.d/   # Module configuration directory
/etc/httpd/conf/httpd.conf    # Main configuration file
/usr/lib64/httpd/modules/     # Module binaries (.so files)

To enable a module (e.g., rewrite_module):

# Check if module is already loaded
grep -i rewrite_module /etc/httpd/conf.modules.d/*.conf

# If not present, enable it by creating or editing a conf file
echo "LoadModule rewrite_module modules/mod_rewrite.so" | sudo tee -a /etc/httpd/conf.modules.d/00-rewrite.conf

# Verify the module is loaded
httpd -M | grep rewrite_module

To disable a module, either:

# Method 1: Comment out the LoadModule line
sudo sed -i 's/^LoadModule rewrite_module/#LoadModule rewrite_module/' /etc/httpd/conf.modules.d/00-rewrite.conf

# Method 2: Remove the module's conf file
sudo rm /etc/httpd/conf.modules.d/00-rewrite.conf

For those who prefer Ubuntu-style commands, create these bash scripts:

#!/bin/bash
# /usr/local/bin/a2enmod
if [ -z "$1" ]; then
    echo "Usage: a2enmod module_name"
    exit 1
fi

MODULE_FILE="/etc/httpd/conf.modules.d/00-${1}.conf"
if [ -f "$MODULE_FILE" ]; then
    echo "Module $1 is already enabled!"
    exit 0
fi

echo "LoadModule ${1}_module modules/mod_${1}.so" | sudo tee $MODULE_FILE > /dev/null
sudo systemctl restart httpd
echo "Module $1 enabled successfully"
  • mod_rewrite: URL rewriting
  • mod_ssl: HTTPS support
  • mod_deflate: Compression
  • mod_headers: HTTP headers manipulation

Use these commands to check module status:

# List all loaded modules
httpd -M

# Check if specific module is loaded
httpd -M | grep -i ssl_module

# View compiled-in modules
httpd -l

Always restart Apache after module changes:

sudo systemctl restart httpd

# For configuration syntax check first
sudo apachectl configtest
sudo systemctl restart httpd