Difference between revisions of "Apache HTTP Server"

From ArchWiki
Jump to: navigation, search
(PHP)
(PHP)
Line 94: Line 94:
  
 
* Add these lines in {{Filename|/etc/httpd/conf/httpd.conf}}:
 
* Add these lines in {{Filename|/etc/httpd/conf/httpd.conf}}:
:Place this at the beginning of the "LoadModule" list (for some reason php5 won't work if this line is at the bottom of the "LoadModule" list):
+
:Place this in the "LoadModule" list anywhere after {{Codeline|LoadModule dir_module modules/mod_dir.so}}:
 
   LoadModule php5_module modules/libphp5.so
 
   LoadModule php5_module modules/libphp5.so
  
 
:Place this at the end of the "Include" list:
 
:Place this at the end of the "Include" list:
 
   Include conf/extra/php5_module.conf
 
   Include conf/extra/php5_module.conf
 +
 +
:In order to load this file you need to enable {{Codeline|Includes}} for your {{Codeline|DocumentRoot}}.  In the {{Codeline|<Directory "/srv/http">}} section (or {{Codeline|<Directory "/path/to/documentroot">}}) find the "Options" line and append "Includes" e.g.:
 +
  Options Indexes FollowSymLinks Includes
 +
 +
{{Note|This will enable all Includes listed in {{Filename|httpd.conf}}.  You may wish to check for unwanted Includes and comment them out.}}
  
 
{{Note|If you do not see {{Filename|libphp.so}} in the Apache modules directory, you may have forgotten to install the ''php-apache'' package.}}
 
{{Note|If you do not see {{Filename|libphp.so}} in the Apache modules directory, you may have forgotten to install the ''php-apache'' package.}}
  
* If your {{Codeline|DocumentRoot}} is outside of {{Filename|/home/}}, add it to {{Codeline|open_basedir}} in {{Filename|/etc/php/php.ini}} as such:
+
* If your {{Codeline|DocumentRoot}} is not {{Codeline|/srv/http}}, add it to {{Codeline|open_basedir}} in {{Filename|/etc/php/php.ini}} as such:
   open_basedir=/home/:/tmp/:/usr/share/pear/:/path/to/documentroot
+
   open_basedir=/srv/http/:/home/:/tmp/:/usr/share/pear/:/path/to/documentroot
:'''Note:''' Should not be necessary anymore (18.08.2009)
+
:Instead, add your document root as follows (this is the default):
+
  open_basedir=/srv/http:/home/:/tmp/:/usr/share/pear/
+
  
 
* Restart the Apache service to make changes take effect:
 
* Restart the Apache service to make changes take effect:
 
   # /etc/rc.d/httpd restart
 
   # /etc/rc.d/httpd restart
  
* Test PHP with a simple, but very informative script:
+
* A test file for PHP is included by default and can be found in {{Codeline|/srv/http}}
  <?php
+
    phpinfo();
+
  ?>
+
  
* Save this file as {{Filename|test.php}} and copy to <tt>/srv/http/</tt> or to <tt>~/public_html</tt> if you permitted such a configuration.<!-- Also, remember to make it executable ({{Codeline|chmod o+x test.php}}).-->
+
* Remember to copy this file to <tt>~/public_html</tt> if you permitted such a configuration.<!-- Also, remember to make it executable ({{Codeline|chmod o+x test.php}}).-->
  
 
* Test PHP: http://localhost/test.php or http://localhost/~myname/test.php
 
* Test PHP: http://localhost/test.php or http://localhost/~myname/test.php
  
:If the php instruction is not executed (you see : <html>...</html>), add this to your {{Filename|/etc/httpd/conf/httpd.conf}}:
+
:If the PHP instruction is not executed (you see : <html>...</html>), check that you have added "Includes" to the "Options" line for your root directory.
 +
 
 +
====Advanced options====
 +
* If you don't want to enable Includes for your {{Codeline|DocumentRoot}} you can add this to your {{Filename|/etc/httpd/conf/httpd.conf}}:
 
   AddType application/x-httpd-php .php
 
   AddType application/x-httpd-php .php
 
   AddType application/x-httpd-php-source .phps
 
   AddType application/x-httpd-php-source .phps
  
:'''Note:''' This is not necessary as these includes are in the conf/extra/php5_module.conf. The includes assume the dir_module has been loaded, so putting the LoadModule php5_module modules/libphp5.so right after the dir_module line makes it work correctly (2010/02/23).
 
 
{{Tip|You must use the full php opening tags while testing your script or enable the short_open_tag directive in {{Filename|/etc/php/php.ini}} for this to work.}}
 
 
====Advanced options====
 
 
* Remember to add a file handler for .phtml if you need it in {{Filename|/etc/httpd/conf/extra/php5_module.conf}}:
 
* Remember to add a file handler for .phtml if you need it in {{Filename|/etc/httpd/conf/extra/php5_module.conf}}:
 
   DirectoryIndex index.php index.phtml index.html
 
   DirectoryIndex index.php index.phtml index.html

Revision as of 11:21, 18 March 2010

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

This article describes how to set up the Apache web server on an Arch Linux system. It also tells how to optionally install PHP and MySQL and integrate these in the Apache server. This combination is commonly referred to as LAMP (Linux Apache MySQL PHP).

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

Installation

# pacman -S apache php php-apache mysql

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.

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, though you may still 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 Template:Filename

  • Check for the existence of the http user by looking for http in the output of the following command:
 # cat /etc/shadow
  • Create the system user http if it does not exist already:
 # useradd -d /srv/http -r -s /bin/false -U http
This creates the http user with home directory Template:Filename, as a system account (-r), with a bogus shell (-s Template:Filename) and creates a group with the same name (-U).
 127.0.0.1 localhost.localdomain localhost
If you want a different hostname, append it to the end:
 127.0.0.1 localhost.localdomain localhost myhostname
 #
 # Networking
 #
 HOSTNAME="localhost"
  • Make sure the hostname appears in /etc/hosts or apache will fail to start. Alternatively, you can

edit Template:Filename and comment the following module:

 LoadModule unique_id_module        modules/mod_unique_id.so
  • Run the following in a terminal to start the HTTP server:
 # /etc/rc.d/httpd start
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 Template:Filename:
Include conf/extra/httpd-userdir.conf
  • To start Apache automatically at boot, edit Template:Filename and add the httpd daemon:
 DAEMONS=(... httpd ...)
Or add this line to Template:Filename:
 /etc/rc.d/httpd start
 UserDir public_html
and
 <Directory /home/*/public_html>
   AllowOverride FileInfo AuthConfig Limit Indexes
   Options MultiViews Indexes SymLinksIfOwnerMatch ExecCGI
   <Limit GET POST OPTIONS PROPFIND>
     Order allow,deny
     Allow from all
   </Limit>
   <LimitExcept GET POST OPTIONS PROPFIND>
     Order deny,allow
     Deny from all
   </LimitExcept>
 </Directory>
  • You must make sure that your home directory permissions are set properly so that Apache can get there. Your home directory and Template:Filename must be executable for others ("rest of the world"). This seems to be enough:
 $ chmod o+x ~
 $ chmod o+x ~/public_html

There may be some other, more-secure ways of setting the permissions by creating a special group and allowing only Apache and you to enter there.

Advanced Options

These options in Template:Filename 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.

# ServerAdmin sample@sample.com

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

# 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 change the

<Directory "/srv/http">

to whatever you changed your DocumentRoot to, 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 403 error too.

PHP

PHP is practically available out of the box now.

Place this in the "LoadModule" list anywhere after Template:Codeline:
 LoadModule php5_module modules/libphp5.so
Place this at the end of the "Include" list:
 Include conf/extra/php5_module.conf
In order to load this file you need to enable Template:Codeline for your Template:Codeline. In the Template:Codeline section (or Template:Codeline) find the "Options" line and append "Includes" e.g.:
 Options Indexes FollowSymLinks Includes
Note: This will enable all Includes listed in Template:Filename. You may wish to check for unwanted Includes and comment them out.
Note: If you do not see Template:Filename in the Apache modules directory, you may have forgotten to install the php-apache package.
 open_basedir=/srv/http/:/home/:/tmp/:/usr/share/pear/:/path/to/documentroot
  • Restart the Apache service to make changes take effect:
 # /etc/rc.d/httpd restart
  • Remember to copy this file to ~/public_html if you permitted such a configuration.
If the PHP instruction is not executed (you see : <html>...</html>), check that you have added "Includes" to the "Options" line for your root directory.

Advanced options

 AddType application/x-httpd-php .php
 AddType application/x-httpd-php-source .phps
 DirectoryIndex index.php index.phtml index.html
  • If you want the libGD module, install php-gd package and uncomment in Template:Filename:
Note: php-gd requires libpng, libjpeg, and freetype2
 ;extension=gd.so

to

 extension=gd.so
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 to display errors to debug your php code, change this line of Template:Filename:
 display_errors=Off

to

 display_errors=On
  • If you want the mcrypt module, install php-mcrypt package and uncomment in Template:Filename:
 ;extension=mcrypt.so
to
 extension=mcrypt.so

MySQL

  • Configure MySQL as described in MySQL.
 ;extension=mysql.so
Caution:Some users have reported typos on this line. Please make sure that it reads Template:Codeline and not Template:Codeline.
  • You can add minor privileged users for your web scripts by editing the tables found in the mysql database. You have to restart MySQL for changes to take effect. Do not forget to check the Template:Codeline table. If there is a second entry for root and your hostname is left with no password set, everybody from your host probably could gain full access. Perhaps see next section for these jobs.
  • Run in terminal:
 # /etc/rc.d/mysqld start
  • You may also need to restart Apache. Run in terminal:
 # /etc/rc.d/httpd restart
  • MySQL should now be running. Set the root password and test it by running:
 # mysqladmin -u root password password
 # mysql -u root -p
Type exit to exit from the CLI MySQL client
 DAEMONS=(... mysqld ...)

Or add this line to Template:Filename:

 /etc/rc.d/mysqld start
  • You can get the Template:Codeline message instantly whenever you try to connect to the MySQL daemon by TCP/IP. This is the TCP wrappers system (tcpd), which uses the hosts_access(5) system to allow or disallow connections.
  • If you are running into this problem, be sure to add this to your Template:Filename file:
 # mysqld : ALL : ALLOW
 # mysqld-max : ALL : ALLOW
 # and similar for the other MySQL daemons.
Note: The examples above are the simplest case, telling tcpd to allow connections from anywhere. You may wish to use a more-appropriate choice of permissible sources instead of 'ALL'. Just make sure that localhost and the IP address (numeric or DNS) of the interface by which you connect are specified.
 skip-networking

to

 #skip-networking
Tip: You may want to install phpmyadmin to work with your databases.

See also

  • MySQL - Article for MySQL
  • Xampp - Self contained web-server that supports PHP, Perl, and MySQL

External links