NOW Hiring

Quick links

1. Installation and configuration of the Apache HTTP server

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

2. Apache HTTP

The Apache HTTP server project develops and maintains an open-source HTTP server for modern operating system including UNIX and Windows NT. The Apache HTTP web server is still the most used web server worldwide. == Installation and configuration

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

sudo apt-get install apache2

3. Configuration files in Apache HTTP

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

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

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

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

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

3.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
/etc/init.d/apache2 restart

The compression can be activate in the default configuration file for this module located in /etc/apache2/mods-available/deflate.conf or 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

[[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

3.8. Configuring your server to send out emails

In case you want to send out emails from your PHP sides you have to configure an email client. A simple one is exim4, a setup description can be found under the following links: https://help.ubuntu.com/community/Exim4 and https://wiki.debian.org/GmailAndExim4

sudo apt-get install exim4

Here is an example /etc/exim4/update-exim4.conf.conf file

dc_eximconfig_configtype='smarthost'
dc_other_hostnames=''
dc_local_interfaces='127.0.0.1 ; ::1'
dc_readhost='gilbert.vogella.com'
dc_relay_domains=''
dc_minimaldns='false'
dc_relay_nets=''
dc_smarthost='smtp.gmail.com:587'
CFILEMODE='644'
dc_use_split_config='true'
dc_hide_mailname='false'
dc_mailname_in_oh='true'

You need to enter valid user credentials in the passwd.client file in the following format.

*google.com:youruser@gmail.com:yourpassword

Afterwards you need to enable the new configuration with the following command.

3.9. Using the pagespeed module

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

3.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 if the new server works fine.

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.

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</filename.

Unresolved directive in 200_vhosts.adoc - include::examples/vhosts/www.vogella.com[]

You enable or disable virtual hosts 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.

5. About this website

6. Apache HTTP links

6.1. vogella GmbH training and consulting support

TRAINING SERVICE & 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-2016 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.