Team LiB
Previous Section Next Section

Installing PHP for Linux/Unix

This section shows you how to install PHP on Linux/Unix as a dynamic module for Apache. By building a dynamic rather than a static module, you can upgrade or recompile PHP without having to recompile Apache as well. For example, all you'll be doing in this section is configuring PHP for MySQL support. If you decide you want additional options later in the game, such as image-creation functions or additional encryption functions, you'll only have to change the configuration command for PHP, recompile the module, and restart Apache. No additional changes will be needed for the Apache installation, because one PHP module file just replaces another.

To download the PHP source distribution, visit the Downloads page at the PHP website: http://www.php.net/downloads.php.

  1. From the Complete Source Code section, follow the link for PHP 5.x.x where x.x refers to the version. The current source code version is 5.0.0RC1, and that version number will be used in the following steps. Although your version number (and therefore filename) might vary in the future, the procedure will remain the same, substituting the new name as appropriate.

  2. Once it's downloaded to your system, type cp php-5.0.0RC1.tar.gz/usr/local/src/ and press Enter to copy the PHP source distribution to the /usr/local/src/ directory.

    Note 

    You can put PHP anywhere you want on your file system, such as /usr/local/bin/ or /opt/ or wherever you want to put the file. Just be sure to substitute your path for the path indicated in these directions.

  3. Go to /usr/local/src/ by typing cd /usr/local/src/ and pressing Enter.

  4. Unzip the source file by typing gunzip php-5.0.0RC.tar.gz and pressing Enter.

  5. Extract the files by typing tar -xvf php-5.0.RC1.tar and pressing Enter. This will create a directory structure, and then put you back at the prompt. The parent directory will be /usr/local/src/php-5.0.0b4/.

  6. Enter the parent directory by typing cd php-5.0.RC1 and pressing Enter.

  7. Type the following and press Enter to prepare to build PHP:

    ./configure --prefix=/usr/local/php5 --with-mysql=/usr/local/mysql/
    --with-apxs2=/usr/local/apache2/bin/apxs
    
    
    Note 

    In configuration directives, use your own paths to the MySQL and Apache directories, should they reside elsewhere on your file system.

    The configuration script will run through its process of checking your configuration and creating makefiles, and then will put you back at the prompt.

  8. Type make and press Enter. This second step of the installation process will produce many lines of output on your screen. When it is finished, you will be back at the prompt.

  9. Type make install and press Enter. This final step of the installation process will produce many lines of output on your screen. When it is finished, you will be back at the prompt.

Now, to get a basic version of PHP working with Apache, all you need to do is make a few modifications to the httpd.conf file.

Configuring Apache to Use PHP

The installation process will have placed a module in the proper place within the Apache directory structure. Now you must make some modifications to the httpd.conf file before starting up Apache with PHP enabled.

  1. Open the httpd.conf file in your text editor of choice.

  2. Look for the following line, which will have been inserted into the file by the installation process:

    LoadModule php5_module      modules/libphp5.so
    

    You want this line to be uncommented, so ensure that it is (as shown).

  3. Look for the following lines:

    # AddType allows you to add to or override the MIME configuration
    # file mime.types for specific file types.
    #AddType application/x-tar .tgz
    
  4. Add to these lines the following:

    AddType application/x-httpd-php .phtml .php
    
  5. Save and close the httpd.conf file.

This modification tells Apache that anytime a file with an extension of .php or .phtml is requested, Apache should first run that file through the PHP parser before sending any output to the web browser.

Once these changes have been made to httpd.conf, you're ready to start Apache and test your PHP installation.

Testing the PHP Installation

Now that all of your modifications have been made to the httpd.conf file, you can restart Apache using the method you learned in Chapter 2, "Installing Apache." To test that Apache and PHP are playing nice together, you'll next create a simple PHP script to test your installation. PHP scripts and other files (HTML, images, and so on) should be located in the document root of your web server. For Apache, the document root is the htdocs directory within your Apache installation directory.

  1. Open a new file in your text editor and type the following:

    <? phpinfo(); ?>
    
    
  2. Save the file with the name phpinfo.php.

  3. Place this file in the document root of your web server.

  4. Open your web browser, type http://127.0.0.1/phpinfo.php, and then press Enter.

The output of the phpinfo.php script should be a long page full of system and environment information. This information is very helpful when you're trying to figure out what's available to you.

For more information on configuring and building additional functionality into your PHP installation, see Appendix A, "Additional Configuration Options."

Click To expand

You're now ready to move on to Part II, "The Absolute Basics of Coding in PHP," and learn the fundamentals of the PHP language.


Team LiB
Previous Section Next Section