I recently installed phpMyAdmin on one of my EC2 machine images. In the course of doing so, I found it necessary to upgrade the PHP interpreter. I did not think that the best way to perform this upgrade was obvious, and so I decided to document my experience.
Motivation
The phpMyAdmin package, as the name suggests, is a tool for MySQL administration, written in PHP. It expects the PHP interpreter which executes its code to contain certain extensions (e.g. zip, mcrypt). Some of these extensions were not present in the PHP interpreter I was using before installing phpMyAdmin. In particular, the following extensions were missing:
- zip
- gd 2.0+
- mcrypt
- mbstring
The Hard Way
My first thought was to recompile PHP. This I did, using the following script, which is compatible with the ami-25b6534c machine image. That image is an Amazon-provisioned i386 Fedora Core 4 build with MySQL, Apache 2.0, and PHP pre-installed. The script builds PHP for Apache 2.0, and removes the pre-existing PHP installation.
# Stop Apache
httpd -k stop
# Remove existing PHP installation
yum -y remove php.i386
# Install PHP prerequisites
yum -y install gcc.i386
yum -y install flex.i386
yum -y install bison.i386
yum -y install httpd-devel.i386
yum -y install libxml2-devel
yum -y install libpng-devel.i386
yum -y install libmcrypt-devel.i386
yum -y install mysql-devel.i386
yum -y install libtool-ltdl-devel.i386
# Retrieve the PHP source code
wget http://us.php.net/get/php-5.2.6.tar.gz/from/us2.php.net/mirror
tar -zxf php-5.2.6.tar.gz
# Create and execute the PHP Makefile
cd php-5.2.6
./configure --enable-zip --enable-mbstring --with-mysql=/usr/include/mysql3/mysql/ --with-mcrypt --with-gd --with-apxs2=/usr/sbin/apxs
make
make install
cd ..
# Place a test file in the html directory
cp test.php /var/www/html/
# Add a configuration file to conf.d
cp php.conf /etc/httpd/conf.d/
# The Makefile places superfluous lines in httpd.conf; remove them
sed -e '/php/ d' -i /etc/httpd/conf/httpd.conf
# Restart Apache
httpd -k start
The script makes reference to two supporting files: An Apache configuration file for the PHP module, and a test PHP script. Here they are, in their entirety.
php.conf
#
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#
LoadModule php5_module modules/libphp5.so
#
# Cause the PHP interpreter to handle files with a .php-style extension.
#
<FilesMatch "\.php[2-5]?$">
SetHandler application/x-httpd-php
</FilesMatch>
#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php
#
# Uncomment the following lines to allow PHP to pretty-print .phps
# files as PHP source code:
#
#<FilesMatch "\.phps$">
# SetHandler application/x-httpd-php-source
#</FilesMatch>
test.php
<!DOCTYPE HTML
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html;charset=utf-8">
<STYLE type="text/css">
/* Basic Style */
html {
background: white;
}
body {
color: black;
margin: 2em 10% 0px;
border: 0px;
padding: 0px;
font-family: "Lucida Grande",Geneva,Verdana,Arial,Helvetica,sans-serif;
font-size: 10pt;
}
</STYLE>
<TITLE>PHP Test</TITLE>
</HEAD>
<BODY>
<H1>PHP Test</H1>
<P>
<B>An example of PHP in action</B>
<BR>
<?php
echo "The current date and time is: ";
echo date("g:i A l, F j Y.");
?>
</P>
<H2>PHP Information</H2>
<P>
<?php phpinfo(); ?>
</P>
</BODY>
</HTML>
This approach works, but I found it unsatisfactory. Many features are omitted from the PHP interpreter, as can be seen from the script’s brief “./configure” command. Although I did not need those features, it seemed that I was going to a lot of trouble to produce something that wasn’t very good.
The Easy Way
After doing a little research, I discovered PHP’s support for dynamic extensions, and that many of those extensions are supported by standard package managers. In particular, I found I could install 3 of the 4 extensions I needed in this way. The only unsupported extension was zip, which I decided I could live without for the time being. Here is the (much simpler) upgrade script.
# Stop Apache
httpd -k stop
# Install PHP extensions
yum -y install php-gd.i386
yum -y install php-mcrypt.i386
yum -y install php-mbstring.i386
# Place a test file in the html directory
cp test.php /var/www/html/
# Restart Apache
httpd -k start
Still Not Happy
After installing the extensions, phpMyAdmin worked fine. The completist in me, however, still wanted to install that last, nagging zip extension. Apparently this extension is supported through the PECL system. Unfortunately, I was not able to install it. What follows is a brief account of my difficulties.
My first problem was that the pecl command wasn’t defined in my machine image. This didn’t bother me too much; PEAR and PECL share a packaging and distribution system, and PEAR seemed to be installed properly.
Unfortunately, the PEAR installed in my machine image didn’t seem to be quite up to date; pear -V
reported a version number of 1.3.5, while the PEAR website indicates that 1.7.2 is the latest version. PEAR also didn’t appear to work properly, as, for instance, the command:
pear search zip
failed with the error message:
HTTP error, got response: HTTP/1.1 410 Gone
So far undaunted, I decided to try installing PEAR according to the instructions:
wget http://pear.php.net/go-pear -O go-pear.php
php go-pear.php
When this failed with a memory allocation error, however, I became dispirited.
In the end, I abandoned the attempt to install the zip extension, as I had no immediate need for it, and did have more pressing issues to resolve. Some stories don’t end completely happily.