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 LinuxYou 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 beginning to install PHP on Linux, check the following:
httpd --l
You might have to be in the directory where httpd is located before the command will work. The output usually shows a long list of modules. All you need to be concerned with for PHP is mod_so. If mod_so is not loaded, Apache must be reinstalled using the enable-module=so option.
The apxs utility is installed. apxs is installed when Apache is installed. You should be able to find a file called apxs. If Apache was already installed on Linux or installed from a Linux distribution Web site, apxs might not have been installed. Some Apache installations consist of two installation packages: one for the basic Apache server and one for Apache development tools. Possibly the development tools, which contain apxs, need to be installed.
httpd --vYou might have to be in the directory where httpd is located before the command will work.
As of this writing, the PHP Web site does not recommend using Apache 2 with PHP on Linux/Unix. For use on production Web sites, it might be better to use Apache 1.3 than Apache 2. Keep updated on the status of PHP with Apache 2 by checking www.php.net/manual/en/install.unix.apache2.php
To install PHP on Unix or Linux with an Apache Web server, follow these steps:
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.
gunzip -c php-5.2.1.tar.gz | tar -xf –
A new directory called php-5.2.1 is created with several subdirectories.
Change to the new directory that was created when you unpacked the tarball.
For example, type cd php-5.2.1.
./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.
Type make.
You will see many lines of output. Wait until it’s finished. This might take a few minutes.
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.
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:
httpd --v
You might have to be in the directory where httpd is located before the command will work.
As of this writing, the PHP Web site does not recommend using Apache 2 with PHP. For use on production Web sites, it might be better to use Apache 1.3 than Apache 2. Keep updated on the status of PHP with Apache 2 by checking the PHP Web site at www.php.net/manual/en/install.unix.apache2.php.
httpd --l
You might have to be in the directory where httpd is located before the command will work. The output usually shows a long list of modules. All you need to be concerned with for PHP is mod_so. If mod_so is not loaded, Apache must be reinstalled.
The apxs utility is installed. apxs is normally installed when Apache is installed. To determine whether it’s installed on your computer, you should look for a file called apxs, usually in the /usr/sbin/apxs directory. If you can find the file, apxs is installed; if not, it’s not.
To install PHP on Mac, follow these steps:
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.
tar xvfz php-5.2.1.tar.gz
A new directory called php-5.2.1 is created with several subdirectories.
cd php-5.2.1
--prefix=/usr --sysconfdir=/etc --localstatedir=/var --mandir=/usr/share/man
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.
Type make.
You will see many lines of output. Wait until it is finished. This might take a few minutes.
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 OptionsOption | 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. |
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:
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.
Activate MySQL support. Instructions are provided in Activating MySQL Support.
Configure your Web server. See Configuring your Web Server for instructions.
Configure PHP. Follow the directions in Configuring PHP.