This page describe how to configure a website with Apache.
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 :
The first step is to link the file system to an url path with one of the following way :
DocumentRoot /var/www
Alias /wiki /opt/dokuwiki
Then, the requests have to be autorized with the following options:
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 :
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>.
It is possible to customize the server with the following options :
Example :
ServerAdmin matthieu@bouthors.fr
Apache has several useful features :
Hosting of several websites :
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 are often used, they allow to ask the user to request another url.
Options used are :
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>
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/
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/
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
Logs can be controled by different ways :
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
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