How To Configure Apache on Ubuntu 14.04 LTS Production Server ( Part II )

In part I, we got our servers up and running. Just in case here is How To Install LAMP / LEMP Ubuntu 14.04 LTS Production Server. In Part II, we configure Apache and its modules.

Apache

The first step is to ensure that Apache server is running well under default settings and the easiest way to do that is to navigate to server’s IP address using your browser.

http://your_server_IP_address

It would load Ubuntu’s default Apache web page (screenshot below).

apache-default

In Ubuntu, the main configuration files for Apache are located at /etc/apache2, with the following directory structure.

|-- apache2.conf
|
|-- ports.conf
|
|-- mods-available
|       |-- *.load
|       `-- *.conf
|-- mods-enabled
|       |-- *.load
|       `-- *.conf
|
|-- conf-available
|       `-- *.conf
|-- conf-enabled
|       `-- *.conf
|
|-- sites-available
|       `-- *.conf
|-- sites-enabled
|       `-- *.conf
|
|-- envvars
|
|-- magic

Lets go through each config file and their functions.

  • apache2.conf is the main configuration file that brings together all the different configuration files for the web-server. In-fact, you can have all your configuration done here ( don’t do it ).

  • ports.conf configures the port that the virtual host listens to for incoming requests by default port ’80’ for normal and port ‘443’ for SSL. It must be included into apache2.conf.

  • conf-enabled contains symlinks of enabled configurations.

  • conf-available contains snippets of customised configuration .conf files that could be enabled or disabled using the following commands

    • Enable Configuration
    sudo a2enconf name-of-config-file
    
    • Disable Configuration
    sudo a2disconf name-of-config-file
    
  • Apache Modules – these are pieces of software that adds functionality to Apache server. A typical Apache web server will come with a common set of modules pre-installed which could be seen using the following command.
apache2ctl -M
  • mods-enabled contains symlinks of enabled modules.
  • mods-available contains all the available modules that can be added to your Apache configuration.
    Here are some handy commands to help you install, enable, & disable Apache modules.

Install Apache Module

sudo apt-get install module-name
  • Note that you might need to download your module first before you could install. Download sources could be found modules documentation.

  • Enable Apache Module

sudo a2enmod module-name
  • Disable Apache Module
sudo a2dismod module-name

Apache2.conf

Now, that we have a idea on the bits and bottles that make up the Apache configuration lets have a closer look at the main configuration file. The file contains directives for resources, processes and virtual hosts.

Resources

Section to include mods-enabled and conf-enabled configuration files along with other additional resources.

Process

Directives that control the way in which Apache handles requests

MPM Module Configuration

MPM extends the modular design of Apache using multi threading. This provides improved performance and scalability for modern web applications with minimal resource footprint. Even though Apache comes with several MPM modules like Event and Worker, one needs to install and then compile Apache before usage. If you are on Apache 2.4.X then MPM Event is your best bet with Apache moving its status to ‘Stable’ in Apache 2.4.X from ‘Experimental’ in Apache 2.2.

a2enmod mpm_event

If the module has been enabled you would see configuration details in the main apache2.conf file. Make sure you tweak the settings according to your needs.

Timeout

The time out options defines the time Apache waits for a request.The default setting at ‘300’ seconds is too high in most cases and could be reduced to ’60’ or ’90’ seconds.

KeepAlive

This option directs Apache to keep HTTP sessions alive for stipulated time. This reduces the need for multiple TCP connection which in turn reduces latency. It’s important that you enable this option by setting it to ‘On’ to have a good server response time.

MaxKeepAliveRequests

Directive to control the number of requests per connection.Setting it to ‘0’ will allow unlimited requests but the resources could be vulnerable to abuse. You could start of with a relatively safe value of

MaxKeepAliveRequests 300

and work your way up or down depending on your needs.

KeepAliveTimeout

Number of seconds Apache will wait before ending connections. Keeping a high threshold timeout can reduce performance if there are resource bottlenecks, as it uses resources to serve idle connections. So, striking a balance between performance and resource usage is key.

Virtual Hosts

Virtual hosts allow multiple sites to be hosted on a single Apache server.The default virtual host file for Apache document root is located in sites-available directory.

You can have a look at default settings to gain a perspective into replicating that file to host more websites. Use the following command to open the default virtual host file

sudo nano /etc/apache2/sites-available/000-default.conf

Default virtual host file content

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Require all granted
        </Directory>

Note: The directive for access control Apache 2.4 has been changed from Apache 2.2

In 2.2

Order allow,deny
Allow from all

In 2.4

Require all granted

Similarly to grant access control for hostnames.

In 2.2

Order Deny,Allow
Deny from all
Allow from smartpixels.net

In 2.4

Require host smartpixels.net

The default virtual host file is configured to listen to port ’80’ and serve webpages from the default public directory at /var/www/html on every request but the default settings can be overruled using additional virtual host files and their settings for creating virtual servers.

Often Server Alias directive is used to create name-based virtual hosts.Example name-based virtual host configuration file.

<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com *.domain.com
DocumentRoot var/www/domain.com
</VirtualHost>

Apache needs to be restarted for the changes to take effect.

sudo service apache2 restart

Conclusion

We have moved a little closer in getting our production server ready by configuring HTTP server. We still don’t have an optimised server but that’s a separate tutorial by itself.We configure PHP and MySQL/MariaDB in the next couple of tutorials.

Subscribe and stay connected.

Leave a Reply

Your email address will not be published. Required fields are marked *