Installing PHP

Although PHP runs on many platforms, I describe installing it on Unix, Linux, Mac, and Windows, which includes the majority of Web sites on the Internet. PHP runs with several Web servers, but these instructions focus mainly on Apache and Internet Information Servers (IIS) because together they power almost 90 percent of the Web sites on the Internet. If you need instructions for other operating systems or Web servers, see the PHP Web site, at www.php.net.

This document provides installation instructions for PHP 5 and 6. If you’re installing an earlier version, there are some small differences, so read the install.txt file provided with the PHP distribution.

Installing on Unix and Linux
Installing on Mac
Installation options for Linux and Mac
Installing on Windows

Installing on Unix and Linux

You can install PHP as an Apache module or as a stand-alone interpreter. If you’re using PHP as a scripting language in Web pages to interact with a database, install PHP as an Apache module. PHP is faster and more secure as a module. I don’t discuss PHP as a stand-alone interpreter in this document.

I provide step-by-step instructions in the next few sections for compiling and installing PHP on Linux and Unix. Read all the way through the steps before you begin the installation procedure.

Before installing

Before beginning to install PHP on Linux, check the following:

Installing

To install PHP on Unix or Linux with an Apache Web server, follow these steps:

  1. Change to the directory where you downloaded the source code (for instance, cd /usr/src). You see a file named php-, followed by the version name and tar.gz.

  2. Unpack the tarball. The command for PHP version 5.2.1 is
       gunzip -c php-5.2.1.tar.gz | tar -xf –

    A new directory called php-5.2.1 is created with several subdirectories.

  3. Change to the new directory that was created when you unpacked the tarball.
    For example, type cd php-5.2.1.

  4. Type the configure command. The configure command consists of ./configure, followed by the configuration options you want to use. The minimum configure command is:
       ./configure --with-apxs

    If you’re using Apache 2, use the option with-apxs2.

    You may want to use other configuration options with the configure command. The available configuration options are discussed in Installation Options.

    You activate MySQL support with a configuration option. Activating MySQL support is discussed later in this section.

    When you type the configure command, you will see many lines of output. Wait until the configure command has finished. This might take a few minutes. If the configure command fails, it provides an informative message. Usually, the problem is missing software. You see an error message indicating that xyz software can’t be found or that xyz version 5.6 is required but xyz version 4.2 is found. You need to install or update the software that PHP needs.

    If the apxs utility isn’t installed in the expected location, you see an error message indicating that apxs couldn’t be found. If you get this message, check the location where apxs is installed (find / -name apxs) and include the path in the with-apxs option of the configure command:

       --with-apxs=/usr/sbin/apxs

    or /usr/local/apache/bin/apxs. If you’re using Apache 2, the option is --with-apxs2=/usr/sbin/apxs.

  5. Type make.

    You will see many lines of output. Wait until it’s finished. This might take a few minutes.

  6. Type make install.

Installing on Mac OS X

Beginning with PHP 4.3, you can install PHP on Mac OS X as easily as on Unix and Linux. You install PHP by downloading source files, compiling the source files, and installing the compiled programs. Read all the way through the steps before you begin to be sure that you understand it all clearly and have everything prepared so you don’t have to stop in the middle of the installation.

Before installing

If you want to use PHP with Apache for your Web site, Apache must be installed. Most Mac OS X systems come with Apache already installed.

Before beginning to install PHP, check the following:

Installing

To install PHP on Mac, follow these steps:

  1. Change to the directory where you downloaded PHP (for example, cd /usr/src).

    You see a file named php-, followed by the version name and tar.gz. This file contains several files compressed into one file. The file might have been unpacked by the StuffIt Expander automatically so that you see the directory php-5.2.1. If so, skip to Step 3.

  2. Unpack the tarball.The command to unpack the tarball for PHP version 5.2.1 is:
       tar xvfz php-5.2.1.tar.gz

    A new directory called php-5.2.1 is created with several subdirectories.

  3. Change to the new directory that was created when you unpacked the tarball. For example, you can use a command like the following:
       cd php-5.2.1
  4. Type the configure command. The configure command consists of ./configure followed by all the necessary options. The minimum set of options follows:
    • Location options: Because the Mac stores files in different locations than the PHP default locations, you need to tell PHP where files are located. Use the following options:
        --prefix=/usr 
        --sysconfdir=/etc 
        --localstatedir=/var 
        --mandir=/usr/share/man
    • zlib option: --with-zlib.
    • Apache option: If you are installing PHP for use with Apache, use the following option: --with-apxs or --with-apxs2.

    The most likely configuration command is

       ./configure --prefix=/usr --sysconfdir=/etc 
           --localstatedir=/var --mandir=/usr/share/man
           --with-apxs –-with-zlib

    You also need to use an option to include MySQL support. See Activating MySQL Support later in this section.

    You can type the configure command on one line. If you use more than one line, type a \ at the end of each line.

    You will see many lines of output. Wait until the configure command has finished. This may take a few minutes.

    If the apxs utility isn’t installed in the expected location, you see an error message, indicating that apxs could not be found. If you get this error message, check the location where apxs is installed (find / -name apxs) and include the path in the with-apxs option of the configure command:

       --with-apxs=/usr/sbin/apxs.

    You might need to use many other options, such as options that change the directories where PHP is installed. These configure options are discussed in the Installation options section.

  5. Type make.

    You will see many lines of output. Wait until it is finished. This might take a few minutes.

  6. Type sudo make install.

