PHP (I think it's originally stranded for Personal Homepage Processor, but now it's an acronym for PHP: Hypertext Preprocessor) is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.
As usual, we can either install it from an RPM package or from the source code. We will use the later in this lab. To get the code run the following commands
# cd /usr/local/src # wget http://www.php.net/get/php-5.2.5.tar.gz/from/us3.php.net/mirror # tar xvfz php-5.2.5.tar.gz # cd php-5.2.5
Before building the PHP module for Apache, we need properly configure PHP make file. This is an important process, because here you specify what web server PHP is being built for as well as all the libraries you like to use in your version of PHP. We will use the following configuration options:
# ./configure \ --with-apxs2 \ --with-mysql=/usr/local/mysql \ --with-mysql-sock=/tmp/mysql.sock \ --with-mysqli \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-mcrypt
The meaning of these options is:
Unfortunately for me, my machine produced the following error message when I was trying to run the configure command with the above options:
configure: error: mcrypt.h not found. Please reinstall libmcrypt.The reason for this error is simple: I didn't have the required library (libmcrypt) installed. Thus, before installing PHP I need to install all the libraries I'm planning to use with it.
Installation of libmcrypt is quite simple: get the source code package, unpack it, configure, make it, and install it. All of these steps can be performed with the following commands:
# cd /usr/local/src # wget ftp://mcrypt.hellug.gr/pub/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz # tar xvfz libmcrypt-2.5.7.tar.gz # cd libmcrypt-2.5.7 # ./configure # make # make installAfter the installation if complete, do not forget to get back to the PHP directory and run the configure command again. This time, everything should go smoothly. At the end of the long output of the configuration script you should see something like
+--------------------------------------------------------------------+ | 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.Now, to complete the PHP installation simply run
# make # make install
First of all, let us create a simple PHP file and place it in the Apache document root directory. Our file will look like this
<? echo "PHP works!"; ?>Name this file phptest.php and place it in the appropriate directory. (In my case, it's /data/www). After accessing this file through the Apache web server, we should get

In order to do this, open the Apache configuration file httpd.conf (in my case it's located in /usr/local/apache/conf directory) and add the following lines:
LoadModule php5_module modules/libphp5.so AddType application/x-httpd-php .php .phtml AddType application/x-httpd-php-source .phpsThe first line tells the Apache that PHP module (libphp5.so) should be loaded and the other two lines instruct the server that files with extensions php, phtml, and phps should be treated as applications by means of this module. Files with the phps extension will not be executed by PHP instead they will be syntax highlighted and sent to the client. Do not forget to restart the web server after the changes are done. Now, if the server started successfully, the same page http://localhost/phptest.php should look quite different.

In the Apache configuration file we also can modify the DirectoryIndex directive by adding index.php to its current value:
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
This means that if the user requests a directory (not a particular file), then the server will first search for index.html file in this directory and if the there is no such file the server will try to find index.php script.
In order to see how exactly we installed the PHP, we can create another simple file:
<? phpinfo(); ?>Save this file under phpinfo.php in the web directory and access it through the web browser. You should see a big page with all kind of information about PHP interpreter installed on your machine.
Now, let's see if PHP can connect to the MySQL server you previously installed. To do this, create the following PHP script:
<?
$connect = mysql_connect("localhost", "daniel", "dpasswd");
mysql_select_db("SecureDB", $connect);
$query = "select uname, gname from UserInfo";
$res = mysql_query($query, $connect);
if( $res )
while( $row=mysql_fetch_row($res) ){
echo "<li> user: <b>$row[0]</b> group: <b>$row[1]</b>";
}
else
echo mysql_error();
mysql_close($connect);
?>
Note: we work under the assumption that we have already executed the following MySQL commands:
create database SecureDB;
use SecureDB;
create table UserInfo (
uname char(16) primary key,
passwd char(41),
gname char(16) default 'users'
);
insert into UserInfo values ('bob', password('test'), 'IST280');
insert into UserInfo values ('billy', password('test'), 'IST446');
grant ALL on SecureDB.* to daniel@localhost identified by 'dpasswd';
flush privileges;
In order to test the graphical library, create the following PHP file:
<?
header("Content-Type: image/jpeg");
$img = imagecreatetruecolor(200, 200);
$white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
$navy = imagecolorallocate($img, 0x00, 0x00, 0x80);
$red = imagecolorallocate($img, 0xFF, 0x00, 0x00);
imagefilledrectangle($img, 1, 1, 198, 198, $white);
imagefilledarc($img, 100, 100, 180, 180, 0, 300, $navy, IMG_ARC_PIE);
imagefilledarc($img, 110, 95, 180, 180, 300, 360, $red, IMG_ARC_PIE);
imagejpeg($img);
imagedestroy($img);
?>
Please install PHPMyAdmin from Fedora repository and connect to your DB table.
When you are finished with this in-class project, call me up and show me that everything works on your machine.