Revision as of 18:03, 2 April 2014

LAMP refers to a common combination of software used in many web servers: Linux, Apache, MySQL/MariaDB, 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 MariaDB 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.

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.

Apache should now be running. Test by visiting http://localhost/ in a web browser. It should display a simple Apache test page.

User directories

User directories are available by default through http://localhost/~yourusername/ 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 out 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:

Virtual Hosts

If you want to have more than one host, uncomment the following line in /etc/httpd/conf/httpd.conf:

Include conf/extra/httpd-vhosts.conf

In /etc/httpd/conf/extra/httpd-vhosts.conf set your virtual hosts. The default file contains an elaborate example that should help you get started.

To test the virtual hosts on you local machine, add the virtual names to your /etc/hosts file:

127.0.0.1 domainname1.dom
127.0.0.1 domainname2.dom

If you want to use SSL on vhosts, copy the file httpd-ssl.conf to a custom named *.conf file, copy the <VirtualHost .. </VirtualHost> block to the number of vhosts, edit the file so it matches with the httpd-vhosts.conf file and change line SSL Include conf/extra/httpd-ssl.conf inside /etc/httpd/conf/httpd.conf:

Managing lots of virtual hosts

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.

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/domainname2.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 want to 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.

ServerAdmin you@example.com

This is the admin's email address which can be found on e.g. error pages.

DocumentRoot "/srv/http"

This is the directory where you should put your web pages.

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 Require all denied 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.

Tip: If you have issues with your configuration you can have Apache check the configuration with: apachectl configtest

More settings can be found in /etc/httpd/conf/extra/httpd-default.conf:

To turn off your server's signature:

ServerSignature Off

To hide server information like Apache and PHP versions:

ServerTokens Prod

Troubleshooting

If you have problems with Apache then the first place where you can find more information is logs:

systemctl status httpd

will show the daemon status and system logs. Apache logs you can be found in /var/log/httpd/.

If you encounter Error: PID file /run/httpd/httpd.pid not readable (yet?) after start.

Comment out the unique_id_module:

#LoadModule unique_id_module modules/mod_unique_id.so

PHP

Note:libphp5.so included with php-apache does not work with mod_mpm_event (FS#39218). You'll have to use mod_mpm_prefork instead. Otherwise you will get the following error:

Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP.
AH00013: Pre-configuration failed
httpd.service: control process exited, code=exited status=1

Using php5 with php-fpm, mod_proxy_fcgi and mod_proxy_handler

Note: Unlike the widespread setup with ProxyPass, the proxy configuration with mod_proxy_handler and SetHandler respects other Apache directives like DirectoryIndex. This ensures a better compatibility with software designed for libphp5, mod_fastcgi and mod_fcgid.
If you still want to try ProxyPass, experiment with a line like this:

MariaDB

Warning: As of PHP 5.5, mysql.so is deprecated and will fill up your log files.

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.