You are here: Home / Debian GNU/Linux / Servers / Apache 2 / Install Apache 2 on Debian

Install Apache 2 on Debian

by Pierre-Yves Landuré last modified Mar 02, 2018 08:35

Apache is a commonly used HTTP server. It is known for its modularity and the flexibility of its usages. This guide help you setup Apache 2 on Debian GNU/Linux. It accelerates the creation of basic virtualhosts.

This howto is tested on:

  • Debian 5.0 Lenny
  • Debian 6.0 Squeeze
  • Debian 7.0 Wheezy
  • Ubuntu 13.10 Saucy Salamander


There is 3 Apache 2 versions in Debian repositories :

  • apache2-mpm-worker : Multi-threaded version able to manage a great number of incoming connexions with a low memory consumption.
  • apache2-mpm-event : apache2-mpm-worker based version with a better Keep-Alive support. This version is experimental in Apache 2.2 (Debian 6.0 Squeeze and below).
  • apache2-mpm-prefork : Version without multi-thread support needed to use mod_php5. This version is not optimal. It is best to use PHP-FPM with apache2-mpm-worker.

Install the HTTP server :

command apt-get install apache2-mpm-worker

Enable rewrite module :

command a2enmod rewrite

Enable named virtual hosts in order to host many domains on one IP address :

[[ -z "$(command grep 'NameVirtualHost.*:80' /etc/apache2/ports.conf)" ]] \
&& command sed -i -e '/Listen[\t ]*80/i\
NameVirtualHost *:80' '/etc/apache2/ports.conf'
[[ -e "${default_site}" ]] \
&& || default_site='/etc/apache2/sites-available/000-default.conf'
[[ -e "${default_site}" && -z "$(command grep 'VirtualHost[ \t]*\*:80' "${default_site}")" ]] \
&& command sed -i -e 's/\(VirtualHost[ \t]*\*\)>/\1:80>/g' \
-e 's/^\(NameVirtualHost.*\)$/#\1/g' \

Reload the configuration :

command type -f 'service' &>'/dev/null' && command service 'apache2' 'force-reload'
command type -f 'service' &>'/dev/null' || /etc/init.d/apache2 'force-reload'


Redirect emails sent to www-data user to the root user:

[[ -e '/etc/aliases' && -z "$(command grep '^www-data:' '/etc/aliases' )" ]] \
  && command sed -i -e '/^root:/i\
www-data: root' \

Reload aliases list:

command type -f 'newaliases' &>'/dev/null' && command newaliases


This howto recommands :


This howto recommands enabling PHP 5 with:

Note : if you don't wish to use PHP-FPM, install PHP 5 with:

# command apt-get install libapache2-mod-php5
# command a2enmod php5 # command type -f 'service' &>'/dev/null' && command service 'apache2' 'force-reload'
# command type -f 'service' &>'/dev/null' || /etc/init.d/apache2 'force-reload'


The following commands are available to manage the Apache 2 server :

  • a2enmod: Enable a module (the available modules configurations are in '/etc/apache2/mods-available').
  • a2dismod: Disable an active module (the enabled modules configurations are in '/etc/apache2/mods-enabled')
  • a2ensite: Enable a site (the available sites configurations are in '/etc/apache2/sites-available')
  • a2dissite: Disable a site (the enabled sites configurations are in '/etc/apache2/sites-enabled')
  • apache2ctl: Advanced administration tool for Apache 2.

Test Apache 2 configuration (prevent server crashs due to errors in configuration) :

# command apache2ctl -t

    Reload server configuration without interrupting the service :

    # command type -f 'service' &>'/dev/null' && command service 'apache2' 'reload'
    # command type -f 'service' &>'/dev/null' || /etc/init.d/apache2 'reload'

    Reload the server configuration by restarting the server without closing existing connections (no HTTP error) :

    # command type -f 'service' &>'/dev/null' && command service 'apache2' 'force-reload'
    # command type -f 'service' &>'/dev/null' || /etc/init.d/apache2 'force-reload'


    Reload the server configuration by closing existing connections (a HTTP error is displayed to the user) :

    # command type -f 'service' &>'/dev/null' && command service 'apache2' 'restart'
    # command type -f 'service' &>'/dev/null' || /etc/init.d/apache2 'restart'

    List enabled Apache 2 modules :

    # command apache2ctl -t -D DUMP_MODULES 


    These books can help you: