English translation
Enabling and Disabling Apache2 Modules
In the previous article, we explored how to configure Apache2 virtual hosts in depth. While virtual hosts form the foundation of website hosting, Apache2’s capabilities extend far beyond this. To enhance server functionality and add specialized features, you can enable or disable various Apache2 modules. This section provides a detailed guide on managing these modules.
Overview of Apache2 Modules
In Apache2, modules are libraries of code that extend the web server’s functionality. They provide features such as URL rewriting, basic authentication, SSL/TLS support, and more. By default, Apache2 ships with several core modules enabled; however, many others are optional and must be manually activated.
Basic Commands for Enabling and Disabling Modules
Two essential commands for managing Apache2 modules are a2enmod and a2dismod.
-
To enable a module:
sudo a2enmod module_name -
To disable a module:
sudo a2dismod module_name
After enabling or disabling a module, restart Apache2 to apply the changes:
sudo systemctl restart apache2
Examples of Commonly Used Modules
1. URL Rewriting Module (rewrite)
mod_rewrite is one of the most widely used modules—it enables clean, user-friendly URLs through powerful rewriting rules.
Enabling mod_rewrite
Run the following command to enable it:
sudo a2enmod rewrite
Once enabled, you can define rewrite rules in your virtual host configuration.
For example, add the following block to your virtual host configuration to allow .htaccess overrides:
<Directory /var/www/html>
AllowOverride All
</Directory>
Then, create a .htaccess file in your site’s root directory (e.g., /var/www/html/.htaccess) and include rewrite rules like:
RewriteEngine On
RewriteRule ^old-page\.html$ new-page.html [R=301,L]
2. SSL Module (ssl)
To serve content over HTTPS, you must enable the mod_ssl module.
Enabling mod_ssl
Execute the following command:
sudo a2enmod ssl
After enabling, configure SSL settings in your virtual host—e.g., for port 443:
<VirtualHost *:443>
ServerName www.example.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/ssl/certs/your_certificate.crt
SSLCertificateKeyFile /etc/ssl/private/your_private_key.key
</VirtualHost>
⚠️ Note: Ensure valid SSL certificates are installed before enabling HTTPS.
3. Caching Modules (headers, expires)
Enabling caching modules improves page load performance by instructing browsers to store static assets locally.
Enabling mod_headers and mod_expires
sudo a2enmod headers
sudo a2enmod expires
Then, configure cache behavior—for instance, add the following to your virtual host configuration:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 month"
</IfModule>
Checking Module Status
To list all currently enabled modules, run:
apache2ctl -M
This outputs a complete list of active modules—useful for verifying whether a specific module has been successfully enabled.
Summary
In this section, we covered how to enable and disable Apache2 modules, along with practical configuration examples for several commonly used ones. By selectively enabling modules aligned with your application’s needs, you can significantly enhance both functionality and performance. Next, we’ll discuss uploading website files to the server—an essential final step in completing your web deployment.
Continue