Home Tutorials Training Consulting Products Books Company Donate Contact us

NOW Hiring

Quick links


1. Installation and configuration of the Apache HTTP server

This articles contains information about the Apache HTTP server.

2. Apache HTTP

The Apache HTTP server project develops and maintains an open-source HTTP server. The Apache HTTP web server is one of the most used web server worldwide.

3. Installation and configuration

On Ubuntu you can install the Apache HTTP server with the following command.

sudo apt-get install apache2

4. Configuration files in Apache HTTP

The following describes the configuration files and commands for the Apache HTTP web server on Ubuntu.

4.1. Important Apache HTTP commands

Use the following command to list the available modules of the Apache HTTP server.

/etc/init.d/apache2 -l
/etc/init.d/apache2 restart

4.2. Configuration Files

The main configuration file for the Apache Http server is the /etc/apache2/apache2.conf file. The error log of Apache is located in the /var/log/apache2/error.log file.

4.3. Multi-Processing-Module (MPMs)

Apache HTTP can run in different modes. These modes determine how the web requests of users are answered. There are called Multi-Processing-Module (MPMs).

The selected mode is compiled into the server and can be seen via the following command.

sudo apachectl -V | grep -i mpm

The configuration for the event mpm is stored in /etc/apache2/mods-available/mpm_event.conf. Configure only the module which your server is using.

The following listing shows a configuration for a high traffic web server using the event module.

<IfModule mpm_event_module>
    StartServers             200
        ServerLimit      600
    MinSpareThreads      600
    MaxSpareThreads      2000
    ThreadLimit          64
    ThreadsPerChild      50
    MaxRequestWorkers     15000
    MaxConnectionsPerChild   10

4.4. Checking for Apache HTTP problems

The error.log file contains the error messages of the Apache HTTP server. For example to check for too many simultaneous request you can run.

grep MaxClients /var/log/apache2/error.log

// If there are problems you might get something like this:
// server reached MaxClients setting, consider raising the MaxClients setting

4.5. Apache Configuration via .htaccess

Use the file ".htacess" to configure certain behavior of Apache HTTP. One major application of this file is to redirect an URL to other URL’s.

The following .htacess file reroutes http://vogella.com to http://www.vogella.com. It also redirect access to a certain webpage (/articles/SpringFramework/article.html) to another webpage via a 301 redirect. The 301 redirect will tell search engines that this side has moved and is the recommended way to move webpages.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.vogella\.de$
RewriteRule ^(.*)$ http://www.vogella.com/$1 [L,R=301]
redirect 301 /articles/SpringFramework/article.html http://www.vogella.com/tutorials/SpringDependencyInjection/article.html

4.6. Using modules on Apache Http

Apache Http supports the usage of modules. To enable modules use the a2enmod command, e.g. a2enmod rewrite to enable the rewrite module.

4.7. Performance - Turn on gzip compression

To optimize the download time of your webpages you can turn on gzip compression. This requires the Apache module "mod_deflate" which can be installed by the following command:

a2enmod deflate
sudo /etc/init.d/apache2 restart

The compression can be activated in the default configuration file for this module located in /etc/apache2/mods-available/deflate.conf or via the ".htaccess" file.

# compress all text & html:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

[[Other Apache modules]] == Supporting php and wordpress

sudo apt-get install libapache2-mod-fcgid
sudo apt-get install php5-cgi

Afterwards activate the corresponding modules.

sudo a2enmod fastcgi
sudo a2enmod proxy
# required for wordpress blog
sudo a2enmod rewrite

4.8. Using the pagespeed module

You should also install the pagespeed module from Google, as this compresses the content of the website and results in a much faster download. See http://tecadmin.net/install-apache2-with-mod-pagespeed-on-ubuntu/ for the setup.

4.9. Migration of servers

In case you are migrating your server from one server to another you can tell your local computer to resolve to the new server, even though you did not yet change the DNS setting. This way, you can test if the new server works fine.

5. Configuring virtual hosts in Apache

5.1. What are virtual hosts in Apache?

Virtual hosts allow Apache2 to be configured for multiple sites that have separate configurations.

This allows you to have one Apache HTTP web server running on one IP serving multiple domains.

5.2. How to configure vhosts under Ubuntu

Under Ubuntu you create a configuration file in the /etc/apache2/sites-available folder, for example the vogella.conf.

<VirtualHost *:80>
    ServerName www.vogella.com
    ServerAdmin test@test.com
    ServerAlias vogella.de www.vogella.de vogella.com www.vogella.org vogella.org

    DocumentRoot /var/www/vhosts/vogella/www
    <Directory /var/www/vhosts/vogella/www>
        Options -Indexes
        AllowOverride all
        Order allow,deny
        allow from all

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
        CustomLog ${APACHE_LOG_DIR}/www.vogella.com-access.log combined


You enable or disable virtual hosts with the following command.

#enable the vhost
sudo a2ensite vogella.conf

#disable the vhost
sudo a2dissite www.vogella.com

After enabling sites you have to reload the Apache HTTP server configuration with the sudo service apache2 reload command.

6. About this website

7. Apache HTTP links

7.1. vogella GmbH training and consulting support


The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2017 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.