This is an old revision of the document!


Apache Configuration

This page describe how to configure a website with Apache.

Structure

A configuration file for Apache use a structure, for example :

<VirtualHost *:80>
        Servername www.bouthors.fr
        ServerAdmin matthieu@bouthors.fr
 
        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
 
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
                RedirectMatch ^/$ /wiki/
        </Directory>
</VirtualHost>

Is this example, the tags are VirtualHost and Directory, and each section contains several options like “Servername”, “DocumentRoot”, “Options”, “allow”, …

Common tags are :

  • <VirtualHost> defines virtual hosting
  • <Directory> defines directives for a folder on the file system
  • <Location> defines directives based on the url path

Pulish a folder

The first step is to link the file system to an url path with one of the following way :

  • DocumentRoot allows to map the root url ”/”, for example :
  DocumentRoot /var/www
  • Alias allows to map a part of the webserver, for example ”/wiki” :
  Alias /wiki /opt/dokuwiki

Then, the requests have to be autorized with the following options:

  • Allow allows requests based on IP, DNS or a variable
  • Deny denies requests based on IP, DNS or a variable
  • Order defines precedence between “allow” and “deny” options, if a request match both “allow” and “deny” :
    • Order Allow,Deny will deny the request
    • Order Deny,Allow will allow the request

Complete example :

  Alias /wiki /opt/dokuwiki
  <Directory /opt/dokuwiki>
    allow from all
    Order deny,allow
  </Directory>

Apache also allows additional parameters with “option” which can have several values :

  • None : no options
  • All : all features except MultiViews
  • ExecCGI : allows CGI exectution
  • FollowSymLinks : allows to access symbolic links
    The destination of the link must be readable by Apache, be carefull with the security.
    This option is not available with <Location>
  • Includes : allows “Includes”
  • IncludesNOEXEC : allows “Includes” but will disable functions #exec cmd and #exec cgi
  • Indexes : allows directory listing
  • MultiViews : allows the use of mod_negociation
  • SymLinksIfOwnerMatch : similar to FollowSymlinks but only allows links with the same owner

Example with symbolic link authorization :

  Alias /wiki /opt/dokuwiki
  <Directory /opt/dokuwiki>
    Options FollowSymLinks
    allow from all
    Order deny,allow
  </Directory>

It is possible to use ”+ and ”-” to define modification from inherited options. For example to add directory listing to a subfolder (without removing other options) :

  <Directory /opt/dokuwiki/share>
    Options +Indexes
  </Directory>

Finally, it is possible to change the options with ”.htaccess” files located directly on the file system. To allow the use of ”.htaccess”, the option “AllowOverride” must be defined. For example :

Alias /wiki /opt/dokuwiki
<Directory /opt/dokuwiki/>
        Options +FollowSymLinks
        AllowOverride All
        order allow,deny
        allow from all
</Directory>

.htaccess example which deny all access to the folder :

order allow,deny
deny from all

Note : it is possible to apply options based on url path instead of file system with the tag <Location> instead of <Directory>.

Customization

It is possible to customize the server with the following options :

  • ServerAdmin defines the email displayed in error messages
  • ServerSignature defines the signature displayed on server generated pages

Example :

        ServerAdmin matthieu@bouthors.fr

Virtual hosting, redirects and reverse proxy

Apache has several useful features :

  • Virtual hosting used to host several websites on a single public IP
  • redirecting users to another url, also used to redirect the root to another part of a site
  • rewriting with reverse proxy to forwarding the request to another server

Virtual hosting

L'hébergement local de plusieurs site se fait grâce aux options suivantes :

  • NameVirtualHost décrit quelles IP hébergent des virtual host
  • <VirtualHost> définit un virtual host
  • ServerName définit le nom d'hôte du <VirtualHost>
  • ServerAlias permet d'ajouter d'autres noms en plus de ServerName

Par exemple pour héberger www.bouthors.fr et web3.bouthors.fr sur le même apache avec deux configurations différentes :

<VirtualHost *:80>
        ServerName www.bouthors.fr
        ServerAlias ipv6.bouthors.fr
        DocumentRoot /var/www
 
        ...
 
</VirtualHost>
<VirtualHost *:80>
        ServerName web3.bouthors.fr
        DocumentRoot /var/www
 
        ...
 
</VirtualHost>

Redirections

Les redirections sont très utilisées, elles permettent d'indiquer à l'utilisateur de faire sa requête auprès d'une autre url.

Les commandes utilisées sont :

  • Redirect redirige un client d'une URL vers une autre. Redirige également toutes les sous URL
  • RedirectMatch redirige les URL correspondant à une expression régulière

Exemple de redirection de la racine d'un serveur web :

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
                RedirectMatch ^/$ /wiki/
        </Directory>

Exemple de redirection inconditionnelle par défaut :

<VirtualHost *:80>
        RedirectMatch 301 ^/(.*) http://www.bouthors.fr/$1
 
</VirtualHost>

Reverse Proxy

Apache peut également agir en tant que reverse proxy et transmettre la requête à un autre serveur web. Le module mod-proxy est alors utilisé.

Il faut dans un premier temps activer mod-proxy :

# a2enmod proxy_http
Considering dependency proxy for proxy_http:
Enabling module proxy.
Enabling module proxy_http.
Run '/etc/init.d/apache2 restart' to activate new configuration!
# /etc/init.d/apache2 restart

Puis utiliser la directive ProxyPass. Par exemple rediriger le répertoire /wiki vers le serveur web4 :

ProxyPass /wiki/ http://web4.bouthors.fr/wiki/

Paramètres supplémentaires

Include

Include est utilisé pour insérer un autre fichier.

Exemple (à la fin de apache2.conf) :

# Include generic snippets of statements
Include conf.d/
 
# Include the virtual host configurations:
Include sites-enabled/

Configuration de PHP

Dans /etc/php5/apache2/php.ini configurer la taille maximum d'upload autorisé, par exemple :

upload_max_filesize = 15M

Il est également nécessaire de modifier la taille des posts :

post_max_size = 8M

Dans les cas ou il faut beaucoup de mémoire, modifier memory_limit :

memory_limit = 128M

Log

Les logs peuvent être contrôlées de différentes façons, voici quelques paramètres fréquents :

  • CustomLog : Définit le nom du fichier de log et son format
  • ErrorLog : Définit où les logs d'erreur sont envoyées
  • LogLevel : Définit le niveau de verbosité

Exemple :

       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

Encodage des caractères spéciaux

Le fichier /etc/apache2/conf.d/charset définit le Content-Type renvoyé par Apache.

Par défaut, il est en UTF8 :

AddDefaultCharset UTF-8

Pour configurer le serveur en ISO, il faut indiquer :

AddDefaultCharset ISO-8859-15

Links

en/linux/serveur_web/config.1323257819.txt.gz · Last modified: 2011/12/07 12:36 by matthieu
Recent changes RSS feed Debian Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki