Home Tutorials Training Consulting Books Company Contact us


Get more...

Installation and configuration of the Apache HTTP server

This articles contains information about the Apache HTTP server.

1. 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.

1.1. Installation

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

sudo apt-get install apache2

1.2. Starting Apache

To start the Apache service, use the following command.

sudo service apache2 start

Use the following commands to check if the Apache configuration is valid and to reload.

sudo apache2ctl configtest
sudo service apache2 reload

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

/etc/init.d/apache2 -l

1.3. 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.

1.4. 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
</IfModule>

1.5. 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

1.6. 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 ^(.*)$ https://www.vogella.com/$1 [L,R=301] 
redirect 301 /articles/SpringFramework/article.html https://www.vogella.com/tutorials/SpringDependencyInjection/article.html

1.7. 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.

1.8. 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

1.9. 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.

1.10. 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 whether the new server works fine.

2. Configuring virtual hosts in Apache

2.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.

2.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
    </Directory>

    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


</VirtualHost>

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

3. Apache HTTP links