Installing and Configuring PHP

Chapter 1 - What is PHP?
byGareth Downes-Powellet al.
Wrox Press 2003

Installing PHP might sound like a challenging task, especially if you have already looked inside the archive you downloaded from the PHP web site. Whilst the methods for installing PHP on both Windows and Linux machines are entirely different, they are both relatively easy.

Installation Shortcuts

Whilst knowing how to install PHP individually can be widely considered a good thing, it should not be forgotten that there are always alternative ways to achieve the same goal. Installing PHP is no different, and it is especially useful to know of the quicker alternatives, if you are pressed for time.

PHP itself is available as two different downloads from the PHP web site, www.php.net. There is a Zip file distribution, which requires a little bit of manual tweaking, and there is also a self-installing version of PHP available that uses the Windows Installer. Neither method is entirely foolproof, but in general the Windows Installer version is quicker with certain configurations.

There are also a number of alternative, combination distributions available that not only install PHP, but also install MySQL and the Apache Web Server too. This is the perfect option, should you have no local web server to use for development purposes, or if you wish to use a web server other than Internet Information Server. There are two main distributions that fall into this category: PHPTriad and phpdev. Unfortunately PHPTriad has recently been discontinued, though its files are still available from http://www.sourceforge.net/projects/phptriad. phpdev is still under active development and can be obtained from http://www.firepages.com.au.

Installing PHP On Windows

