Tuesday, August 11, 2009

Installing PHP with Apache

lthough 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 (www.php.net).

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

Installing PHP on Unix/Linux/Mac
with Apache

On Unix/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 data­ base, 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 book.

You install PHP by downloading source files, compiling these files, and installing the compiled programs. This process isn’t as technical and daunt­ ing as it sounds. I provide step-by-step instructions in the next few sections. Read all the way through the steps before you begin the installation procedure.

For Linux users only: PHP for Linux is available in an RPM as well as in source files. It might be in RPM format on your distribution CD. However, when you install PHP from an RPM, you can’t control the options that PHP is installed with. For instance, you need to install PHP with MySQL support enabled, but the RPM may not have MySQL support enabled. MySQL is

popular, so many RPMs enable support for it, but it is out of your control. Also, an RPM usually enables all the most popular options, so an RPM might enable options that you don’t need. Consequently, the simplest and most effi­ cient way to install PHP could be from the source. If you’re familiar with RPMs, feel free to find an RPM and install it. RPMs are available. However, I
am providing steps for source code installation, not RPMs.

Before installing
Before beginning to install PHP, check the following:

The Apache module mod_so is installed. It usually is. To display a list of all the modules, type 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 were installed on Linux from an RPM, apxs might not have been installed. Some RPMs for Apache consist of two RPMs: one for the basic Apache server and one for Apache development tools. Possibly the RPM with the development tools, which installs apxs, needs to be installed.

Apache version is recent. See Appendix C for information about Apache versions.

To check the version, type the following:

httpd --v

You might have to be in the directory where httpd is located before the command will work.

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

1. Point your Web browser to www.php.net, the PHP home page.

2. Click Downloads.

3. Click the latest version of the PHP source code, which is version 5.0.0 as of this writing.

The file you are about to download contains many files compressed into one file — a tarball.

A dialog box opens.

4. Select the option to save the file.

A dialog box opens that lets you select where the file will be saved.

5. Navigate to where you want to save the source code (for example,
/usr/src). Then click Save.

6. After the download, change to the download directory (for instance,
cd-/usr/src).

You see a file named php-, followed by the version name and tar.gz.

7. Unpack the tarball. The command for PHP version 5.0.0 is

gunzip -c php-5.0.0.tar.gz | tar -xf –

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

8. Change to the new directory that was created when you unpacked the tarball. For example:

cd php-5.0.0

9. Type the configure command.

Use one of the two following configure commands:

./configure --with-mysql=DIR --with-apxs
./configure --with-mysqli=DIR --with-apxs

Use mysql if you’re using MySQL 4.0 or earlier; use mysqli if you’re using MySQL 4.1 or later. DIR is the path to the appropriate MySQL directory. When using with-mysql, use the path to the directory where mysql is installed, for instance:

--with-mysql=/user/local/mysql

When using with-mysqli, use the path to the file named mysql_
config.

If you’re using Apache 2, use the option with-apxs2. (See Appendix C
for information on using Apache 2.)

You will see many lines of output. Wait until the configure command has completed. 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.

10. Type make.

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

11. Type make install.

On Mac OS X

With the release of PHP 4.3, you can install PHP on Mac OS X as easily as on
Unix/Linux. You install PHP by downloading source files, compiling the
source files, and installing the compiled programs. This process isn’t as tech­ nical and daunting as it might appear. I provide step-by-step instructions in the next few sections. Read all the way through the steps before you begin the installation procedure 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. For more information on Apache, see Appendix C.

Before beginning to install PHP, check the following:

The Apache version is recent: See Appendix C for a discussion of Apache versions. To check the version, type the following on the com­ mand line:

httpd --v

You might have to be in the directory where httpd is located before the command will work.

As of this writing, PHP with Apache 2 is still considered experimental. For use on production Web sites, it might be better to use Apache 1.3 than Apache 2. See Appendix C for a discussion of Apache versions. Keep updated on the status of PHP with Apache 2 by checking the PHP Web site at www.php.net/manual/en/install.apache2.php.

