Difference between revisions of "Apache HTTP Server"

From ArchWiki
Jump to: navigation, search
m (PHP: added some templates and grammar fixes)
m (User directories: Requires root access)
(5 intermediate revisions by 3 users not shown)
Line 11: Line 11:
 
[[tr:LAMP]]
 
[[tr:LAMP]]
 
[[zh-CN:LAMP]]
 
[[zh-CN:LAMP]]
 +
{{Article summary start}}
 +
{{Article summary text|This page explains the installation and configuration of a complete LAMP server.}}
 +
{{Article summary heading|Related}}
 +
{{Article summary wiki|MySQL}}
 +
{{Article summary wiki|PhpMyAdmin}}
 +
{{Article summary wiki|Adminer}}
 +
{{Article summary wiki|Xampp}}
 +
{{Article summary wiki|mod_perl}}
 +
{{Article summary end}}
 
[[Wikipedia:LAMP (software bundle)|LAMP]] refers to a common combination of software used in many web servers: '''L'''inux, '''A'''pache, '''M'''ySQL, and '''P'''HP. This article describes how to set up the [http://httpd.apache.org Apache HTTP Server] on an Arch Linux system. It also tells you how to optionally install [[PHP]] and [[MySQL]] and integrate these in the Apache server.
 
[[Wikipedia:LAMP (software bundle)|LAMP]] refers to a common combination of software used in many web servers: '''L'''inux, '''A'''pache, '''M'''ySQL, and '''P'''HP. This article describes how to set up the [http://httpd.apache.org Apache HTTP Server] on an Arch Linux system. It also tells you how to optionally install [[PHP]] and [[MySQL]] and integrate these in the Apache server.
  
Line 18: Line 27:
 
This document assumes you will install Apache, PHP and MySQL together. If desired however, you may install Apache, PHP, and MySQL separately and simply refer to the relevant sections below.
 
This document assumes you will install Apache, PHP and MySQL together. If desired however, you may install Apache, PHP, and MySQL separately and simply refer to the relevant sections below.
  
You can [[pacman|install]] {{Pkg|apache}}, {{Pkg|php}}, {{Pkg|php-apache}} and {{Pkg|mysql}} from the [[official repositories]].
+
You can [[pacman|install]] {{Pkg|apache}}, {{Pkg|php}}, {{Pkg|php-apache}} and {{Pkg|mysql}} from the [[official repositories]]. Alternatively, run:
 +
  $ pacman -S apache php php-apache mysql
  
 
{{Note|New default user and group: Instead of group '''nobody''', {{ic|apache}} now runs as user/group '''http''' by default. You might want to adjust your {{ic|httpd.conf}} according to this change, although it is still possible to run {{ic|httpd}} as '''nobody'''.}}
 
{{Note|New default user and group: Instead of group '''nobody''', {{ic|apache}} now runs as user/group '''http''' by default. You might want to adjust your {{ic|httpd.conf}} according to this change, although it is still possible to run {{ic|httpd}} as '''nobody'''.}}
Line 29: Line 39:
 
After installation, you can:
 
After installation, you can:
 
* Change {{ic|httpd.conf}} and optionally {{ic|extra/httpd-default.conf}} to your liking and
 
* Change {{ic|httpd.conf}} and optionally {{ic|extra/httpd-default.conf}} to your liking and
* Start the '''httpd''' [[Daemons|daemon]].
+
* [[Daemons|Start]] the '''httpd''' daemon.
 +
  $ httpd
  
 
:Apache should now be running. Test by visiting http://localhost/ in a web browser. It should display a simple Apache test page. If you receive a '''403 Error''', comment out the following line in {{ic|/etc/httpd/conf/httpd.conf}}:
 
:Apache should now be running. Test by visiting http://localhost/ in a web browser. It should display a simple Apache test page. If you receive a '''403 Error''', comment out the following line in {{ic|/etc/httpd/conf/httpd.conf}}:
Line 45: Line 56:
  
 
* A more secure way to share your home folder with Apache is to add the '''http''' user to the group that owns your home folder. For example, if your home folder and other sub-folders in your home folder belong to group '''piter''', all you have to do is following:
 
* A more secure way to share your home folder with Apache is to add the '''http''' user to the group that owns your home folder. For example, if your home folder and other sub-folders in your home folder belong to group '''piter''', all you have to do is following:
   $ usermod -aG piter http
+
   # usermod -aG http piter
  
 
* Of course, you have to give ''read'' and ''execute'' permissions on {{ic|~/}},  {{ic|~/public_html}}, and all other sub-folders in {{ic|~/public_html}} to the group members (group '''piter''' in our case). Do something like the following ('''modify the commands for your specific case'''):
 
* Of course, you have to give ''read'' and ''execute'' permissions on {{ic|~/}},  {{ic|~/public_html}}, and all other sub-folders in {{ic|~/public_html}} to the group members (group '''piter''' in our case). Do something like the following ('''modify the commands for your specific case'''):
Line 53: Line 64:
 
{{Note|This way you do not have to give access to your folder to every single user in order to give access to '''http''' user. Only the '''http''' user and other potential users that are in the '''piter''' group will have access to your home folder.}}
 
{{Note|This way you do not have to give access to your folder to every single user in order to give access to '''http''' user. Only the '''http''' user and other potential users that are in the '''piter''' group will have access to your home folder.}}
  
and restart '''httpd'''.
+
and [[Daemons|restart]] '''httpd'''.
  
 
==== SSL ====
 
==== SSL ====
Create a self-signed certificate (you can change the key size and the number of days of validity):
+
* Create a self-signed certificate (you can change the key size and the number of days of validity):
 
   # cd /etc/httpd/conf
 
   # cd /etc/httpd/conf
 
   # openssl genrsa -des3 -out server.key 1024
 
   # openssl genrsa -des3 -out server.key 1024
Line 64: Line 75:
 
   # openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
 
   # openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
  
Then, in {{ic|/etc/httpd/conf/httpd.conf}}, uncomment the line containing:
+
* Then, in {{ic|/etc/httpd/conf/httpd.conf}}, uncomment the line containing:
 
   Include conf/extra/httpd-ssl.conf
 
   Include conf/extra/httpd-ssl.conf
and [[Daemons#Restarting|restart]] '''httpd'''.
+
and [[Daemons|restart]] '''httpd'''.
  
 
==== Virtual Hosts ====
 
==== Virtual Hosts ====
If you want to have more than one host, make sure you have
+
* If you want to have more than one host, make sure you have
 
{{bc|
 
{{bc|
 
# Virtual hosts
 
# Virtual hosts
Line 76: Line 87:
 
in {{ic|/etc/httpd/conf/httpd.conf}}.
 
in {{ic|/etc/httpd/conf/httpd.conf}}.
  
In {{ic|/etc/httpd/conf/extra/httpd-vhosts.conf}} set your virtual hosts according the example, e.g.:
+
* In {{ic|/etc/httpd/conf/extra/httpd-vhosts.conf}} set your virtual hosts according the example, e.g.:
{{bc|
+
{{hc|/etc/httpd/conf/extra/httpd-vhosts.conf|
 
NameVirtualHost *:80
 
NameVirtualHost *:80
  
Line 135: Line 146:
 
}}
 
}}
  
Add your virtual host names to your {{ic|/etc/hosts}} file (not necessary if [[BIND]] is serving these domains already, but will not hurt to do it anyway):
+
* Add your virtual host names to your {{ic|/etc/hosts}} file (not necessary if [[BIND]] is serving these domains already, but will not hurt to do it anyway):
 
{{bc|127.0.0.1 domainname1.dom  
 
{{bc|127.0.0.1 domainname1.dom  
 
127.0.0.1 domainname2.dom}}
 
127.0.0.1 domainname2.dom}}
  
and restart '''httpd'''.
+
and [[Daemons|restart]] '''httpd'''.
  
If you setup your virtual hosts to be in your user directory, sometimes it interferes with Apache's {{ic|Userdir}} settings. To avoid problems disable {{ic|Userdir}} by commenting it out:
+
* If you setup your virtual hosts to be in your user directory, sometimes it interferes with Apache's {{ic|Userdir}} settings. To avoid problems disable {{ic|Userdir}} by commenting it out:
 
{{bc|
 
{{bc|
 
# User home directories
 
# User home directories
 
#Include conf/extra/httpd-userdir.conf}}
 
#Include conf/extra/httpd-userdir.conf}}
  
As said above, ensure that you have the proper permissions:
+
* As said above, ensure that you have the proper permissions:
 
  # chmod 0775 /home/''yourusername''/
 
  # chmod 0775 /home/''yourusername''/
  
If you have a huge amount of virtual hosts, you may want to easily disable and enable them. It is recommended to create one configuration file per virtual host and store them all in one folder, eg: {{ic|/etc/httpd/conf/vhosts}}.
+
* If you have a huge amount of virtual hosts, you may want to easily disable and enable them. It is recommended to create one configuration file per virtual host and store them all in one folder, eg: {{ic|/etc/httpd/conf/vhosts}}.
  
First create the folder:
+
* First create the folder:
 
  # mkdir /etc/httpd/conf/vhosts
 
  # mkdir /etc/httpd/conf/vhosts
  
Then place the single configuration files in it:
+
* Then place the single configuration files in it:
 
  # nano /etc/httpd/conf/vhosts/domainname1.dom
 
  # nano /etc/httpd/conf/vhosts/domainname1.dom
 
  # nano /etc/httpd/conf/vhosts/domainname2.dom
 
  # nano /etc/httpd/conf/vhosts/domainname2.dom
 
  ...
 
  ...
  
In the last step, {{ic|Include}} the single configurations in your {{ic|/etc/httpd/conf/httpd.conf}}:
+
* In the last step, {{ic|Include}} the single configurations in your {{ic|/etc/httpd/conf/httpd.conf}}:
{{bc|#Enabled Vhosts:
+
{{bc|
 +
#Enabled Vhosts:
 
Include conf/vhosts/domainname1.dom
 
Include conf/vhosts/domainname1.dom
#Include conf/vhosts/domainname1.dom}}
+
#Include conf/vhosts/domainname1.dom
 +
}}
  
You can enable and disable single virtual hosts by commenting or uncommenting them.
+
* You can enable and disable single virtual hosts by commenting or uncommenting them.
  
 
==== Advanced Options ====
 
==== Advanced Options ====
Line 170: Line 183:
  
 
  # Listen 80
 
  # Listen 80
This is the port Apache will listen to. For Internet-access with router, you have to forward the port.
+
* This is the port Apache will listen to. For Internet-access with router, you have to forward the port.
  
 
If you setup Apache for local development you may want it to be only accessible from your computer. Then change this line to:
 
If you setup Apache for local development you may want it to be only accessible from your computer. Then change this line to:
 
  # Listen 127.0.0.1:80
 
  # Listen 127.0.0.1:80
  
This is the admin's email address which can be found on e.g. error pages:
+
* This is the admin's email address which can be found on e.g. error pages:
 
  # ServerAdmin you@example.com
 
  # ServerAdmin you@example.com
  
This is the directory where you should put your web pages:
+
* This is the directory where you should put your web pages:
 
  # DocumentRoot "/srv/http"
 
  # DocumentRoot "/srv/http"
  
Line 186: Line 199:
  
 
  # AllowOverride None
 
  # AllowOverride None
This directive in {{ic|<Directory>}} sections causes Apache to completely ignore {{ic|.htaccess}} files. If you intend to use {{ic|mod_rewrite}} or other settings in {{ic|.htaccess}} files, you can allow which directives declared in that file can override server configuration. For more info refer to the [http://httpd.apache.org/docs/current/mod/core.html#allowoverride Apache documentation].
+
* This directive in {{ic|<Directory>}} sections causes Apache to completely ignore {{ic|.htaccess}} files. If you intend to use {{ic|mod_rewrite}} or other settings in {{ic|.htaccess}} files, you can allow which directives declared in that file can override server configuration. For more info refer to the [http://httpd.apache.org/docs/current/mod/core.html#allowoverride Apache documentation].
  
 
{{Note|If you have issues with your configuration you can have Apache check the configuration with: {{ic|apachectl configtest}}}}
 
{{Note|If you have issues with your configuration you can have Apache check the configuration with: {{ic|apachectl configtest}}}}
  
More settings in {{ic|/etc/httpd/conf/httpd.conf}}:
+
* More settings in {{ic|/etc/httpd/conf/httpd.conf}}:
  
To turn off your server's signature:
+
* To turn off your server's signature:
 
  ServerSignature Off
 
  ServerSignature Off
  
To hide server information like Apache and PHP versions:
+
* To hide server information like Apache and PHP versions:
 
  ServerTokens Prod
 
  ServerTokens Prod
  
Line 220: Line 233:
 
   open_basedir=/srv/http/:/home/:/tmp/:/usr/share/pear/:/path/to/documentroot
 
   open_basedir=/srv/http/:/home/:/tmp/:/usr/share/pear/:/path/to/documentroot
  
* Restart the '''httpd''' [[daemon]].
+
* [[Daemons|Restart]] the '''httpd''' daemon.
  
 
* To test whether PHP was correctly configured: create a file called {{ic|test.php}} in your Apache {{ic|DocumentRoot}} directory (e.g. {{ic|/srv/http/}} or {{ic|~/public_html}}) and inside it put:
 
* To test whether PHP was correctly configured: create a file called {{ic|test.php}} in your Apache {{ic|DocumentRoot}} directory (e.g. {{ic|/srv/http/}} or {{ic|~/public_html}}) and inside it put:
Line 237: Line 250:
  
 
* If you want the {{ic|libGD}} module, install {{Pkg|php-gd}} and uncomment {{ic|1=extension=gd.so}} in {{ic|/etc/php/php.ini}}:
 
* If you want the {{ic|libGD}} module, install {{Pkg|php-gd}} and uncomment {{ic|1=extension=gd.so}} in {{ic|/etc/php/php.ini}}:
{{Note|{{Pkg|php-gd}} requires {{Pkg|libpng}}, {{Pkg|libjpeg}}, and {{Pkg|freetype2}}.}}
+
{{Note|{{Pkg|php-gd}} requires {{Pkg|libpng}}, {{Pkg|libjpeg-turbo}}, and {{Pkg|freetype2}}.}}
 
  extension=gd.so
 
  extension=gd.so
  
Line 295: Line 308:
 
* Make sure {{ic|/etc/php/php.ini}} has the directive enabled:
 
* Make sure {{ic|/etc/php/php.ini}} has the directive enabled:
 
  cgi.fix_pathinfo=1
 
  cgi.fix_pathinfo=1
and restart '''httpd'''.
+
and [[Daemons|restart]] '''httpd'''.
  
 
{{Note|1=As of Apache 2.4 (the {{AUR|apache24}} package is available in the [[AUR]]) you can now use [http://httpd.apache.org/docs/2.4/mod/mod_proxy_fcgi.html mod_proxy_fcgi] (part of the official distribution) with PHP-FPM (and the new event MPM). See this [http://wiki.apache.org/httpd/PHP-FPM configuration example].}}
 
{{Note|1=As of Apache 2.4 (the {{AUR|apache24}} package is available in the [[AUR]]) you can now use [http://httpd.apache.org/docs/2.4/mod/mod_proxy_fcgi.html mod_proxy_fcgi] (part of the official distribution) with PHP-FPM (and the new event MPM). See this [http://wiki.apache.org/httpd/PHP-FPM configuration example].}}
  
===MySQL===
+
=== MySQL ===
 
* Configure MySQL as described in [[MySQL]].
 
* Configure MySQL as described in [[MySQL]].
  
Line 309: Line 322:
 
* You can add minor privileged MySQL users for your web scripts. You might also want to edit {{ic|/etc/mysql/my.cnf}} and uncomment the {{ic|skip-networking}} line so the MySQL server is only accessible by the localhost. You have to restart MySQL for changes to take effect.  
 
* You can add minor privileged MySQL users for your web scripts. You might also want to edit {{ic|/etc/mysql/my.cnf}} and uncomment the {{ic|skip-networking}} line so the MySQL server is only accessible by the localhost. You have to restart MySQL for changes to take effect.  
  
* Restart the '''httpd''' [[daemon]].
+
* [[Daemons|Restart]] the '''httpd''' daemon.
  
 
{{Tip|You may want to install a tool like [[phpMyAdmin]], [[Adminer]] or {{AUR|mysql-workbench}} to work with your databases.}}
 
{{Tip|You may want to install a tool like [[phpMyAdmin]], [[Adminer]] or {{AUR|mysql-workbench}} to work with your databases.}}
  
==See also==
+
== External links ==
* [[MySQL]] - Article for MySQL
+
* [http://www.apache.org/ Apache Official Website]
* [[PhpMyAdmin]] - Web frontend for MySQL typically found in LAMP environments
+
* [http://www.php.net/ PHP Official Website]
* [[Adminer]] - A full-featured database management tool which is available for MySQL, PostgreSQL, SQLite, MS SQL and Oracle
+
* [http://www.mysql.com/ MySQL Official Website]
* [[Xampp]] - Self contained web-server that supports PHP, Perl, and MySQL
+
* [http://www.akadia.com/services/ssh_test_certificate.html Tutorial for creating self-signed certificates]
* [[mod_perl]] - Apache + Perl
+
* [http://wiki.apache.org/httpd/CommonMisconfigurations Apache Wiki Troubleshooting]
 
+
==External links==
+
* http://www.apache.org/
+
* http://www.php.net/
+
* http://www.mysql.com/
+
* http://www.akadia.com/services/ssh_test_certificate.html
+
* http://wiki.apache.org/httpd/CommonMisconfigurations
+

Revision as of 22:00, 11 March 2013

Summary help replacing me
This page explains the installation and configuration of a complete LAMP server.
Related
MySQL
PhpMyAdmin
Adminer
Xampp
mod_perl

LAMP refers to a common combination of software used in many web servers: Linux, Apache, MySQL, and PHP. This article describes how to set up the Apache HTTP Server on an Arch Linux system. It also tells you how to optionally install PHP and MySQL and integrate these in the Apache server.

If you only need a web server for development and testing, Xampp might be a better and easier option.

Installation

This document assumes you will install Apache, PHP and MySQL together. If desired however, you may install Apache, PHP, and MySQL separately and simply refer to the relevant sections below.

You can install apache, php, php-apache and mysql from the official repositories. Alternatively, run:

 $ pacman -S apache php php-apache mysql
Note: New default user and group: Instead of group nobody, apache now runs as user/group http by default. You might want to adjust your httpd.conf according to this change, although it is still possible to run httpd as nobody.

Configuration

Apache

For security reasons, as soon as Apache is started by the root user (directly or via startup scripts) it switches to the UID/GID specified in /etc/httpd/conf/httpd.conf. The default is user http and it is created automatically during installation.

After installation, you can:

  • Change httpd.conf and optionally extra/httpd-default.conf to your liking and
  • Start the httpd daemon.
 $ httpd
Apache should now be running. Test by visiting http://localhost/ in a web browser. It should display a simple Apache test page. If you receive a 403 Error, comment out the following line in /etc/httpd/conf/httpd.conf:
Include conf/extra/httpd-userdir.conf

User directories

  • User directories are available by default through http://localhost/~user/ and show the contents of ~/public_html (this can be changed in /etc/httpd/conf/extra/httpd-userdir.conf).
  • If you do not want user directories to be available on the web, comment the following line in /etc/httpd/conf/httpd.conf:
 Include conf/extra/httpd-userdir.conf
  • You must make sure that your home directory permissions are set properly so that Apache can get there. Your home directory and ~/public_html/ must be executable for others ("rest of the world"). This seems to be enough:
 $ chmod o+x ~
 $ chmod o+x ~/public_html
  • A more secure way to share your home folder with Apache is to add the http user to the group that owns your home folder. For example, if your home folder and other sub-folders in your home folder belong to group piter, all you have to do is following:
 # usermod -aG http piter
  • Of course, you have to give read and execute permissions on ~/, ~/public_html, and all other sub-folders in ~/public_html to the group members (group piter in our case). Do something like the following (modify the commands for your specific case):
 $ chmod g+xr-w /home/yourusername
 $ chmod -R g+xr-w /home/yourusername/public_html
Note: This way you do not have to give access to your folder to every single user in order to give access to http user. Only the http user and other potential users that are in the piter group will have access to your home folder.

and restart httpd.

SSL

  • Create a self-signed certificate (you can change the key size and the number of days of validity):
 # cd /etc/httpd/conf
 # openssl genrsa -des3 -out server.key 1024
 # openssl req -new -key server.key -out server.csr
 # cp server.key server.key.org
 # openssl rsa -in server.key.org -out server.key
 # openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
  • Then, in /etc/httpd/conf/httpd.conf, uncomment the line containing:
 Include conf/extra/httpd-ssl.conf

and restart httpd.

Virtual Hosts

  • If you want to have more than one host, make sure you have
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

in /etc/httpd/conf/httpd.conf.

  • In /etc/httpd/conf/extra/httpd-vhosts.conf set your virtual hosts according the example, e.g.:
/etc/httpd/conf/extra/httpd-vhosts.conf
NameVirtualHost *:80

#this first virtualhost enables: http://127.0.0.1, or: http://localhost, 
#to still go to /srv/http/*index.html(otherwise it will 404_error).
#the reason for this: once you tell httpd.conf to include extra/httpd-vhosts.conf, 
#ALL vhosts are handled in httpd-vhosts.conf(including the default one),
# E.G. the default virtualhost in httpd.conf is not used and must be included here, 
#otherwise, only domainname1.dom & domainname2.dom will be accessible
#from your web browser and NOT http://127.0.0.1, or: http://localhost, etc.
#

<VirtualHost *:80>
    DocumentRoot "/srv/http"
    ServerAdmin root@localhost
    ErrorLog "/var/log/httpd/127.0.0.1-error_log"
    CustomLog "/var/log/httpd/127.0.0.1-access_log" common
    <Directory /srv/http/>
      DirectoryIndex index.htm index.html
      AddHandler cgi-script .cgi .pl
      Options ExecCGI Indexes FollowSymLinks MultiViews +Includes
      AllowOverride None
      Order allow,deny
      Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin your@domainname1.dom
    DocumentRoot "/home/username/yoursites/domainname1.dom/www"
    ServerName domainname1.dom
    ServerAlias domainname1.dom
    <Directory /home/username/yoursites/domainname1.dom/www/>
      DirectoryIndex index.htm index.html
      AddHandler cgi-script .cgi .pl
      Options ExecCGI Indexes FollowSymLinks MultiViews +Includes
      AllowOverride None
      Order allow,deny
      Allow from all
</Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin your@domainname2.dom
    DocumentRoot "/home/username/yoursites/domainname2.dom/www"
    ServerName domainname2.dom
    ServerAlias domainname2.dom
    <Directory /home/username/yoursites/domainname2.dom/www/>
      DirectoryIndex index.htm index.html
      AddHandler cgi-script .cgi .pl
      Options ExecCGI Indexes FollowSymLinks MultiViews +Includes
      AllowOverride None
      Order allow,deny
      Allow from all
</Directory>
</VirtualHost>
  • Add your virtual host names to your /etc/hosts file (not necessary if BIND is serving these domains already, but will not hurt to do it anyway):
127.0.0.1 domainname1.dom 
127.0.0.1 domainname2.dom

and restart httpd.

  • If you setup your virtual hosts to be in your user directory, sometimes it interferes with Apache's Userdir settings. To avoid problems disable Userdir by commenting it out:
# User home directories
#Include conf/extra/httpd-userdir.conf
  • As said above, ensure that you have the proper permissions:
# chmod 0775 /home/yourusername/
  • If you have a huge amount of virtual hosts, you may want to easily disable and enable them. It is recommended to create one configuration file per virtual host and store them all in one folder, eg: /etc/httpd/conf/vhosts.
  • First create the folder:
# mkdir /etc/httpd/conf/vhosts
  • Then place the single configuration files in it:
# nano /etc/httpd/conf/vhosts/domainname1.dom
# nano /etc/httpd/conf/vhosts/domainname2.dom
...
  • In the last step, Include the single configurations in your /etc/httpd/conf/httpd.conf:
#Enabled Vhosts:
Include conf/vhosts/domainname1.dom
#Include conf/vhosts/domainname1.dom
  • You can enable and disable single virtual hosts by commenting or uncommenting them.

Advanced Options

These options in /etc/httpd/conf/httpd.conf might be interesting for you.

# Listen 80
  • This is the port Apache will listen to. For Internet-access with router, you have to forward the port.

If you setup Apache for local development you may want it to be only accessible from your computer. Then change this line to:

# Listen 127.0.0.1:80
  • This is the admin's email address which can be found on e.g. error pages:
# ServerAdmin you@example.com
  • This is the directory where you should put your web pages:
# DocumentRoot "/srv/http"

Change it, if you want to, but do not forget to also change

<Directory "/srv/http">

to whatever you changed your DocumentRoot too, or you will likely get a 403 Error (lack of privileges) when you try to access the new document root. Do not forget to change the Deny from all line, otherwise you will get a 403 Error.

# AllowOverride None
  • This directive in <Directory> sections causes Apache to completely ignore .htaccess files. If you intend to use mod_rewrite or other settings in .htaccess files, you can allow which directives declared in that file can override server configuration. For more info refer to the Apache documentation.
Note: If you have issues with your configuration you can have Apache check the configuration with: apachectl configtest
  • More settings in /etc/httpd/conf/httpd.conf:
  • To turn off your server's signature:
ServerSignature Off
  • To hide server information like Apache and PHP versions:
ServerTokens Prod

PHP

  • To enable PHP, add these lines to /etc/httpd/conf/httpd.conf:
Place this in the LoadModule list anywhere after LoadModule dir_module modules/mod_dir.so:
 LoadModule php5_module modules/libphp5.so
Place this at the end of the Include list:
 Include conf/extra/php5_module.conf
Make sure that the following line is uncommented in the <IfModule mime_module> section:
 TypesConfig conf/mime.types
Uncomment the following line (optional):
 MIMEMagicFile conf/magic
  • Add this line in /etc/httpd/conf/mime.types:
 application/x-httpd-php       php    php5
Note: If you do not see libphp5.so in the Apache modules directory (/etc/httpd/modules), you may have forgotten to install php-apache.
  • If your DocumentRoot is not /srv/http, add it to open_basedir in /etc/php/php.ini as such:
 open_basedir=/srv/http/:/home/:/tmp/:/usr/share/pear/:/path/to/documentroot
  • To test whether PHP was correctly configured: create a file called test.php in your Apache DocumentRoot directory (e.g. /srv/http/ or ~/public_html) and inside it put:
<?php phpinfo(); ?>
To see if it works go to: http://localhost/test.php or http://localhost/~myname/test.php
If the PHP code is not executed (you see plain text in test.php), check that you have added Includes to the Options line for your root directory in /etc/httpd/conf/httpd.conf. Moreover, check that TypesConfig conf/mime.types is uncommented in the <IfModule mime_module> section, you may also try adding the following to the <IfModule mime_module> in httpd.conf:
AddHandler application/x-httpd-php .php

Advanced options

  • It is recommended to set your timezone (list of timezones) in /etc/php/php.ini like so:
date.timezone = Europe/Berlin
  • If you want to display errors to debug your PHP code, change display_errors to On in /etc/php/php.ini:
display_errors=On
  • If you want the libGD module, install php-gd and uncomment extension=gd.so in /etc/php/php.ini:
Note: php-gd requires libpng, libjpeg-turbo, and freetype2.
extension=gd.so
Note: Pay attention to which extension you uncomment, as this extension is sometimes mentioned in an explanatory comment before the actual line you want to uncomment.
  • If you want the mcrypt module, install php-mcrypt and uncomment extension=mcrypt.so in /etc/php/php.ini:
extension=mcrypt.so
  • Remember to add a file handler for .phtml, if you need it, in /etc/httpd/conf/extra/php5_module.conf:
DirectoryIndex index.php index.phtml index.html

Using php5 with apache2-mpm-worker and mod_fcgid

  • Uncomment following in /etc/conf.d/apache:
HTTPD=/usr/sbin/httpd.worker
  • Uncomment following in /etc/httpd/conf/httpd.conf:
Include conf/extra/httpd-mpm.conf
  • Create /etc/httpd/conf/extra/php5_fcgid.conf with following content:
/etc/httpd/conf/extra/php5_fcgid.conf
# Required modules: fcgid_module

<IfModule fcgid_module>
	AddHandler php-fcgid .php
	AddType application/x-httpd-php .php
	Action php-fcgid /fcgid-bin/php-fcgid-wrapper
	ScriptAlias /fcgid-bin/ /srv/http/fcgid-bin/
	SocketPath /var/run/httpd/fcgidsock
	SharememPath /var/run/httpd/fcgid_shm
        # If you don't allow bigger requests many applications may fail (such as WordPress login)
        FcgidMaxRequestLen 536870912
        PHP_Fix_Pathinfo_Enable 1
        # Path to php.ini – defaults to /etc/phpX/cgi
        DefaultInitEnv PHPRC=/etc/php/
        # Number of PHP childs that will be launched. Leave undefined to let PHP decide.
        #DefaultInitEnv PHP_FCGI_CHILDREN 3
        # Maximum requests before a process is stopped and a new one is launched
        #DefaultInitEnv PHP_FCGI_MAX_REQUESTS 5000
        <Location /fcgid-bin/>
		SetHandler fcgid-script
		Options +ExecCGI
	</Location>
</IfModule>
  • Create the needed directory and symlink it for the PHP wrapper:
# mkdir /srv/http/fcgid-bin
# ln -s /usr/bin/php-cgi /srv/http/fcgid-bin/php-fcgid-wrapper
  • Edit /etc/httpd/conf/httpd.conf:
#LoadModule php5_module modules/libphp5.so
LoadModule fcgid_module modules/mod_fcgid.so
Include conf/extra/php5_fcgid.conf
  • Make sure /etc/php/php.ini has the directive enabled:
cgi.fix_pathinfo=1

and restart httpd.

Note: As of Apache 2.4 (the apache24AUR package is available in the AUR) you can now use mod_proxy_fcgi (part of the official distribution) with PHP-FPM (and the new event MPM). See this configuration example.

MySQL

  • Configure MySQL as described in MySQL.
  • Uncomment at least one of the following lines in /etc/php/php.ini:
extension=pdo_mysql.so
extension=mysqli.so
extension=mysql.so
  • You can add minor privileged MySQL users for your web scripts. You might also want to edit /etc/mysql/my.cnf and uncomment the skip-networking line so the MySQL server is only accessible by the localhost. You have to restart MySQL for changes to take effect.
Tip: You may want to install a tool like phpMyAdmin, Adminer or mysql-workbenchAUR to work with your databases.

External links