Installing PHP5 with Apache 2

This document describes how to install the version of the PHP general-purpose scripting language that is located on the class website, version 5.4.5. You may obtain this version either by clicking here or by going to the class web page and clicking on Download Course Software. If you choose to use this version of PHP, you may follow the detailed instructions below. Otherwise you may download another version from the PHP web site, but in that case you will have to follow the installation instructions included with the software. If you need assistance you should read the documentation that comes with the PHP package or go to the online documentation at http://www.php.net/manual/en/.

The following procedure describes how to build and install the PHP 5.4.5 binary loadable module for Apache 2. Once again, this procedure works ONLY FOR the version of PHP located on the course web site, namely 5.4.5.


Download and Unpack PHP 5 Package

Step 1: Download the 5.4.5 source version of the PHP language from the class website by clicking here or go to the course home page and click on Download Course Software. Upload the file to your class user account home directory.

Assume your user account home directory is /home/scf-22/myname/, and you want to unpack the apache files there.

Step 2: unpack the tar package by running "tar xvf php-5.4.5.tar"

Step 3: after several minutes, a directory tree /home/scf-22/myname/php-5.4.5 will be created.

Step 4: you should delete php-5.4.5.tar which is no longer needed by running "rm php-5.4.5.tar"


Install PHP 5.4.5 Binary Loadable Module

Connect to the PHP source distribution directory just created and do a "pwd" command to get the full path to the PHPO directory, as in:

      cs-server.usc.edu(2): cd php-5.4.5
      cs-server.usc.edu(3): pwd
      /home/scf-22/csci571b/php-5.4.5
      cs-server.usc.edu(4): 

Part 1: Source creation and configuration.

Then type the following command to create the source files and configure them to your "hardware" environment:

./configure --with-apxs2=/absolute/path/to/apache/directory/bin/apxs --prefix=/absolute/path/to/php5/directory --without-pear --disable-phar

 

Note: The directory /absolute/path/to/php5/directory/ SHOULD BE a new directory, e.g. /home/scf-22/myname/php5, so that you can erase the distribution directory /home/scf-22/myname/php-5.4.5 at the end of this installation process. Therefore we strongly recommend that you use a new directory for this installation.


Note: The directory /absolute/path/to/apache/directory/ SHOULD BE the directory where you have installed Apache 2, e.g. /home/scf-22/myname/apache2, as described in the Apache 2 installation.


During installation, which could last as long as 10 minutes, you will see hundreds of output lines. Many will look like checking for _name_ yes/no. At the beginning and end of the source code creation, you should see output similar to the one below (most of the "checking" and "creating" lines have been omitted):

      cs-server.usc.edu(5): ./configure --with-apxs2=/home/scf-22/csci571b/apache2/bin/apxs --prefix=/home/scf-22/csci571b/php5 --without-pear --disable-phar 
      checking for ... 
      
      Generating files
      configure: creating ./config.status
      creating main/internal_functions.c
      creating main/internal_functions_cli.c
      +--------------------------------------------------------------------+
      | License:                                                           |
      | This software is subject to the PHP License, available in this     |
      | distribution in the file LICENSE.  By continuing this installation |
      | process, you are bound by the terms of this license agreement.     |
      | If you do not agree with the terms of this license, you must abort |
      | the installation process at this point.                            |
      +--------------------------------------------------------------------+

      Thank you for using PHP.

      config.status: creating php5.spec
      config.status: creating main/build-defs.h
      config.status: creating scripts/phpize
      config.status: creating scripts/man1/phpize.1
      config.status: creating scripts/php-config
      config.status: creating scripts/man1/php-config.1
      config.status: creating sapi/cli/php.1
      config.status: creating main/php_config.h
      config.status: executing default commands
      cs-server.usc.edu(6): 
Part 2: Source compilation and linking.

Do not change directory. Then type the following command to compile all source files and create (i.e. "make" in UNIX lingo) the PHP5 binary loadable module:

make

The compilation will take a long time (as long a 20 minutes).

Part 3: Library Linking and installation.

Once compilation is finished, you can install the PHP5 binary loadable module using:

make install

This phase will last about 3 minutes. You should see output similar to the one below:

      cs-server.usc.edu(7): make install
      Installing PHP SAPI module:       apache2handler
      /home/scf-22/csci571b/apache2/build/instdso.sh SH_LIBTOOL='/home/scf-22/csci571b/apache2/build/libtool' libphp5.la /home/scf-22/csci571b/apache2/modules
      /home/scf-22/csci571b/apache2/build/libtool --mode=install cp libphp5.la /home/scf-22/csci571b/apache2/modules/
      cp .libs/libphp5.so /home/scf-22/csci571b/apache2/modules/libphp5.so
      chmod +x /home/scf-22/csci571b/apache2/modules/libphp5.so
      cp .libs/libphp5.lai /home/scf-22/csci571b/apache2/modules/libphp5.la
      libtool: install: warning: remember to run `libtool --finish /home/scf-22/csci571b/php-5.4.5/libs'
      chmod 755 /home/scf-22/csci571b/apache2/modules/libphp5.so
      [activating module `php5' in /home/scf-22/csci571b/apache2/conf/httpd.conf]
      Installing PHP CLI binary:        /home/scf-22/csci571b/php5/bin/
      Installing PHP CLI man page:      /home/scf-22/csci571b/php5/php/man/man1/
      Installing PHP CGI binary:        /home/scf-22/csci571b/php5/bin/
      Installing build environment:     /home/scf-22/csci571b/php5/lib/php/build/
      Installing header files:          /home/scf-22/csci571b/php5/include/php/
      Installing helper programs:       /home/scf-22/csci571b/php5/bin/
        program: phpize
        program: php-config
      Installing man pages:             /home/scf-22/csci571b/php5/php/man/man1/
        page: phpize.1
        page: php-config.1
      Installing PDO headers:          /home/scf-22/csci571b/php5/include/php/ext/pdo/
      cs-server.usc.edu(8):