The Apache module mod_so is installed. It usually is. To display a list of all the modules, type 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.

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 direc­ tory. If you can find the file, apxs is installed; if not, it’s not.

The files from the Developer’s Tools CD are installed. This CD is sup­ plemental to the main Mac OS X distribution. If you can’t find the CD, you can download the tools from the Apple Developer Connection Web site at developer.apple.com/tools/macosxtools.html.

Installing
To install PHP on Mac, follow these steps:

1. Point your Web browser to www.php.net, which is the PHP home
page.

2. Click Downloads.

3. Click the latest version of the PHP source code, which is version 5.0.0 as of this writing.

A dialog box opens.

4. Select the option to save the file.

A dialog box opens that lets you select where the file is to be saved.

5. Navigate to where you want to save the source code (for example,
/usr/src), and then click Save.

6. After the download, change to the download directory (for example,
cd-/usr/src).

You see a file named php-, followed by the version name and tar.gz. This file is 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.0.0. If so, skip to Step 8.

7. Unpack the tarball.

The command to unpack the tarball for PHP version 5.0.0 is

tar xvfz php-5.0.0.tar.gz

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

8. 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.0.0

9. Type the configure command:

The configure command consists of ./configure followed by all the necessary options. The minimum set of options is as follows:

• Location options: Because the Mac stores files in different loca­ tions 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.

Therefore, the most likely configuration command that you should use 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. Use one of the following options:

--with-mysql=DIR
--with-mysqli=DIR

Use mysql if you’re using MySQL 4.0 or earlier; use mysqli if you’re using MySQL 4.1 or later. DIR is the path to the appropriate MySQL directory. When using with-mysql, use the path to the directory where mysql is installed, as follows:

--with-mysql=/user/local/mysql

When using with-mysqli, use the path to the file named
mysql_config.

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 completed. 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 for the data­ base that you’re using or options that change the directories where PHP is installed. These configure options are discussed in the section, “Installation Options,” later in this Appendix.

10. Type make.

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

11. Type sudo make install.

Installation Options

The previous sections give you steps to quickly install PHP with the options needed for the applications in this book. 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 loca­ tions. 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 13 of the Unix/Linux installation instructions or Step 9 in the Mac installation
instructions. In general, the order of the options in the command line doesn’t matter. Table B-1 shows the most commonly used options for PHP. To see a list of all possible options, type ./configure —help.

Configuring Apache for PHP

You must configure Apache to recognize and run PHP files. An Apache config­ uration file, httpd.conf, is on your system, possibly in /etc or in /usr/ local/apache/conf. You must edit this file before PHP can run properly.

Follow these steps to configure your system for PHP:

1. Open the httpd.conf file so you can make changes.

2. Configure Apache to load the PHP module.

Find the list of LoadModule statements. Look for the following line:

LoadModule php5_module libexec/libphp5.so.

