Build PHP 5.4 on CentOS 6.2

In case you haven’t heard the news, the PHP project released version 5.4.0 last Thursday. Naturally, I decided it was time to install and give it a try. I chose to install to a clean and bare-bones CentOS 6.2 virtual machine using VirtualBox. I did this for two reasons: 1) I wanted a clean environment for the build, and 2) I wanted to play with CentOS. At the time of this writing, there are not yet any official CentOS RPMs for PHP 5.4, so I had to build PHP from source. What follows are the notes I took during the installation and build process. I hope you find them helpful.

Set up a CentOS virtual machine

First of all, go grab the CentOS 6.2 netinstall ISO. Depending on the mirror, it will likely be located somewhere like centos/6.2/isos/x86_64/CentOS-6.2-x86_64-netinstall.iso (note that I’m using the 64-bit version). Following that, create a new basic VirtualBox VM (512 MB RAM, 8 GB HDD, 12 MB video RAM) for your CentOS installation (use “Red Hat (64 bit)” as the Version) and follow these instructions for a CentOS 6.2 netinstall. I chose the 64-bit version, using the netinstall URL of http://mirror.centos.org/centos/6.2/os/x86_64/.

Since the VirtualBox video RAM selected is only 12 MB, it won’t boot into the graphical mode shown in the netinstall installation guide. Don’t worry about this. Also, it will make some assumptions (e.g. it won’t install Gnome or KDE), since you aren’t in graphical mode. What you’ll have at the end is a very bare-bones server installation.

After installation, I like to power down the VM and adjust my settings to set up port forwarding so that I can shell into my VM from my Mac terminal. Feel free to configure things as you like. See the screenshot for an example of forwarding ports in Virtual Box. Once set up, you can boot the VM and then use your favorite local terminal to SSH to the instance:

ssh -p 2222 root@localhost

You’ll probably want to create a user for yourself, so that you’re not using root all the time, but since this is a VM on your local machine, it’s not a huge deal. If this was a box out in the open, I’d recommend locking it down and creating a user with much more restricted permissions.

Screenshot of VirtualBox port forwarding tables

Install packages needed for PHP

Once logged in to the VM, you’ll need to install some basic build stuff, like gcc, etc.

yum install man wget
yum groupinstall "Development Tools"

Next, update iptables to allow connections to the VM over ports 80 and 8000.

sed -i '/22/ i -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT' /etc/sysconfig/iptables
sed -i '/22/ i -A INPUT -m state --state NEW -m tcp -p tcp --dport 8000 -j ACCEPT' /etc/sysconfig/iptables
/etc/init.d/iptables restart

For the PHP configuration that I’m using, install the following packages. There will be a bunch of dependencies it will ask you to install. Just say yes to them all.

yum install \
libxml2-devel \
httpd-devel \
libXpm-devel \
gmp-devel \
libicu-devel \
t1lib-devel \
aspell-devel \
openssl-devel \
bzip2-devel \
libcurl-devel \
libjpeg-devel \
libvpx-devel \
libpng-devel \
freetype-devel \
readline-devel \
libtidy-devel \
libxslt-devel

For some reason, libmcrypt isn’t available in the main CentOS repositories, so add the RPMForge repo to install it.

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
rpm -K rpmforge-release-0.5.2-2.el6.rf.*.rpm # Verifies the package
rpm -i rpmforge-release-0.5.2-2.el6.rf.*.rpm
yum install libmcrypt-devel

Now, the environment is all ready for building PHP 5.4!

Build PHP 5.4.0

All that’s left is to get the PHP 5.4.0 release package and build it. Choose a mirror closest to you and run the following commands to configure, make, and install PHP.

wget http://www.php.net/get/php-5.4.0.tar.bz2/from/this/mirror
tar jxf php-5.4.0.tar.bz2
cd php-5.4.0/

What you want to enable and build into your PHP installation will vary, but here’s the configure line that I used for this build:

./configure \
--with-libdir=lib64 \
--prefix=/usr/local \
--with-layout=PHP \
--with-pear \
--with-apxs2 \
--enable-calendar \
--enable-bcmath \
--with-gmp \
--enable-exif \
--with-mcrypt \
--with-mhash \
--with-zlib \
--with-bz2 \
--enable-zip \
--enable-ftp \
--enable-mbstring \
--with-iconv \
--enable-intl \
--with-icu-dir=/usr \
--with-gettext \
--with-pspell \
--enable-sockets \
--with-openssl \
--with-curl \
--with-curlwrappers \
--with-gd \
--enable-gd-native-ttf \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-zlib-dir=/usr \
--with-xpm-dir=/usr \
--with-vpx-dir=/usr \
--with-freetype-dir=/usr \
--with-t1lib=/usr \
--with-libxml-dir=/usr \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-soap \
--with-xmlrpc \
--with-xsl \
--with-tidy=/usr \
--with-readline \
--enable-pcntl \
--enable-sysvshm \
--enable-sysvmsg \
--enable-shmop

Assuming the configure script ran perfectly, you’re ready to make and install PHP.

make && make install

Now, you’ve got a fully-functioning build of PHP 5.4.0 on CentOS 6.2!

Trying out the build

Now, that PHP is installed, it’s time to try out a few things. Create a simple phpinfo() script, make sure Apache knows to parse .php files as PHP, and start Apache.

echo "<?php phpinfo();" > /var/www/html/phpinfo.php
echo -e "<FilesMatch \\.php$>\nSetHandler application/x-httpd-php\n</FilesMatch>" > /etc/httpd/conf.d/php.conf
/etc/init.d/httpd start

Now, assuming your VM port forwarding is set up correctly, from your local machine visit http://localhost:8080/phpinfo.php. You should see the output of phpinfo().

One cool new PHP 5.4 feature is the built-in web server for development testing. You can try it out with this build. The following commands tell PHP to run the built-in web server using the current directory as the web root, while listening on port 8000 to all interfaces (not recommended on a public machine, but necessary for the request to the VM to work).

cd /var/www/html
php -S 0.0.0.0:8000

From your local machine, visit http://localhost:8000/phpinfo.php. Note that the Server API line will say “Built-in HTTP server.” Of course, we’ve also set up Apache, so using the built-in HTTP server may not be of much value now, but in the future, if developing locally, the built-in web server can be a handy way to quickly try out things that typically need a web server.

Wrapping up

There you have it, and it only takes about an hour to work through these installation and build steps, most of which is taken up by waiting on things to finish installing, configuring, or building. I hope these steps have been helpful. Feel free to let me know of your own experiences installing PHP 5.4 and leave any tips or tricks in the comments.

Thanks for reading!