Apache2’s functionality can be extended using modules, which are pieces of software that add specific features to the server. Modules can handle a variety of tasks such as enabling SSL, rewriting URLs, serving dynamic content, caching, and authentication.
Apache uses a modular architecture where features are added or removed as needed, helping to reduce the overhead on the server by only loading necessary functionality.
Apache comes with built-in tools for managing modules:
Enable a module:
sudo a2enmod module_name
sudo systemctl restart apache2
Disable a module:
sudo a2dismod module_name
sudo systemctl restart apache2
List all enabled modules:
apache2ctl -M
Purpose: Provides a powerful way to rewrite URLs on the server side. It’s commonly used for URL redirection, SEO-friendly URLs, and more.
Enable:
sudo a2enmod rewrite
Example usage (in .htaccess
):
RewriteEngine On
RewriteRule ^old-page.html$ new-page.html [R=301,L]
Purpose: Adds support for SSL/TLS, enabling secure communication between clients and the server.
Enable:
sudo a2enmod ssl
Usage:
SSLCertificateFile
and SSLCertificateKeyFile
in the virtual host configuration for port 443.Purpose: Enables caching of content to improve performance by reducing the load on backend systems and speeding up content delivery.
Enable:
sudo a2enmod cache
sudo a2enmod cache_disk
Usage:
Purpose: Enables Apache to act as a reverse proxy, forwarding client requests to other servers. It’s commonly used for load balancing and distributing traffic to multiple backend servers.
Enable:
sudo a2enmod proxy
sudo a2enmod proxy_http
Usage:
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://backend-server/
ProxyPassReverse / http://backend-server/
</VirtualHost>
Purpose: Provides data compression to reduce the size of transmitted files, improving page load speeds and reducing bandwidth usage.
Enable:
sudo a2enmod deflate
Usage:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml
</IfModule>
Purpose: Provides control over HTTP headers. You can modify, add, or remove headers in the HTTP response, which is useful for enhancing security (e.g., adding CORS headers or security headers).
Enable:
sudo a2enmod headers
Usage:
.htaccess
file:Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "DENY"
Purpose: Implements Basic Authentication, which allows access control based on username and password.
Enable:
sudo a2enmod auth_basic
Usage:
<Directory "/var/www/html/secure">
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
To create a .htpasswd
file for storing users and passwords:
sudo htpasswd -c /etc/apache2/.htpasswd username
Purpose: Automatically generates directory listings when no index file (like index.html
) is present.
Enable:
sudo a2enmod autoindex
Usage:
Options
directive:<Directory /var/www/html>
Options Indexes FollowSymLinks
</Directory>
Purpose: Enables Apache to execute PHP scripts. For dynamic content with PHP, Apache can use mod_php. Alternatively, mod_cgi allows other scripting languages, like Python and Perl, to be executed.
Enable:
sudo apt install libapache2-mod-php
sudo a2enmod php
Usage:
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
Purpose: These are Multi-Processing Modules (MPMs), which control how Apache handles connections. You can enable one at a time based on your server’s needs:
Enable:
sudo a2enmod mpm_event
sudo a2dismod mpm_prefork
sudo systemctl restart apache2