Installing PHP5 with Linux and Apache

At the time of this writing, the very first release candidate of
PHP5 was available, and that's the one we're using. But you may want to check
the PHP site for more recent versions, and any notes about changes.

The combination of Linux, Apache, MySQL, and PHP is probably the
most common production environment for running PHP Web servers. This combination
of open-source software has been referred to by the acronym LAMP. If you run the
same combination of software, you can benefit from the experiences of the many
other people who've used this setup.

The PHP developers work very closely with the Apache and MySQL
teams to ensure that advances in the three server systems are fully supported by
the other components. However, at the time of this writing PHP5 is being
distributed with SQLite rather than MySQL, because there is some concern about
whether MySQL is still open source. This may not be a concern when you read this
and begin developing, but it's worth noting.

Choosing Your
Installation Method

As with other open-source software, you have the option of
downloading the PHP and Apache source code (which, in both cases, is written in
the C programming language) and compiling the programs yourself. If that sounds
daunting (it's not actually as scary as it sounds), you can obtain precompiled
versions in one of two forms: binary downloads, which are precompiled versions
of the software that typically come with installation scripts to put all the
required pieces into the necessary parts of your file system, and binary
packages, which are available for systems that have a software package
management system, such as the Red Hat Package Manager (RPM) for Linux, and are
the easiest to install.

Here's a quick overview of the three methods:

Installation Method

Advantages

Disadvantages

Source

Most flexible solution for custom installations. Additional
tests and examples are included in the source distribution

Needs to be compiled. Slightly more difficult than the other
options. Harder to remove once it's been done

Binary (compiled)

No need to mess around with trying to compile the server.
Takes less time to install

Less flexible than doing an installation from
source

Binary RPMs

Fastest and easiest installation method. Very easy to
uninstall or upgrade later

Must be using an RPM-based Linux distribution such as Red
Hat. Least flexible installation method

An RPM
Installation of PHP4

The version of Red Hat we're using is actually called
Fedora, because Red Hat has split off development into two parts: Fedora and the
enterprise version of Red Hat Linux. Currently, the Fedora site doesn't have an RPM for PHP5, so we'll provide the
instructions for getting and installing the RPM for PHP4 here, and then show how
to download and compile PHP5 for Fedora later. By the time you read this, in all
likelihood there will be an RPM available for PHP5 for your Linux distribution,
so the RPM installation presented here should provide good guidance for
installing PHP5 via the RPM method.

A number of popular Linux distributions use the Red Hat Package
Manager, including Red Hat, SuSE, Mandrake, Definite, TurboLinux, Caldera, and
Yellow Dog. If your system uses an alternative package management system, such
as Debian's deb packages, refer to your distribution's manual for installation
instructions.

Obtaining
RPMs

The best place to get RPMs is almost always the disks from
which you installed your Linux system. Red Hat 7 and SuSE 7 both include PHP4
(although it isn't installed by default)â€”by the time you read this, the same
should be true of most current Linux distribution versions.

If your distribution doesn't include PHP4, or it doesn't include
all the required functionality or support RPMs, then the next place to check is
your Linux distribution vendor's Web site, which should have a download area or
FTP site from which you can obtain the latest RPMs.

Finally, www.rpmfind.net provides a
comprehensive search service for RPMs. When you download RPMs, though, make
certain that they are compatible with your Linux distribution and your computer
hardware. Different distributions put important files in different places, and
this can lead to RPMs from different vendors not working on other systems. Most
RPMs are available compiled to run on the different hardware systems that Linux
supports. The following table shows the most common abbreviations used in RPM
names (you need the abbreviation to search on the rpmfind site):

Servers and workstations running the processors which use
the 64-bit SPARC architecture, such as Sun Microsystems'
UltraSPARC

m68k

Computers built around Motorola's older 68000 series
processors, such as Amigas, and older Apple Macintoshes, for which various Linux
ports exist

Refer to your distribution's manual if you want to use the
graphical installation tools that come with your specific distribution. These
differ widely, so they can't all be covered here. However, any RPM-based system can be controlled using the rpm
command-line tool, and you'll see how to install the required components using
this interface.

Which RPM
Packages do You Need?

The RPM packages you will need are:

zlib

libpng

libjpeg

gd

gd-devel

apache

mod_php4

You can find out which of them are already installed on your
system by typing the following at a command prompt, substituting in the name of
each of these packages in turn:

As you can see, if the package is installed, it gives you a
random-looking string. If it isn't installed, you get a helpful error message.
The string actually tells you which version of the software you installed using
the package (1.1.3 in this case), which release of the package it is (this
example has the sixth public release installed), and the architecture for which
the RPM was compiled (Intel 386 compatible, which is just as well, because the
package is installed on a Pentium III for this book).

Note which of the packages you already have, and which versions
they are (the version number is more important than the release number).

Apache is at version 1.3.29 if you want to remain at the old
versions of GD, or 2.0.48 if you want to be current with the latest version of
GD. Of course, if you are installing PHP5, GD is now bundled with PHP and is up
to version 2.0.17

Then locate suitably up-to-date versions of all the packages that
you don't have already, or have old versions for. As suggested, try your install
CDs, your distributor's Web site, and www.rpmfind.net.

Once you have current versions of all the packages you need, you
can install them. The command for upgrading an existing installation or
installing a package for the first time is exactly the same. Navigate your
command prompt to the location of the files on the CD or the directory into
which you downloaded the RPMs. As root, type:

> rpm -Uh libpng-1.0.5-3-i386.rpm
##################

For each package you need to upgrade or install, just substitute
the name of the package file you downloaded. The line of # signs extends across
the screen as each installation progresses.

If you install the packages in the order listed previously,
you should find that all the prerequisite files are installed in the necessary
order.

Installing
PHP5 by Compiling From Source Files

The installation method we'll use for installing PHP5 on Red
Hat Fedora running Apache is downloading the source files and compiling them.
You use command-line commands in Linux, but also make use of some of the visual
tools (such as Konqueror) included in your Red Hat installation. If you are
running Linux visually (for example, with KDE), you get to the command prompt by
going to the Red Hat button, and then choosing System Tools â‡¨ Terminal.

You must have a compiler installed (an ANSI C compiler). Sometimes
such a compiler is installed as part of your Linux installation, but if you need
one, a good one (and free), named gnugcc, can be found at www.gnu.org.

With your compiler installed, download the source file from www.php.net. This file is
archived as a tar file and compressed with gzip, so you will need to uncompress
it. There is also a .bz2 file you can download, but you
need only one of these filesâ€”either the gzip or a .bz2
file.

You can use the Konquerer file-management tool to view the
contents of the compressed file.

You also could use Konquerer to copy all of the compressed file's
contents directly to another folder, but doing so will make your compilation
fail cryptically (meaning you'll get strange error messages that won't help you
figure out what's wrong). Instead, make sure to use the following command from
the terminal (see Figure 1-5) to
uncompress the files:

tar -xvzf php-5.0(insert the rest of the version number here).tar.gz

Next, use the cd command to change to the
PHP5 distribution directory:

cd php-5.0(insert the rest of the version number here)

Now that you've cd'd to the php-5.0.0RC1
folder, you'll see quite a few folders and files there. Open the INSTALL text file to find many of the instructions related
to your installation.

Folder and directory are
equivalent terms and can be used interchangeably.

For this book, PHP is installed as a Dynamic Shared Object (DSO),
and that's what you'll also do, so that the entire Apache Server won't need to
be recompiled.

The latest versions of Apache support DSOs, and shared objects can
be used by other programs, such as PostGreSQL. Although you could compile PHP5
as a static module, that isn't recommended. If PHP is statically linked to, say,
Apache or PostGreSQL, each of those programs would need to be recompiled before
they would recognize PHP. The programs' configuration files can be easily
changed in shared objects (DSOs) without any recompiling.

Checking
Apache for DSO Installation

You must have Apache installed and set up for dynamic
modules before compiling PHP5 as a DSO. Use the following command from the
terminal to make sure Apache is ready:

httpd -1

As long as mod_so.c is present, you're OK
to proceed.

Running the
Configure Script

Within your PHP5 directory (probably named something like
php-5.0.0RC1.), you'll find a shell script named configure. This script accepts arguments to control the
features that PHP will support. You'll need to run this script to configure
compilation, not PHP5 itself (PHP configuration with php.ini will come later).

Commands
Available for the Configure Script

The default configure is to compile PHP5 as a CGI binary.
Use the -with-apache option to compile PHP5 as a static
module; use the -with-apxs option to compile PHP5 as a
DSO. For this installation, use the -with_apxs option
(actually -with_apxs2 because you're running Apache
2).

Here are some of the command line arguments that you may use when
you compile PHP5. The configure command is ./configure
(the ./ lets it run) followed by a single space and then as many of the
following options as you like:

-enable-track-vars: Automatically
populates associative arrays with values submitted as part of GET and POST requests or provided in
a cookie.

-with-gd = /path/to/directory:
Enables support for the GD library that allows you to create dynamic GIF and PNG
images from your scripts. You'll either want to compile with this or add this
module later to do the graphics work.

-with-mysql = /path/to/directory:
WithMySQL support.

-with-pgsql = /path/to/directory:
WithPostgreSQL support.

For your quick install use only -with-mysql and -with-apxs2. If you
get any error messages telling you something couldn't be found, provide the full
path to the folder in which the appropriate files can be found. For example, our
configure command found the path to mysql. If it hadn't we would have provided
the full path to mysql as part of the command to run the configure
script.

Other
Configure Options

There are many more command line arguments that you can use
as well. For example, type in the command

./configure --help

and you'll see the complete range of arguments that you can
use, along with their descriptions.

Performing the
QUICK INSTALL (DSO)

The quick installation in the PHP INSTALL text file recommends starting with just two commands:
-with-mysql and -with-apxs.
Run the configure script like this for quick installation:

./configure --with-mysql --with-apxs2

You need to use -with-apxs2 rather than
-with-apxs because you're running a later version of
Apache. The script actually came back and informed me of this when it ran, which
was very helpful. If you read through the commands that appear in your terminal
as the script runs, you'll see that it does quite a few such checks as it gets
ready for the make command.

After the configure script has run, you need to enter two more
commands:

make
make install

Install makes a directory in /user/local/lib named php where it
places a copy of PEAR (php Extension Add-on Repository) and the php.ini file.

Running
Additional Configure Options

You may use some of the other options of the configure
script to compile PHP5 with enable_track_vars, with-gd,
and with_pgsql. But if you'd like to use the configure
options for gd (the graphics module) and pgsql (the database) you must make sure
these programs are also loaded for everything to work properly, and you must
provide the full path to the installations as required.

Running as a
CGI

PHP5 is compiled as a module if you used the configure
script with the -with-apache or --with-apxs2 options. It's compiled as a CGI executable
binary file if you used the configure script without any reference to Apache or
apxs. And if you compiled PHP5 as a CGI, then when you installed PHP5, it most
likely put the actual binary in /usr/local/bin. Copy
the CGI executable binary to your cgi-bin directory using:

cp /usr/local/bin/php /usr/local/apache/cgi-bin/php.cgi

This enables Apache users to run different PHP-enabled pages
under different user-ids. However, CERT advisory CA-96.11 recommends against
placing any interpreters (such as PHP5) into cgi-bin because attacks such as accessing system files and Web documents can
be launched against your server. When you compile PHP5 with the -with-apache option, you will have a Server Application
Programming Interface (SAPI), which provides a higher level of performance and
security over the CGI method.

Setting up
Apache for PHP

To install Apache, use RPMs or download and compile the
source code. But Apache probably comes with your Linux distribution, and may
already be properly installed. On my Red Hat Fedora installation, Apache was
already installed, and all that was needed to verify this was go to the Red Hat
button, choose System Settings â‡¨ Server Settings
â‡¨ Services, and look for httpd.

Httpd (all lowercase) means HTTP daemon, and daemon is the name of
services running in the background on Linux machines. So httpd means the HTTP
daemon running in the background, for example, the Web server.

If you're using Linux visually (running KDE, for example), click
the httpd service to see whether it's running. If it isn't, start it, and then
open your browser and enter http://localhost.

If Apache is not already installed and running, you can use these
commands for installing it from the Terminal:

You must replace the NN with the minor
version number, and PREFIX with the correct path in
which you'd like to install Apache (the default is /usr/local/apache2).

Configuring
Apache to Run PHP5

If PHP5 is installed as a DSO (as the example installation
was), you need to check the Apache configuration file (named httpd.conf) to make sure it has several entries. In the
Fedora installation, you can find the httpd.conf file
in the /etc/httpd/conf folder. Open any text editor and let's modify the httpd.conf file.

First, ensure that PHP is enabled on your Apache server. Look for
a lot of lines that begin with the word LoadModule.
Among them you should find a line like this:

LoadModule php5_module /usr/local/apache/lib/libphp5.so

If there isn't such a line, you need to add one; or if the path is
wrong, you need to correct it. Find out where the PHP compile put your libphp5.so file. Open Konqueror and choose Tools â‡¨ Find file. My installation put the libphp5.so file in

/usr/lib/httpd/modules

You need this information to tell Apache how to run PHP scripts.
In the Apache configuration file httpd.conf, add the
LoadModule instruction to load PHP5. Put it after any
of the other LoadModule lines, using the path you just
obtained:

LoadModule php5_module /usr/lib/httpd/modules/libphp5.so

Now that Apache knows how to load PHP5, you need to activate PHP5
in Apache. There's a section farther down the file consisting of a lot of lines
beginning with AddModule. If you find a ClearModulesList
line in the file, you need to add the following line to the file. Although it
doesn't matter where you put it, it makes sense to locate it near other AddModule lines for easy access in the future:

AddModule mod_php5.c

The AddModule line is not required unless
you have a ClearModulesList line.

Finally, you tell Apache how to recognize a PHP program file by
its extension. Further down the document are some directives that begin AddType. To the end of these, add the following line:

AddType application/x-httpd-php .php

This tells Apache that all files that end in .php are PHP programs. Now you're done, so save the
file.

Starting or
Restarting Apache

Check to see if Apache is running by going into Services
once again and checking the httpd service. If it's not running, start it. Verify
that it's running by opening the http://localhost test
page. If everything seems to be working OK (and you're not also installing on
Windows), move on to the "Testing Your Installation"
section to test that PHP is working properly.

The majority of Web hosting computers runs some version of
Linux, such as Debian, RedHat, FreeBSD, and so on. The Web server of choice for
these machines is Apache. PHP is compatible with Linux and Apache, so you can
install and configure PHP on these systems. However, if you are not in charge of
the Web hosting computer (and many times you won't be) you'll probably have
little control over the installation and configuration. If you find yourself in
this position (for example, if you've been hired to work on an existing Web site
running on someone else's server) you can simply verify the operating system,
Web server software, and PHP version so you can cope with whatever you've got as
you develop your PHP programs.

Private Cloud Hosting

Lunarpages
offers all customers private cloud hosting solutions that are tailored
specifically for their needs and their business model. Our company has
extensive experience in building custom private cloud hosting platforms
for our customers that require control over their resources and
performance.

Every
customized private cloud hosting plan requires a consultation with one
of our experienced cloud specialists to ensure that we build your
private cloud hosting system based on your businessâ€™ needs and
specifications.

Lunarpages offers assistance with clustering Web and SQL services, as
well as managed services for both hardware nodes and Virtual Machines.

Below is an example of a customized build that was set up for one of Lunarpages customers.