Support free tutorials:











vogella training Training Books



Apache HTTP Server - Tutorial

Lars Vogel

Version 1.1

23.05.2013

Revision History
Revision 0.1 21.04.2010 Lars
Vogel
created
Revision 0.2 - 1.1 27.05.2011 - 23.05.2013 Lars
Vogel
bug fixed and enhancements

Apache HTTP

This articles contains some information about handling the Apache HTTP server.


Table of Contents

1. Apache HTTP
2. Installation
3. Apache HTTP
3.1. Commands
3.2. Configuration Files
3.3. Multi-Processing-Module (MPMs)
3.4. Checking for Apache HTTP problems
3.5. Apache Configuration via .htaccess
3.6. Performance - Turn on gzip compression
4. Configuring virtual hosts in Apache
4.1. What are virtual hosts in Apache?
4.2. How to configure vhosts under Ubuntu
5. Support this website
5.1. Thank you
5.2. Questions and Discussion
6. Links and Literature

1. Apache HTTP

The Apache HTTP server Ppoject is an effort to develop and maintain an open-source HTTP server for modern operating systems including UNIX and Windows NT.

The Apache HTTP web server is still the most used web server worldwide.

2. Installation

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

sudo apt-get install apache2 

The Apache HTTP server provides a selection of Multi-Processing Modules (MPMs) which are responsible for binding to network ports on the machine, accepting requests, and dispatching children to handle the requests. On Ubuntu you can select one of them via one of the following commands.

# install the event MPM
# this is supposted to be less resource intensive
sudo apt-get install apache2-mpm-event

# install the worker MPM
sudo apt-get install apache2-mpm-worker

# install the worker MPM
sudo apt-get install apache2-mpm-prefork 

3. Apache HTTP

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

3.1. Commands

Table 1. 

Command Description
/etc/init.d/apache2 -l List the available modules of Apache
/etc/init.d/apache2 restart Restart the apache2 web server

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

3.3. Multi-Processing-Module (MPMs)

Apache HTTP can be running in different modes. These modes determine how the web requests of users are answered. These modes are called Multi-Processing-Module (MPMs). The selected mode is compiled into the server and can be seen via the following command.

apache2 -l 

You find several sections in apache2.conf for configuring the different modules. Configure only the module which your server is using. For example if you see the modul "prefork.c" then you only need to configure the "mpm_prefork_module".

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

<IfModule mpm_prefork_module>
StartServers       20
MinSpareServers    20
MaxSpareServers   30
MaxClients  80
MaxRequestsPerChild   10000
</IfModule> 

3.4. Checking for Apache HTTP problems

"error.log" contains the error messages of Apache. For example to check for too many simulatanous 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 

3.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 URL to other URL's.

The following .htacess file reroutes that 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 

3.6. Performance - Turn on gzip compression

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

a2enmod deflate 
/etc/init.d/apache2 restart 

The compression can be activate via your ".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 

4. Configuring virtual hosts in Apache

4.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. Of course you still need to configure the DNS settings for the domains to map to the server but if you do this, you can

4.2. How to configure vhosts under Ubuntu

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

<VirtualHost 144.76.74.162: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.com/documentroot
  <Directory /var/www/vhosts/vogella.com/documentroot>
    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 sites (vhosts) with the following command.

#enable the vhost
sudo a2ensite www.vogella.com

#disable the vhost
sudo a2dissite www.vogella.com 

After enabling sites you have to restart your Apache HTTP server.

5. Support this website

This tutorial is Open Content under the CC BY-NC-SA 3.0 DE license. Source code in this tutorial is distributed under the Eclipse Public License. See the vogella License page for details on the terms of reuse.

Writing and updating these tutorials is a lot of work. If this free community service was helpful, you can support the cause by giving a tip as well as reporting typos and factual errors.

5.1. Thank you

Please consider a contribution if this article helped you.

Flattr this

5.2. Questions and Discussion

If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the vogella FAQ as I don't respond to questions already answered there.

6. Links and Literature

http://httpd.apache.org/ Apache HTTP Homepage