If all went well, you should see the PHP binary loadable module, libphp5.so, in the Apache 2 modules directory, /absolute/path/to/apache/directory/modules, e.g., /home/scf-22/myname/apache2/modules.

If you have reached this point successfully, you are now ready to customize Apache to use your PHP module. Congratulations!

 


Configure/Customize PHP and Apache

Part 1: Copy php.ini

PHP uses one configuration file, named PHP.ini. We will need to copy this file from the source distribution.

Assuming you have followed the instructions above, the PHP.INI sample files are contained in the folder /home/scf-22/myname/php-5.4.5. One of these sample INI files, php.ini-development, needs to be copied to your PHP "library" directory, e.g., /home/scf-22/csci571b/php5/lib/php.ini, and renamed php.ini. Change to that directory and copy the file:

      cs-server.usc.edu(8): cd /home/scf-22/csci571b/php-5.4.5  
      cs-server.usc.edu(9): cp php.ini-development /home/scf-22/csci571b/php5/lib/php.ini
Part 2: Edit Apache main configuration file, httpd.conf

Apache 2 uses several configuration files. We will need to change one (1) of these configuration files before starting the Apache 2 web server with the PHP loadable module.

Assuming you have followed the instructions in the Apache 2 Installation, the Apache configuration files are contained in the folder /home/scf-22/myname/apache2/conf and its subfolders. Change to that directory:

      cs-server.usc.edu(9): cd /home/scf-22/csci571b/apache2/conf   

Open the file httpd.conf using a text editor, and add the following lines at the end of the file:

      <FilesMatch "\.ph(p[2-6]?|tml)$">
          SetHandler application/x-httpd-php
      </FilesMatch>

You have now completed all the steps to configure, create, install and customize the PHP 5 loadable module for Apache 2. Congratulations!


Stopping and Restarting Apache with PHP

Students MUST run their server on cs-server.usc.edu. If Apache is already running, you will need to stop it. To stop the server, change to the Apache 2 bin directory (e.g., /home/scf-22/csci571b/apache2/bin, in the example above) and type apachectl stop. The following output should be displayed:

      cs-server.usc.edu(10): apachectl stop
      cs-server.usc.edu(11): apachectl start 

It is a good practice to check the error log file, e.g. /home/scf-22/csci571b/apache2/logs/error_log, to ensure your server is properly running. You may see something similar to this:

[Thu Jul 26 11:07:42 2012] [notice] Apache/2.2.22 (Unix) configured -- resuming normal operations

You can also run a PHP "sanity" check to ensure that the PHP module is loaded:

      cs-server.usc.edu(11): /home/scf-22/csci571b/apache2/bin/httpd  -M
      Loaded Modules:
       core_module (static)
       ...
        php5_module (shared)    <---------
      Syntax OK
      cs-server.usc.edu(12):

If the PHP module is loaded successfully, you will see it listed.


Testing Your PHP module

Change to the Apache 2 htdocs directory, e.g., /home/scf-22/csci571b/apache2/htdocs, and create a new file, named phpinfo.php, containing this code:

	<html>
	<head>
		<title>PHP Test</title>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	</head>
	<body>
		<h1>PHP Test</h1>
		<p>
		<b>An Example of PHP in Action</b><be />
          	<?php date_default_timezone_set('America/Los_Angeles');?>
			<?php echo "The Current Date and Time is: <br>";
			echo date("g:i A l, F j Y.");?>
		</p>

		<h2>PHP Information</h2>
		<p>
			<?php phpinfo(); ?>
		</p>
	</body>
	</html>

from a web browser enter the following URL (substituting your Apache port number):

	http://cs-server.usc.edu:33559/phpinfo.php

You should see the PHP 5 local time and information page below:

You can scroll down and see several pages of listed settings and environment variables for both Apache and PHP.

If you installed PHP in a directory other than the source distribution, as we recommended, you can now safely delete the distribution directory, its sub-directories and all its files with:

      cs-server.usc.edu(12): /usr/bin/rm ~/php-5.4.5

Useful Links for Compiling, Installing and Configuring PHP

PHP Installation - http://www.php.net/manual/en/install.unix.apache2.php
PHP Documentation - hhttp://www.php.net/manual/