Difference between revisions of "Apache HTTP Server"

From ArchWiki
Jump to: navigation, search
m
Line 4: Line 4:
 
{{i18n_entry|English|LAMP}}
 
{{i18n_entry|English|LAMP}}
 
{{i18n_entry|Русский|LAMP (russian)}}
 
{{i18n_entry|Русский|LAMP (russian)}}
 +
{{i18n_entry|Česky|LAMP_(Česky)}}
 
{{i18n_links_end}}
 
{{i18n_links_end}}
  

Revision as of 21:56, 3 August 2007

Template:I18n links start Template:I18n entry Template:I18n entry Template:I18n entry Template:I18n links end

Apache, PHP, and MySQL

This document 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.

Install Packages

# pacman -Sy
# pacman -S apache
# pacman -S php
# pacman -S mysql

If you like, you can install just apache, apache and php, or all three. This document assumes you will install all three, but if you wish, you can stop after any of the sections.


Configure Apache

  • Add line to /etc/hosts (If the file doesn't exist, create it)
127.0.0.1  localhost.localdomain   localhost

Note: if you want a different hostname, append it to the end:

127.0.0.1  localhost.localdomain   localhost myhostname
  • Edit /etc/rc.conf:

If you set a hostname in step one, the hostame variable should be the same. otherwise localhost:

#
# Networking
#
HOSTNAME="localhost"
  • Comment one module in Apache configuration
# nano /etc/httpd/conf/httpd.conf
LoadModule unique_id_module        modules/mod_unique_id.so

to

#LoadModule unique_id_module        modules/mod_unique_id.so
  • Run in terminal (as root):
# /etc/rc.d/httpd start
  • Apache should now be running. Test by visiting http://localhost/ in a web browser. It displays a simple Arch Linux home page.
  • Edit /etc/rc.conf (to start Apache at boot):
DAEMONS=(... some daemons ... httpd)

Or add this line to rc.local:

/etc/rc.d/httpd start
  • If you want user directories (ie: ~/public_html on the machine is accessed as http://localhost/~user/) to be available on the web, uncomment the following lines in /etc/httpd/conf/extra/httpd-userdir.conf:
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 have to take into account that your home directory permissions must be 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

There may be some other, more secure ways of setting the permissions by creating a special group and allowing only apache and you enter there... You know how paranoic you are :)

Configure PHP4 & PHP5

PHP is practically available out of the box now.

  • Uncomment this line in /etc/httpd/conf/httpd.conf
#LoadModule php5_module lib/apache/libphp5.so
then, for PHP4, simply substitute 4 for 5
LoadModule php4_module lib/apache/libphp4.so
  • For PHP5 file handlers are already set up
<IfModule mod_php5.c>
 DirectoryIndex index.php index.html
 AddType application/x-httpd-php .php
 AddType application/x-httpd-php-source .phps
</IfModule>

DirectoryIndex index.html index.html.var
However, for PHP4 support you need to make the following changes
#<IfModule mod_php5.c>
 DirectoryIndex index.php index.html index.html.var
 AddType application/x-httpd-php .php
 AddType application/x-httpd-php-source .phps
#</IfModule>

#DirectoryIndex index.html index.html.var
  • Whether you use PHP4 or PHP5 remember to add a file handler for .phtml if you need it
DirectoryIndex index.php index.phtml index.html
  • If you want the libGD module, uncomment in /etc/php.ini
;extension=gd.so

to

extension=gd.so
  • Restart Apache Server to make changes take effect (as root):
# /etc/rc.d/httpd restart
  • Test PHP with a simple, but very informative script:
<html>
<head>
<title>PHP Test Page</title>
</head>

<body>
This is Arch Linux, running PHP.

<?php
  phpinfo();
?>
</p>
</body>
</html>

Save this file as test.php and copy to /home/httpd/html/ or to ~/public_html if you permitted such a configuration. Also remember to make it executable (chmod a+x test.php).

  • Test PHP:
 http://localhost/test.php or http://localhost/~myname/test.php

Setup MySQL support

Do these only if you want MySQL support

Intial Configuration
  • Edit /etc/php.ini (this is in /usr/etc on older systems) to uncomment the following line (By removing ;):
;extension=mysql.so
  • To configure your system either run /etc/rc.d/mysqld start as root OR complete the following steps manually:
  • Add a mysql named group
# groupadd -g 89 mysql
  • Add the mysql user
# useradd -u 89 -g mysql -d /var/lib/mysql -s /bin/false mysql
  • chown the MYSQL root just in case - it may not exist yet
# chown -R mysql:mysql /var/lib/mysql
  • Install the dbase. If you only want run mysql as root you don't need the --user option and chown
# mysql_install_db --datadir=/var/lib/mysql --user=mysql
# chown -R mysql:mysql /var/lib/mysql
  • run /etc/rc.d/mysqld start to start mysql as an Arch daemon.
Further Configuration
  • Test mysql (as root):
# mysql
  • Create root password for mysql (at terminal, as root):
# mysqladmin -u root password 'roots_password'
  • Add mysqld to the list of daemons in /etc/rc.conf (as for httpd above)
  • To login to mysql, type (at terminal, hostname as used in /etc/hosts)
# mysql -u root -h hostname -p
  • You can add minor privilegued users for your web scripts by editing the tables found in the mysql database. You have to restart mysql for changes to take effect.
    Don't forget to check the mysql/users table. If there's a second entry for root and your hostname left with no password set, everybody from your host probably could gain full access.
    Perhaps see next section for these jobs.
  • You can get the "error no. 2013: Lost Connection to mysql server during query" message instantly whenever you tried 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're running into this problem, be sure to add this to your /etc/hosts.allow file:
 # mysqld : ALL : ALLOW
 # mysqld-max : ALL : ALLOW
 # and similar for the other MySQL daemons.
  • Notes: 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.
  • You might also need to edit /etc/my.cnf and change the line that says:
  skip-networking

to

  #skip-networking

Setup PHPMyAdmin

If you or your users want to use PHPMyAdmin to access the database, you might like to go on:

  • Install the package
# pacman -S phpmyadmin
  • phpMyAdmin has changed, the following applies now:
  • Create configuration directory:
# cd /home/httpd/html/phpMyAdmin/
# mkdir config
# chmod o+rw config
  • Create a configuration file, visit:
http://localhost/phpMyAdmin/scripts/setup.php
  • As a minimum add a new server in the "Servers" section. Use "cookie" for authentication to enable web-based cookie authentication (user/password authentication).
  • Save the configuration file through the web based interface in the "Configuration" section.
  • Move configuration file to phpMyAdmin dir for usage:
# mv config/config.inc.php config.inc.php
OLD CONFIGURATION INSTRUCTIONS
  • Edit the configuration file to meet your requirements:
/home/httpd/html/phpMyAdmin/config.default.php
  • Insert the according string to the PmaAbsoluteUri variable, so that it looks like follows
 $cfg['PmaAbsoluteUri'] = 'http://hostname/phpMyAdmin/';
  • Fill in your mysql server's information. In PHPMyAdmin, multiple servers can be defined in the Servers array. To access your single mysql database, you have to edit the first entry; you can ignore the following ones.

On a regular system you only have to set auth_type to http. This means that phpMyAdmin directly uses the entered user and password to communicate with the mysql server, so it can't do stuff not allowed to the according mysql user.

 $cfg['Servers'][$i]['auth_type']     = 'http';

Be aware of that other auth methods or writing any password to this file can seriously damage your database security. Per default this file is world-readable.

  • Rename the configuration file to the following:
/home/httpd/html/phpMyAdmin/config.inc.php

Note: You need to edit the .htaccess file in /home/httpd/html/phpMyAdmin/ and add the line allow from all or allow from 127.0.0.1 or you won't be able to browse http://localhost/phpMyAdmin/