Installing PHP
PHP is available for many operating systems and platforms. The most common setup, however, is to use PHP as a module for the Apache web server on a Unix machine. This section briefly describes how to install Apache with PHP. If you’re interested in running PHP on Windows, see Chapter 15, which explains many of your options for that operating system.
To install Apache with PHP, you’ll need a Unix machine with an ANSI-compliant C compiler, and around 10 MB of available disk space for source and object files. You’ll also need Internet access to fetch the source code for PHP and Apache.
Start by downloading the source distributions of PHP and Apache.
The latest files are always available from the web sites for the
respective tools. Since there are so many options on
installation , we are showing here the generic installation
instructions for a Linux server as shown on the PHP web site at
http://ca3.php.net/manual/en/install.unix.php.
You will have to replace the xxx
signifier in
the following steps with the version of the software that you choose to
install.
Tip
Although Apache has a Version 2.x you may find that it is more adept at serving PHP with Version 1.3.xx, so generally we will be using the 1.3.xx version throughout this book.
gunzip apache_
xxx
.tar.gz
tar -xvf apache_
xxx
.tar
gunzip php-
xxx
.tar.gz
tar -xvf php-
xxx
.tar
cd apache_
xxx
.
/configure --prefix=/www --enable-module=so
make
make install
cd ../php-
xxx
Now, configure your PHP. This is where you customize your PHP with various options, like which extensions will be enabled. Do a
./configure --help
for a list of available options. In our example we’ll do a simple configure with Apache 1 and MySQL support. Your path to apxs may differ from our example../configure --with-mysql --with-apxs=/www/bin/apxs
make
make install
If you decide to change your configure options after installation, you only need to repeat the last three steps. You only need to restart apache for the new module to take effect. A recompile of Apache is not needed.
Note that unless told otherwise,
make install
will also install PEAR, various PHP tools such as phpize, install the PHP CLI, and more.Set up your php.ini file:
cp php.ini-dist /usr/local/lib/php.ini
You may edit your .ini file to set PHP options. If you prefer your php.ini in another location, use
--with-config-file-path=/some/path
in step 10.If you instead choose
php.ini-recommended
, be certain to read the list of changes within, as they affect how PHP behaves.Edit your httpd.conf to load the PHP module. The path on the righthand side of the LoadModule statement must point to the path of the PHP module on your system. The make install from above may have already added this for you, but be sure to check.
LoadModule php5_module libexec/libphp5.so
And in the AddModule section of httpd.conf, somewhere under the ClearModuleList, add this:
AddModule mod_php5.c
Tell Apache to parse certain extensions as PHP. For example, let’s have Apache parse the .php extension as PHP. You could have any extension(s) parse as PHP by simply adding more, with each separated by a space. We’ll add .phtml to demonstrate:
AddType application/x-httpd-php .php .phtml
It’s also common to setup the .phps extension to show highlighted PHP source, this can be done with:
AddType application/x-httpd-php-source .phps
Use your normal procedure for starting the Apache server. (You must stop and restart the server, not just cause the server to reload by using a HUP or USR1 signal.)
You should now have Apache installed with PHP enabled. You will also have some of PHP’s many extensions installed. You may also want to change the PHP configuration. To do that you will have to change the php.ini file and restart your Apache server. Each time you make a change to PHP’s environment you will have to re-start the Apache server in order for those changes to take effect.
As was mentioned, PHP’s configuration settings go in a file called php.ini. The settings in this file control the behavior of PHP features, such as session handling and form processing. Later chapters refer to some of the php.ini options, but in general the code in this book does not require a customized configuration. See http://ca3.php.net/manual/en/configuration.php#configuration.file for more information on php.ini configuration.
The PHP and Apache source directories both include files called INSTALL that contain detailed instructions on troubleshooting and building those programs. If you want a nonstandard installation, or if you encounter problems with the instructions presented here, be sure to read the INSTALL files or go to their respective web sites for further assistance.
Get Programming PHP, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.