Installation options for Unix/Linux/Mac

The previous sections give you steps to quickly install PHP on Unix, Linux, or Mac with the options needed to install PHP and activate MySQL. However, you might want to install PHP differently. For instance, all the PHP programs and files are installed in their default locations, but you might need to install PHP in different locations. Or you might be planning applications using additional software. You can use additional command-line options if you need to configure PHP for your specific needs. Just add the options to the command shown in Step 10 of the Unix and Mac installation instructions. In general, the order of the options in the command line doesn’t matter. Table 2-1 shows the most commonly used options for PHP. To see a list of all possible options, type ./configure --help.

Table 2-1 PHP Configure Options
Option Tells PHP to
prefix=PREFIX Set main PHP directory to PREFIX. Default PREFIX is /usr/local.
exec-prefix=EPREFIX Install architecture dependent files in EPREFIX. Default EPREFIX is PREFIX.
bindir=DIR Install user executables in DIR. Default is EPREFIX/bin.
infodir=DIR Install info documentation in DIR. Default is PREFIX/info.
mandir=DIR Install man files in DIR. Default is PREFIX/man.
with-config-file-path=DIR Look for the configuration file (php.ini) in DIR. Without this option, PHP looks for the configuration file in a default location, usually /usr/local/lib.
disable-libxml Disable XML support that’s included by default.
enable-ftp Enable FTP support.
with-apxs=FILE Build a shared Apache module using the apxs utility located at FILE. Default FILE is apxs.
with-apxs2=FILE Build a shared Apache 2 module using the apxs utility located at FILE. Default FILE is apxs.
with-mysql=DIR Enable support for MySQL 4.0 or earlier databases. Default DIR where MySQL is located is /usr/local.
with-mysqli=DIR Enable support for MySQL 4.1 or later databases. DIR needs to be the path to the file named mysql_config that was installed with 4.1. Available only with PHP 5 or later.
with-openssl=DIR Enable OpenSSL support for a secure server. Requires OpenSSL version 0.9.5 or later.
with-oci8=DIR Enable support for Oracle 7 or later. Default DIR is contained in the environmental variable, ORACLE_HOME.
with-oracle=DIR Enable support for earlier versions of Oracle. Default DIR is contained in the environmental variable, ORACLE_HOME.
with-pgsql=DIR Enable support for PostgreSQL databases. Default DIR where PostgreSQL is located is /usr/local/pgsql.
with-servlet=DIR Include servlet support. DIR is the base install directory for the JSDK. The Java extension must be built as a shared .dll.

Installing on Windows

PHP runs on Windows 98/Me and Windows NT/2000/XP/Vista. Windows 98/Me can be used for development on a local computer but cannot support a public Web site. Windows 95 is no longer supported as of PHP 4.3.0. PHP does not run on Windows 3.1.

To install PHP 5/6 on Windows, you unzip the file that contains all the necessary files for PHP and store the files in the appropriate locations. The following steps show how to install PHP on Windows:

  1. Extract the files from the .zip file into the directory where you want PHP to be installed, such as c:\php.

    The zip file is named php, followed by the version number and win32.zip, such as php5.2.1-Win32.zip-. If you double-click the .zip file, it should open in the software on your computer that extracts files from .zip files, such as WinZip or PKZIP. Select the menu item for extract and select the directory into which the files are to be extracted. C:\php is a good choice for installation because many configuration files assume that’s where PHP is installed, so the default settings are more likely to be correct. Do not to install PHP in a directory with a space in the path, such as in Program Files\PHP.

    You now have a directory and several subdirectories that contain all the files from the zip file. You should now be able to run PHP programs. Occasionally PHP needs DLL files that it can’t find. When this happens, PHP displays an error message when you run a PHP program, saying that it can’t find a particular DLL. You can usually find the DLL in the ext subdirectory and copy it into the main PHP directory.

  2. Activate MySQL support. Instructions are provided in Activating MySQL Support.

  3. Configure your Web server. See Configuring your Web Server for instructions.

  4. Configure PHP. Follow the directions in Configuring PHP.