The process for installing PHP on Windows is relatively simple, as you don't need to worry about compiling sourcecode. You can either use the Windows Installer, or follow these instructions. The process involves extracting the files from the archive you downloaded from the PHP web site, and moving the files and folders it contains to specific locations.

  1. Locate the Zip file you downloaded and extract the contents to c:\php

  2. Using Windows Explorer, move to the c:\php directory

  3. Copy the file php.ini-dist to your Windows directory and rename it to php.ini.

    The Windows directory is slightly different dependent on your version of Windows. Typically, for Windows 95, 98, ME, & XP users this would be c: \windows, for NT & 2000 users this would be c: \winnt.

  4. Copy the php4ts.dll file from your c: \php directory to your system directory (typically c:\windows\system for Windows 95, 98, & ME users, c:\windows\system32 for Windows XP users, or c:\winnt\system32 for Windows NT & 2000 users.

  5. In some, but not all, distributions of PHP you might also find a file called Msvcrt.dll. If you find it there, you also need to copy it to your system directory, if it isn't already present.

  6. Certain library files are required by different parts of PHP. For this reason, you should copy the contents of your c:\php\dlls folder also to your system directory.

  7. Edit the php.ini file in your Windows directory using your favorite text editor (Notepad is perfect for this).

  8. Find the line extension_dir = ./ and change it to read extension_dir = c:\php\extensions. Find the line: session.save_path = and alter it to: session_save_path = c:\WINNT\Temp\ (or a suitable temporary directory).

  9. Save the file, and close the text editor.

Now that the process of installing and configuring PHP to work on your Windows machine is complete, we need to tell the Web Server that you have PHP installed. This process is slightly different for Apache and IIS.

Configuring Apache 1.3.x

Historically there have been two ways to configure PHP to work with Apache. You could configure PHP to work as an Apache module, or you could configure PHP to work as a CGI process. Thanks to a security advisory report at http://www.cert.org/advisories/CA-1996-11.html, the CGI method is now not recommended under ANY circumstances. This is due to the fact that when PHP is installed as a CGI process, a hacker of proficient skill level can, under certain circumstances, access the PHP CGI Program and use it to run system-level commands on your web server. These commands can be anything from transferring the content of your database to a location of their choice, or even accessing your files with the intention of erasing or replacing them. This warning has been officially made as the risks of running any interpreter as a CGI program are too high, and cannot be ignored.

For this reason, we will configure PHP to run as an Apache module.

  1. Open the Apache configuration file httpd.conf in your favorite text editor (again Notepad is fine for this purpose). This is a well-commented file, broken up into several independent sections. Each section relates to a specific part of the Apache Web Server configuration, and details everything from the address of the server and the administrator's e-mail address to the file types that Apache should recognize.

  2. Within httpd.conf look for any lines that say "LoadModule". It is important that you do not change these lines, or their order, but the following line should be appended to this list.

     LoadModule php4_module c:/php/sapi/php4apache.dll 

  3. Now, within the same file, look further down and find the lines that begin "AddModule"; append the following two lines to the list.

     AddModule mod_php4.c AddType application/x-httpd-php .php 

  4. Save the file, and close your text editor.

All that now remains is to shut down and restart the Apache service (Windows NT, 2000, and XP users), to do this open a command prompt window and type "net stop apache" to stop the Apache Service, and "net start apache" to restart the service.

Configuring Internet Information Server (v4 or greater)

Like Apache, there are two ways to configure PHP to work with IIS. You can configure PHP to work as an ISAPI module, or you can configure PHP to work as a CGI process. As the ISAPI module is experimental, and for the moment considered unstable, it is not recommended that you use it in a production environment. For this reason, we will only look at installing PHP as a CGI process with IIS.

  1. Start the Microsoft Management Console, also known as Internet Services Manager. Under Windows 2000 and Windows XP Professional this can be found in Control Panel - > Administrative Tools.

  2. In Internet Services Manager, browse to Internet Services Manager -> Computer Name -> Web Sites. Right-click on Default Web Site and select Properties.

  3. Select the Home Directory tab, and click on the Configuration button.

  4. Under the Mappings tab, click on the Add button. In the new window, browse to the location of the PHP executable, in your c: \php folder.

  5. In Extension type .php and ensure that the Script engine and Check that file exists checkboxes are checked.

  6. Click OK, and you should return to the Management Console.

All that now remains is to shut down and restart the IIS service for the changes to take effect. To do this open a command prompt window and type net stop iisadmin to stop IIS completely. Typing net start w3svc restarts IIS with your new configuration options.

It is worth mentioning here that there is one final task for Windows NT, 2000, and XP users who use NTFS as their file system. In order for the above configuration to work you should additionally follow this procedure:

  1. Using Windows Explorer, view the contents of your C: drive.

  2. Right-click on your c:\php folder and select Properties.

  3. Click on the Security tab, and then click the Add button.

  4. A new window will open, entitled Select Users Computers or Groups. You should now scroll through the list of users and find an entry that begins I_USER_; the remaining part of this user's name should be the name of your computer. Click on this user once to highlight it.

  5. Click the Add button and click OK to close this window.

  6. Click OK once more to complete this modification.

Completing this procedure allows the web server to access this directory when it needs to use the PHP program to process your pages.

Installing PHP On Your Linux Web Server

If you're using Linux on your development server, then you need to follow these instructions. They are borrowed, in part, from the PHP & Linux Quick Install documentation, contained within the PHP sourcecode distribution. More detailed and up-to-date information on installing PHP on Linux can be found at the PHP Online Manual at http://www.php.net/manual/en/install.apache.php.

As with Windows, there are two ways in which PHP can be installed on your Apache web server under Linux. The first method is using a Dynamic Shared Object (DSO) much like the Windows Apache module, or a Static Module. DSO modules are far better in this respect, as they do not rely on you also having the sourcecode for Apache because they compile independently. This also means that should you wish to upgrade your version of PHP at any time, you don't have to also recompile Apache. For this reason, we will concentrate solely on compiling PHP as a DSO module.

  1. Before you begin the compilation process, you must ensure that you are logged in as the root user.

  2. Open a console window, or at the command line, browse to the location where you have stored your downloaded tar.gz archive.

  3. To extract the archive, type: gunzip -c php-4.x.x.tar.gz | tar xf -(Note: replace php-4.x.x for the actual name of the file you downloaded. For example, PHP v4.2.0 is called php-4.2.0.tar.gz).

  4. Type: cd php-4.x.x to enter the PHP directory.

  5. To begin the precompilation process type: ./configure -with-mysql -with-apxs.

  6. Once step 5 has finished begin the compilation process by typing: make.

  7. Step 6 can take a little while to complete, once it has completed type: make install to install PHP onto your Linux machine.

  8. Find the file php.ini-dist and copy it to /usr/local/lib/php.ini.

The final stage in installing PHP on your Linux machine is to configure your web server appropriately. As most Linux distributions come with Apache pre-installed, all you need to do to is edit your httpd.conf, and add a line that looks like this:-

 AddType application/x-httpd-php php 

Once this has been done, restart the Apache service, by typing: apachectl restart in a console, log in as the root user, and your server is ready to process PHP files.

We previously mentioned that OSX 10.1 comes with PHP installed by default. However, it should be noted that whilst PHP is installed, it isn't turned on by default.

Activating the PHP Module in OSX

Thankfully the OSX installer has handled the hard part of installing PHP to work with Apache under OSX. All that remains is to turn on the PHP module, and this is how we do it:

  1. Open a terminal window

  2. Navigate to the Apache directory by typing: cd /etc/httpd

  3. Activate the PHP module itself by typing: sudo apxs -e -a -n php4 libexec/httpd/libphp4.so

  4. That done, we need to inform Apache of the change, and configure it accordingly with the line:

     sudo perl -p -i.bak -e "s%#(AddType \S+-php[ -])%$l%i" httpd.conf 

    Users of Jaguar (OS 10.2) should use: echo 'echo "AddType application/x-httpd-php .php" >> /etc/httpd/httpd.conf' | sudo sh -s

Now that this is done, if you are using a Linux or Mac OSX Server you should open a shell, or terminal window, and enter sudo apachectl graceful to restart Apache.

Testing Your PHP Installation

We have now covered the installation and configuration of PHP on your machine, and all that remains is to check that the installation works. Thankfully there is a very easy way to do this. Create a new file in Notepad (or any other text editor), and enter:

 <?php phpinfo(); ?> 

Save the file as test.php and upload it to your web server. Once there browse the file on your web server and you should see something like this:

click to expand

Troubleshooting PHP Configuration Issues

Nothing in life always goes quite to plan, as we all know. This is often the case with software installation. Here we will look at a list of possible issues that you might come across and their relevant solutions.

Because PHP is ever evolving, there are from time-to-time, version-specific issues that arise. It would be impossible to cover those here, due to the amount of time that PHP has been available. However, there are a number of issues that do occur, that are common over the various versions of PHP. These are detailed within the PHP install guide, that formed part of your download, and some of those are also included here for reference.

When Testing a PHP Page in Your Browser You Get a Blank Page

To determine whether or not any data has been sent to the browser view the source code of this blank page. If this reveals a sourcecode listing that begins with <?php or <?, then it means that your web server isn't properly processing the web page by sending it to PHP for interpretation. In this situation, you should check your PHP installation against the installation instructions given previously.

When Testing a PHP Page in Your Browser You Encounter a 500 Error

A 500 error occurs when something goes wrong with the server during processing. There is a simple diagnostic check, which might give further information.

Open a command prompt, and navigate to your php directory.

Under Windows type php.exe -i or under Linux type php -i. If the resulting output is a series of HTML tags then it is likely that this is a PHP configuration issue. As such, go over the configuration steps again for your web server to determine where the problem is.

If no HTML tags are returned, then you will receive some error messages. It might be clear from these messages what the problem is, enabling you to find a simple solution. If, however, this is not the case, you will find more information at the PHP Frequently Asked Questions (FAQ) page and discussion boards at http://www.php.net/faq.

Another common cause of this problem can be with Windows NT, 2000, and XP users who have not correctly assigned permissions to IUSR_ to access the PHP executable. This process has previously been detailed in the Configuring Internet Information Server section.

The Specified CGI Application Misbehaved by Not Returning a Complete Set of HTTP Headers Visible in the Browser.

Follow the procedure given in the 500 error section, above.

Other Errors

Thankfully, while PHP is free to use, it is also very well supported. If you encounter other errors or problems, then there are two really useful resources to visit. First of all, there is the extensive PHP FAQ, as previously mentioned. Another great resource for installation issues is the dedicated installation mailing list. To subscribe to this mailing list, send an empty e-mail to <php-install-subscribe@lists.php.net>.



Dreamweaver MX PHP Web Development
Dreamweaver Mx: Advanced Php Web Development
ISBN: 1904151191
EAN: 2147483647
Year: 2001
Pages: 88

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net