If this line isn’t there, add it. If a pound sign (#) is at the beginning of the line, remove the pound sign.

3. Configure Apache to recognize PHP extensions.

You need to tell Apache which files might contain PHP code. Look for a section describing AddType. You might see one or more AddType lines for other software. Look for the AddType line for PHP, as follows:

AddType application/x-httpd-php .php

If you find it with a pound sign (#) at the beginning of the line, remove
the pound sign (#). If you don’t find this line, add it to the AddType state­ ments. This line tells Apache to look for PHP code in all files with a .php extension. You can specify any extension or series of extensions.

4. Start (if it is not running) or restart (if it is running) the Apache httpd server.

You can start or restart the server by using a script that was installed on your system during installation. This script might be apachectl or httpd.apache, and might be located in /bin or
/usr/local/apache/bin. For example, you might be able to start the server by typing apachectl start, restart it by using apachectl restart, or stop it by using apachectl stop. Sometimes restarting is not sufficient; you must stop the server first and then start it.

On Windows

PHP runs on Windows 98/Me and Windows NT/2000/XP. It does not run on
Windows 3.1. Windows 95 is no longer supported as of PHP 4.3.0.

To install PHP 5 on Windows with MySQL support, you download a Zip file that contains all the necessary files for PHP, The following steps show how to install PHP on Windows:

1. Point your Web browser at www.php.net.

2. Click Download.

3. Go to the Windows Binaries section. Click the download link for the Sip package for the most recent version of PHP (as of this writing, 5.0.0).

4. Click the link for a mirror Web site from which to download the file and choose the site closest to your location.

A dialog box opens.

5. Select the option to save the file.

A dialog box opens that lets you select where the file will be saved.

6. Navigate to where you want the file to be downloaded. This should be a temporary location, such as a download directory. Then click Save.

After the download is complete, you see a file in the download location containing all the files needed. The file is named php, followed by the version number and win32.zip. For the current version, the file is named php5.0.0-Win32.zip-.

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

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.

It’s best not to install PHP in a directory with a space in the path, such as in Program Files/PHP. It sometimes causes problems.

You now have a directory with several subdirectories that contain the files that you need.

8. Copy the file required for MySQL to the PHP main directory.

The file is located in the ext subdirectory in the directory where PHP is installed. Copy one of the following files, depending on which version of MySQL you’re using:

ext\php_mysql.dll (for MySQL 4.0 or earlier)
ext\php_mysqli.dll (for MySQL 4.1 or later)

Copy the two files into the main PHP directory, such as c:\php.

Another file is required for MySQL support, named libmysql.dll. This file should already be located in the main PHP directory. If it isn’t there, you need to find it and copy it there. If it’s not in your PHP directory, it’s usually installed with MySQL, so find it in the directory where MySQL was installed, perhaps in a bin subdirectory, such as c:\mysql\bin.

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.

9. Configure your Web server.

The next section provides instructions for configuring your Web server.

10. Configure PHP.

Follow the directions in the section later in this chapter.

Configuring Your Web Server for PHP

Your Web server needs to be configured to recognize PHP scripts and run them. Follow the steps in the section for your Web server:

Configuring Apache

You must edit an Apache configuration file, called httpd.conf, before PHP
can run properly To configure Apache for PHP, follow these steps:

1. Open httpd.conf for editing.
You might be able to edit it by choosing Start➪Programs➪Apache
HTTPD Server➪Configure Apache Server➪Edit Configuration.

If Edit Configuration isn’t on your Start menu, find the httpd.conf file on your hard disk, usually in the directory where Apache is installed, in a conf subdirectory (for example, c:\program files\Apache group\ Apache\conf). Open this file in an editor, such as Notepad or WordPad.

2. Set up a nickname for the directory where PHP is installed.

A ScriptAlias statement is used to set up a name for the directory where PHP is installed. Look for ScriptAlias statements in the httpd.conf file. You might see some for other software. If you don’t see one for PHP, add the following:

ScriptAlias /php/ “c:/php/”

The first argument is the name, and the second is what it represents. In this statement, the name /php/ is used to mean c:/php/. Notice that Apache prefers forward slashes. Also, note that the directory path with a special character (the colon) in it is enclosed in double quotes.

3. Configure Apache to run PHP when it encounters a file that is a PHP
program.

An Action statement is used to tell Apache to run PHP when it encoun­ ters a file that is a PHP program. If you don’t find an Action statement for PHP, add the following:

Action application/x-httpd-php /php/php-cgi.exe

Prior to PHP 5, the PHP interpreter was named php.exe. If you’re installing an earlier version of PHP, the line should end with php.exe, rather than php-cgi.exe. If you find an action line for PHP in your httpd.conf file, be sure that it uses the correct PHP interpreter name for the version of PHP that you’re installing.

Notice that the Action statement uses the name defined in the ScriptAlias statement. It locates php-cgi.exe in /php/, which means c:/php/. If you change the ScriptAlias statement to say c:/php27/, the Action statement would then look for php-cgi.exe in c:/php27.

4. You need to tell Apache which files are PHP programs.

Look for a section describing AddType. This section might contain one or more AddType lines for other software. The AddType line for PHP is

AddType application/x-httpd-php .php

Look for this line. If you find it with a pound sign (#) at the beginning of the line, remove the pound sign. If you don’t find the line, add it to the list of AddType statements. You can specify any extension or series of extensions.

This line tells Apache that files with the .php extension are files of the type application/x-httpd-php. Apache then looks at the Action statement from Step 2 and knows that files of this type should be run using the program /php/php.exe.

5. Start (if it is not running) or restart (if it is running) Apache.

You can start it as a service on Windows NT/2000 by choosing Start➪ Programs➪Apache HTTPD Server➪Control Apache Server and then selecting Start or Restart.
Or you can start it on Windows 98/ME by choosing Start➪Programs➪
Apache Web Server➪Management.

Sometimes restarting Apache is not sufficient; you must stop it first and then start it. In addition, your computer is undoubtedly set up so that Apache will start whenever the computer starts. Therefore, you can shut down and then start your computer to restart Apache.

Configuring IIS

To configure IIS to work with PHP, follow these steps:

1. Enter the IIS Management Console.

You should be able to enter by choosing Start➪Programs➪ Administrative Tools➪Internet Services Manager or Start➪Settings➪ Control Panel ➪Administrative Tools➪Internet Services Manager.

2. Right-click your Web site (such as Default Web Site).

3. Select Properties.

4. Select the Home Directory tab.

5. Click the Configuration button.

6. Choose the App Mappings tab.

7. Click Add.

8. In the Executable box, type the path to the PHP interpreter: for exam­
ple, type c:\php\php-cgi.exe.

9. In the Extension box, type .php.

This will be the extension that is associated with PHP scripts.

10. Select the Script Engine check box.

11. Click OK.

Repeat Steps 6–10 if you want any additional extensions, in addition to
.php, to be processed by PHP, such as .phtml.

Configuring PHP

PHP uses settings in a file named php.ini to control some of its behavior. PHP looks for php.ini when it begins and uses the settings that it finds.
If PHP can’t find the file, it uses a set of default settings.

The default location for the php.ini file is one of the following unless you
change it during installation:

Windows: The system directory, depending on the Windows version, as follows:

• Windows 98/Me/XP: windows

• Windows NT/2000: winnt

Unix/Linux/Mac: /usr/local/lib

If the php.ini file isn’t installed during installation, you need to install it now. A configuration file with default settings, called php.ini-dist, is included in the PHP distribution. Copy this file into the appropriate location, such as the default locations mentioned above, changing its name to php.ini.

If you have a previous version of PHP installed (such as PHP 4.3), make a backup copy of the php.ini file before you overwrite it with the php.ini file for PHP 5. You can then see the settings you are currently using and change the settings in the new php.ini file to match the current settings.

To configure PHP, follow these steps.

1. Open the php.ini file for editing.

2. Activate mysql or mysqli support.

Look for a list of extensions. Find the line for mysql (if you’re using MySQL 4.0 or earlier) or for mysqli (if you’re using MySQL 4.1 or later), as follows:

;extension=php_mysql.dll
;extension=php_mysqli.dll

Notice the semicolon (;) at the beginning of the lines. To activate the extension, remove the semicolon. If the extension line isn’t in your php.ini file, add it.

3. Only if you’re using PHP with the IIS Web server, turn off force redi­
rect. Find the line:

;cgi.force_redirect = 1

You need to remove the semicolon so that the settings is active, and also change the 1 to 0. After the changes, the line looks as follows:

cgi.force_redirect = 0

4. Save the php.ini file.

5. You might need to restart the Apache server before the new settings go into effect.

In general, the remaining default settings allow PHP to run okay, but you might need to edit some of these settings for specific reasons. I discuss settings in the php.ini file throughout the book when I discuss a topic that might require you to change settings. For example, PHP error-handling actions can be changed by settings in the php.ini file. The possible settings for error handling and their effects are discussed in Chapter 4.

0 comments: