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

Hosting of several websites :

  • NameVirtualHost defines which IP are hosting virtual hosts
  • <VirtualHost> defines a virtual host
  • ServerName defines the hostname of the current <VirtualHost>
  • ServerAlias allows to define additional hostnames

For example to host www.bouthors.fr and web3.bouthors.fr on the same Apache with two different configurations :

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

Redirects

Redirects are often used, they allow to ask the user to request another url.

Options used are :

  • Redirect redirect all requests to another URL. This directive map a path to a new path.
  • RedirectMatch redirect requests matching a regex

Example with the redirection of the root only :

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

Example with the redirection of all requests :

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

Reverse Proxy

Apache can also become a reverse proxy and forward requests to other web servers. In this case, mod-proxy module is used.

First, mod-proxy needs to be enabled :

# 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

Then the ProxyPass command must be used. For example, to redirect /wiki to the server “web4” :

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

Additional parameters

Include

Include is used to insert another configuration file.

Example (end of apache2.conf) :

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

PHP Configuration

Inside /etc/php5/apache2/php.ini to configure the maximum upload size :

upload_max_filesize = 15M

To change the maximum post size :

post_max_size = 8M

If you need a lot of memory, change memory_limit :

memory_limit = 128M

Log

Logs can be controled by different ways :

  • CustomLog : defines the log name and log format
  • ErrorLog : defines where error logs are sent
  • LogLevel : defines the level of verbosity

Example :

       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

Charset Encoding

The file /etc/apache2/conf.d/charset defines the Content-Type used by Apache.

By default, UTF8 is used :

AddDefaultCharset UTF-8

To use ISO instead, the following option can be used :

AddDefaultCharset ISO-8859-15

Links

en/linux/serveur_web/config.txt · Last modified: 2012/01/06 10:33 by matthieu
Recent changes RSS feed Debian Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki