Enabling/Disabling modules and sites in Apache - Ubuntu/CentOS

The usual configuration in the Apache Web Server stems from a single point - httpd.conf, the main configuration file. There might be other configuration files but they are mostly referred to from this central config file with the Include directive. For example, you might find lines like,

Include conf.d/

in the core configuration file. It is also not uncommon to find that these included configuration files have another Include directive in them which includes other files/directories.

The point to keep in mind is to not overuse this flexibility feature the Apache Web Server offers, since you end up only complicating things for yourself and more often than not, for the person after you.

Today, we’ll be looking at enabling/disabling modules and sites in the Apache Web Server.

Modules

Enabling & Disabling Modules in Debian & other Debian-based distros incl. Ubuntu

To enable modules, use,

$ sudo a2enmod <name-of-module>

## E.g.:

$ sudo a2enmod headers

When a module is enabled, a symlink to the respective module in the mods-available directory is created in the mods-enabled directory.

To disable a module, use,

$ sudo a2dismod <name-of-module>

## E.g.:

$ sudo a2dismod headers

When a module is disabled, the symlink created for the respective module in the mods-enabled directory is removed.

Enabling & Disabling Modules in CentOS & other RedHat based distros

Just as how the Include directive is used in the main config file, we could use the LoadModule directive to load modules into Apache.

You could enable a module by specifying the following line in a config file (the main config file or another config file that is read by Apache):

LoadModule <name-of-module> /path/to/shared-object-module-file.so

## E.g.:

LoadModule headers_module modules/mod_headers.so

Note: A server restart is mandatory after enabling/disabling a module. For Ubuntu and Debian-based distros - $ sudo service apache2 restart For CentOS and RedHat-based distros - $ sudo service httpd restart

Sites

Usually, when you have multiple sites running on the same server, you use VirtualHosts. It’s nicer to have each sites’ VirtualHosts configuration in their own separate files rather than put them all together.

Make sure you have the site-specific / VirtualHosts configuration file loaded and ready-to-go in a file, before we start playing with the next part.

A VirtualHosts configuration file, looks a little like this:

Do NOT use this VirtualHost configuration as yours, it is very much insecure and meant to be this way only for my local development web server!

<VirtualHost *:80>
	ServerName localhost
    
    ServerAdmin webmaster@localhost
    
    DocumentRoot /var/www
    <Directory "/var/www">
        Require all granted
        AllowOverride all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log vhost_combined
</VirtualHost>

Enabling & Disabling Sites in Debian & other Debian-based distros incl. Ubuntu

To enable sites, use,

$ sudo a2ensite <name-of-config-file>

## E.g.,

$ sudo a2ensite site1.conf

Similar to what happens with modules, when a site is enabled, a symlink to the config file at sites-available is created at sites-enabled

To disable sites, use,

$ sudo a2dissite <name-of-config-file>

## E.g.,

$ sudo a2dissite site1.conf

And just as with the modules, the symlink in sites-enabled is removed when a site is disabled.

Enabling & Disabling Sites in CentOS & other RedHat based distros

To enable sites, we could use the Include directive in our root httpd.conf file or any other configuration file that is read, to include our VirtualHost configuration files.

Include </path/to/config-file>

## E.g.,

## To include a bunch of vhosts,
Include /etc/httpd/sites-enabled/*.conf

## To include a single vhost config file,
Include /etc/httpd/sites/site1.conf

Note: A server configuration reload is mandatory after enabling/disabling a module. For Ubuntu and Debian-based distros - $ sudo service apache2 reload For CentOS and RedHat-based distros - $ sudo service httpd reload


Share on

           

comments powered by Disqus