Linux Installation
This section covers the procedure for installing PHP and MySQL undermost current distributions of Linux. These instructions were tested under
the latest version Debian Linux (3.0); however, they should work on other
distributions such as RedHat and Mandrake without much trouble. The steps
involved will be very similar, if not identical.As a user of one of the handful of Linux distributions available, you
may be tempted to download and install packaged distributions of
PHP and MySQL. Debian users will be used to installing software using the apt-get utility,
while other distributions often rely on RPM packages. These prepackaged versions
of software are really easy to install; unfortunately, they also limit the
software configuration options available to you. If you already have MySQL
and PHP installed in packaged form, then feel free to proceed with those versions,
and skip forward to "Post-Installation Setup Tasks". If you encounter any
problems, you can always return here to uninstall the packaged versions and
reinstall PHP and MySQL by hand.Since many Linux distributions will automatically
install PHP and MySQL for you, your first step should be to remove any old
packaged versions of PHP and MySQL from your system. If one exists, use your
distribution's graphical software manager to remove all packages with php or mysql in
their names.If your distribution doesn't have a graphical software manager, or if
you didn't install a graphical user interface for your server, you can remove
these from the command line. You'll need to be logged in as the root user
to issue the commands to do this. Note that in the following commands, shell# represents
the shell prompt, and shouldn't be typed in.In Debian, you can use apt-get to remove the relevant
packages:
shell#apt-get remove mysql-server
shell#apt-get remove mysql-client
shell#apt-get remove php4
In RedHat or Mandrake, you can use the rpm command-line
utility:
shell#rpm -e mysql
shell#rpm -e php
If any of these commands tell you that the package in question is not
installed, don't worry about it unless you know for a fact that it is. In
such cases, it will be necessary for you to remove the offending item by hand.
Seek help from an experienced user if you don't know how. If the last command
runs successfully (i.e. no message is displayed), then you did indeed have
an RPM version of PHP installed, and you'll need to do one more thing to get
rid of it entirely. Open your Apache configuration file (usually /etc/httpd/conf/httpd.conf)
in your favourite text editor and look for the two lines shown here. They
usually appear in separate sections of the file, so don't worry if they're
not together. The path of the libphp4.so file may also
be slightly different (e.g. extramodules instead of just modules).
If you can't find them, don't worry — it just means that the package
utility was smart enough to remove them for you.
LoadModule php4_module modules/libphp4.so
AddModule mod_php4.c
These lines are responsible for telling Apache to load PHP as a plug-in
module. Since you just uninstalled that module, you'll need to get rid of
these lines to make sure Apache keeps working properly. You can comment out
these lines by adding a hash (#) at the beginning of both
lines.To make sure Apache is still in working order, you should now restart
it without the PHP plug-in:
shell#apachectl graceful
With everything neat and tidy, you're ready to download and install
MySQL and PHP.
Installing MySQL
MySQL is freely available for Linux
from http://www.mysql.com/. Download
the latest stable release (listed as recommended on the
download page); as of this writing this is MySQL 3.23.54a, which you'll find
at http://www.mysql.com/downloads/mysql-3.23l.
You should grab the Linux (x86, libc6) version under Binary
packages in the Linux downloads section.With the program downloaded (it was about 9.4MB as of this writing),
you should make sure you're logged in as root before proceeding with the installation,
unless you only want to install MySQL in your own home directory. To begin,
move to /usr/local (unless you want to install MySQL
elsewhere for some reason) and unpack the downloaded file to create the MySQL
directory (replace version with
the full version of your MySQL download to match the downloaded file name
on your system):
shell#cd /usr/local
shell#tar xfz mysql-version.tar.gz
Next, create a symbolic link to the mysql-version directory
with the name mysql to make accessing the directory easier,
then enter the directory:
shell#ln -s mysql-version mysql
shell#cd mysql
MySQL is now installed, but before it can do anything useful its database
files need to be installed too. Still in the new mysql directory,
type the following command:
shell#scripts/mysql_install_db
With MySQL installed and ready to store information, all that's left
is to get the server running on your computer. While you can run the server
as the root user, or even as yourself (if, for example, you installed the
server in your own home directory), the best idea is to set up on the system
a special user whose sole purpose is to run the MySQL server. This will remove
any possibility of someone using the MySQL server as a way to break into the
rest of your system. To create a special MySQL user, you'll need to log in
as root and type the following commands:
shell#groupadd mysql
shell#useradd -g mysql mysql
By default, MySQL stores all database information in the data subdirectory
of the directory to which it was installed[5]. We want to make it so that nobody can access that directory except
our new MySQL user. Still assuming you installed MySQL to the /usr/local/mysql directory,
you can use these commands:
shell#cd /usr/local/mysql
shell#chown -R mysql data
shell#chgrp -R mysql .
shell#chmod -R go-rwx data
Now everything's set for you to launch the MySQL server for the first
time. From the MySQL directory, type the following command:
shell#bin/safe_mysqld --user=mysql &
If you see the message mysql daemon ended,
then the MySQL server was prevented from starting. The error message should
have been written to a file called hostname.err (where hostname is your machine's host name) in MySQL's data directory.
You'll usually find that this happens because another MySQL server is already
running on your computer.If the MySQL server was launched without complaint, the server will
run (just like your Web or FTP server) until your computer is shut down. To
test that the server is running properly, type the following command:
shell#bin/mysqladmin -u root status
A little blurb with some statistics about the MySQL server should be
displayed. If you receive an error message, something has gone wrong. Again,
check the hostname.err file
to see if the MySQL server output an error message while starting up. If you
retrace your steps to make sure you followed the process described above,
and this doesn't solve the problem, a post to the SitePoint
Forums will help you pin it down in no time.If you want your MySQL server to run automatically
whenever the system is running (just like your Web server probably does),
you'll have to set it up to do so. In the support-files subdirectory
of the MySQL directory, you'll find a script called mysql.server that can
be added to your system start-up routines to do this.First of all, assuming you've set up a special MySQL user to run the
MySQL server, you'll need to tell the MySQL server to start as that user by
default. To do this, create in your system's /etc directory
a file called my.cnf that contains these two lines:
[mysqld]
user=mysql
Now, when you run safe_mysqld or mysql.server to
start the MySQL server, it will launch as user mysql automatically.
You can test this by stopping MySQL, and then running mysql.server with
the start argument:
shell#bin/mysqladmin -u root shutdown
shell#chmod u+x support-/image/library/english/10007_mysql.server
shell#support-/image/library/english/10007_mysql.server start
In recent versions of MySQL as of this writing, mysql.server may
spit out an error message along the lines of “@HOSTNAME@:
command not found”. This error is the result of a bug
in the binary distribution of MySQL for Linux, and can be easily remedied.
Simply open mysql.server in your favourite text editor
and find the single occurrence of the string @HOSTNAME@ in
the file. Replace it with /bin/hostname, to point to the
program on your server that will output the machine's host name. Save that
change, shutdown MySQL again, and try starting it using mysql.server
start. This time, it should work.
Request the server's status using mysqladmin as
before to make sure it's running correctly.All that's left to do is to set up your system to run mysql.server automatically
at start-up (to launch the server) and at shutdown (to terminate the server).
This is a highly operating system-dependant task. If you're not sure of how
to do it, you'd be best to ask someone who knows. The following commands,
however, will do the trick for most versions of Linux:
shell#cp /usr/local/mysql/support-/image/library/english/10007_mysql.server /etc/init.d/
shell#cd /etc/init.d
shell#chmod 755 mysql.server
shell#cd /etc/rc2.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc3.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc5.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc0.d
shell#ln -s ../init.d/mysql.server K01mysql
That's it! To test that this works, reboot your system and request the
status of the server as before.One final thing you might like to do for convenience's sake is to place
the MySQL client programs, which you'll use to administer your MySQL server
later on, in the system path. To this end, you can place symbolic links to mysql, mysqladmin,
and mysqldump in your /usr/local/bin directory:
shell#ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
shell#ln -s /usr/local/mysql/bin/mysqladmin
/usr/local/bin/mysqladmin
shell#ln -s /usr/local/mysql/bin/mysqldump
/usr/local/bin/mysqldump
Installing PHP
As mentioned above, PHP is not really a program
in and of itself. Instead, it's a plug-in module for your Web server (probably
Apache). There are actually three ways to install the PHP plug-in for Apache:
As a CGI program that Apache runs every time it needs to process
a PHP-enhanced Web page.
As an Apache module compiled right into the Apache program.
As an Apache module loaded by Apache each time it starts up.
The first option is the easiest to install and set up, but it requires
Apache to launch PHP as a program on your computer every time a PHP page is
requested. This activity can really slow down the response time of your Web
server, especially if more than one request needs to be processed at a time.The second and third options are almost identical in terms of performance,
but since you're likely to have Apache installed already, you'd probably prefer
to avoid having to download, recompile, and reinstall it from scratch. For
this reason, we'll use the third option.To start, download the PHP Complete Source Code package from http://www.php.net/. At the time of this
writing, PHP 4.x has become well-established as the version of choice; however,
some old servers still use PHP 3.x (usually because nobody has bothered to
update it). I'll be covering the installation of PHP 4.3.0 here, so be aware
that if you still work with PHP 3.x there may be some minor differences.The file you downloaded should be called php-version.tar.gz.
To begin, we'll extract the files it contains (the shell% prompt
is to represent that you can run these steps without being logged in as root):
shell%tar xfz php-version.tar.gz
shell%cd php-version
To install PHP as a loadable Apache module, you'll need the Apache apxs program.
This comes with most versions of Apache, but if you're using the copy that
was installed with your distribution of Linux, you may need to install the
Apache development RPM package to access Apache apxs.
You should be able to install this package by whatever means your software
distribution provides. For example, on Debian Linux, you can use apt-get to
install it as follows (you'll have to log in as root first):
shell#apt-get install apache-dev
By default, RedHat and Mandrake will install the program as /usr/sbin/apxs,
so if you see this file, you know it's installed.For the rest of the install procedure, you'll need to be logged in as
the root user so you can make changes to the Apache configuration files.The next step is to configure the PHP installation program by telling
it which options you want to enable, and where it should find the programs
it needs to know about (like Apache and MySQL). Unless you know exactly what
you're doing, simply type the command like this (all on one line):
shell#./configure--prefix=/usr/local/php --with-apxs
--enable-magic-quotes
Important | If you're using Apache 2.0 or later, you need to type --with-apxs2 instead of --with-apxs to enable support for Apache 2.0. As of this writing, this support is still experimental and is not recommended for production sites. As a result of the ongoing work on this front, you may need to download the latest pre-release (unstable) version of PHP to get it working with the latest release of Apache 2.0, but it's worth trying the stable release version first.For full instructions on how to download the latest pre-release version of PHP, see http://www.php.net/anoncvs.php. |
Again, check for any error messages and install any files it identifies
as missing. On Mandrake 8.0, for example, it complained that the lex command
wasn't found. I searched for “lex” in the Mandrake package list
and it came up with flex, which it described as a program
for matching patterns of text used in many programs' build processes. Once
that was installed, the configuration process went without a hitch. After
you watch several screens of tests scroll by, you'll be returned to the command
prompt. The following two commands will compile and then install PHP. Take
a coffee break: this will take some time.
shell#make
shell#make install
As of this writing, the make command often ends with
a warning message about the function tempnam being dangerous
(the exact wording will vary with your configuration), and is often mistaken
as a sign that the process has failed. Don't worry — the warning is
normal, and you can safely proceed with make install.Upon completion of make install, PHP is installed
in /usr/local/php (unless you specified a different directory
with the --prefix option of the configure script
above), with one important exception — its configuration file, php.ini. PHP comes with
two sample php.ini files called php.ini-dist and php.ini-recommended.
Copy these files from your installation work directory to the /usr/local/php/lib directory,
then make a copy of the php.ini-dist file and call it php.ini:
shell#cp php.ini* /usr/local/php/lib/
shell#cd /usr/local/php/lib
shell#cp php.ini-dist php.ini
You may now delete the directory from which you compiled PHP —
it's no longer needed.We'll worry about fine-tuning php.ini shortly.
For now, we need to tweak Apache's configuration to make it more PHP-friendly.
Open your Apache httpd.conf configuration file (usually
under /etc/apache/ or /etc/httpd/ if
you're using your Linux distribution's copy of Apache) in your favourite text
editor.Next, look for the line that begins with DirectoryIndex.
In certain distributions, this may be in a separate file called commonhttpd.conf.
This line tells Apache what file names to use when it looks for the default
page for a given directory. You'll see the usual indexl and
so forth, but you need to add index.php, index.php3,
and index.phtml to that list if they're not there already:
DirectoryIndex indexl ... index.php index.php3 index.phtml
Finally, go right to the bottom of the file (again, this should go in commonhttpd.conf if
you have such a file) and add these lines, to tell Apache which file extensions
should be seen as PHP files:
AddType application/x-httpd-php .php .php3 .phtml
AddType application/x-httpd-php-source .phps
That should do it! Save your changes and restart your Apache server.
If all things go according to plan, Apache should start up without any error
messages. If you run into any trouble, the helpful folks in the SitePoint
Forums (myself included) will be happy to help.[5]Until recently,
it used the var subdirectory.