Apache Modules - Basics and Configurations.
Tags: devops, tutorial, cloud, apache
Published: 2022-03-14 12:20:00

## What are the modules

Apache2 is a modular server. This implies that only the most basic functionality is included in the core server. Extended features are available through modules that can be loaded into Apache2. By default, a base set of modules is included in the server at compile-time. If the server is compiled to use dynamically loaded modules, then modules can be compiled separately and added at any time using the LoadModule directive. Otherwise, Apache2 must be recompiled to add or remove modules.

Ubuntu compiles Apache2 to allow the dynamic loading of modules. Configuration directives may be conditionally included in the presence of a particular module by enclosing them in a <IfModule> block.

You can install additional Apache2 modules and use them with your Web server. For example, run the following command at a terminal prompt to install the Python 3 WSGI module:


```
sudo apt install libapache2-mod-wsgi-py3
```

The installation will enable the module automatically, but we can disable it with a2dismod:

```
$ sudo a2dismod wsgi
$ sudo systemctl restart apache2.service

## And then use the a2enmod utility to re-enable it:

$ sudo a2enmod wsgi
$ sudo systemctl restart apache2.service

```
See the /etc/apache2/mods-available directory for additional modules already available on your system.



## Apache2 core and default modules

1) Mod_security

2) Mod_rewrite

3) Mod_deflate

4) Mod_cache

5) Mod_proxy

6) Mod_ssl

7) mpm_event_module

8) mime_module

We will learn more about these modules in the next lesson and how,why and when to use the modules.



## Modules managing commands

> Enabled modules: 

```
$ ls /etc/apache2/mods-enabled/
```
> Available modules: 
```
$ ls /etc/apache2/mods-available/
```

> List installed Modules
```
$ apachectl -M
```


## Basic module configuration

- Install Modules
- Enable module
- Use module in virtualhost directive


We will install and configure the caching module.

1) Install the caching module using `apachhe2-utils`

Open the terminal and run the following commands:

```
$ sudo apt-get update
$ sudo apt-get install apache2-utils
```

2) Enable Apache cache modules

```
$ sudo a2enmod cache
$ sudo a2enmod cache_disk
$ sudo a2enmod expires
$ sudo a2enmod headers
```

3) Restart Apache Web Server

```
$ sudo systemctl restart apache2
```

4) Cross check the installed and enable modules with this command

```
$ apachectl -M
OR
$ apachectl -t -D DUMP_MODULES 
OR
or on RHEL,CentoS, Fedora:
$ httpd -M
```
5) Configure module in virtualhost directive

Add the following file in the Virtual Host Config:

**Method 1**: Install caching on domain-specific scope follow this method.

Open the "example.com.conf"

```
sudo vi /etc/apache2/sites-available/example.com.conf
```

> Add inside the <VirtualHost/>
```
<VirtualHost *:80>

## Caching_static_contents
CacheQuickHandler off
CacheLock on
CacheLockPath /tmp/mod_cache
CacheLockMaxAge 5
CacheIgnoreHeaders Set-Cookie

<Location />
CacheEnable disk
CacheHeader on
CacheDefaultExpire 800
CacheMaxExpire 64000
CacheIgnoreNoLastMod On
ExpiresActive on
ExpiresDefault A300
</Location>

</VirtualHost>

```

**Method 2**

If you want to do it across your entire Apache web server, or you don’t use virtual hosts, then you can update the Apache server configuration file.

Open the defualt-webserver conf file:

```
sudo vi /etc/apache2/sites-available/000-default.conf
```

Scroll to the bottom of the file and add the following lines just before the line
 
**</   VirtualHost >**