Creating LAMP VM – PHP Preparation

This is part of a series of posts on creating your own LAMP virtual machine for development purposes. Here we will setup our environment for installing PHP.

Estimated time (this post only): 15 minutes

FastCGI/FPM vs. Apache Module

In order to test multiple versions of PHP we have a choice to make:

  1. Install multiple copies of Apache, each with their own version of PHP module, since only one PHP module can be installed per Apache instance.
  2. Install one copy of Apache with all versions of PHP as FastCGI/FPM (more on FPM in the next post).
  3. Install one copy of Apache with one version of PHP as a module and other versions as FastCGI/FPM.

This is far from a complete list, just the most common scenarios. There are a ton of pros and cons for all these methods. Many shared hosts still use the method where their more recent PHP version (5.3 or 5.4 usually) is installed as an Apache module and an older version (5.2 or 5.3) is installed as a FastCGI application. This is partly because PHP directives can be set in .htaccess only if PHP is installed as an Apache module (don’t worry if you don’t know what I’m talking about; more on this later). If this were a production machine, I would recommend installing all PHP versions as FastCGI/FPM applications (or through the suPHP handler for better security), but because this is for testing, let’s go with option 3 above.


Download PHP Source

Let’s get the different versions of PHP we want to test. On your host system, visit the PHP Download Site and download the versions you want to your shared directory (see the main PHP site for links to beta versions). Find versions which have reached end-of-life (5.3 and older, and 5.4 as of next month) by clicking the “Old Archives” link on the right side of the page. You want the .tar.gz source for each.

As of this writing the most common versions of PHP are 5.3 and 5.4, followed by 5.2 and 5.5. 5.6 has about 2% market share and virtually nobody is using 7.0 yet. Since we are setting this VM up for development purposes, I want to test 5.3+, and maybe even 7.0 which is currently in beta stage. It is amazing that 5.2, which reached end-of-life in January 2011, still is used by over 15% of all websites running PHP 5 (meaning 12.5% of all websites in the world since PHP 5 has over 80% market share overall). Even 5.3 reached EOL last year yet remains the #1 version used in the world.

I am going to forgo installation of PHP 5.2 in this guide. Why? Because, as I said, it hasn’t been supported in 4.5 years! Plus, even the servers who do still use it almost always have 5.3/5.4 as their default. And above all, PHP 5.2 will not run under current versions of Apache. In short, if you are developing for PHP 5.2 you are asking for trouble. If for some reason you are working on a web application that requires PHP 5.2, there are plenty of 5+ year-old guides out there on how to do so.

Back to your VM. Unpack the downloads now. Following are the commands I had to use to do so. Yours will probably differ, especially if downloading 7.0.

cd /usr/local/src
sudo tar -zxvf /media/sf_Temp/php-5.3.29.tar.gz
sudo tar -zxvf /media/sf_Temp/php-5.4.43.tar.gz
sudo tar -zxvf /media/sf_Temp/php-5.5.27.tar.gz
sudo tar -zxvf /media/sf_Temp/php-5.6.11.tar.gz
sudo tar -zxvf /media/sf_Temp/php-7.0.0beta2.tar.gz

Prerequisites For Building PHP

The PHP Group maintains extensive documentation on their website. You can read through it for more details, but I will cover everything you should need to know here.

The prerequisites for some of these versions of PHP differ. For example, if you use autoconf >= 2.59 with PHP 5.2/5.3, the build will fail, but you must use a newer version of autoconf for PHP 5.4 and newer. That’s okay. Just follow these steps and you should be fine.

One of the possible prerequisites, re2c, is available in the EPEL repository only for CentOS 6 and older. I did a lot of searching, and only found it on some Russian Fedora repo. Personally, I would rather install it from SourceForge. Depending on your build options, you may not even need it, but just in case, here is how to install it on CentOS 7 entirely from the command line (the directory name after extracting the archive may differ—check it with “ls -lh”):

cd /usr/local/src
sudo wget https://sourceforge.net/projects/re2c/files/latest/download
sudo tar -zxvf download
sudo rm download
cd re2c-0.14.3
sudo ./configure
sudo make
sudo make install

You also need the following:

sudo yum install libxml2-devel autoconf213 autoconf automake libtool flex bison patch

A fix that may be needed for 64-bit systems:

sudo ln -s /usr/include /opt/include
sudo ln -s /usr/lib64 /opt/lib

Any PHP version you install as FPM (not as an Apache module) needs its own user, so do:

sudo useradd -rm php

Prerequisites For Extensions

If you don’t intend to install all the extensions below, you can skip the appropriate sections, though there is no harm in doing all of this.

BZ2 Extension

sudo yum install bzip2-devel

cURL Extension

This has been moved to PECL as of PHP 5.5, but for prior versions you will need to do (cURL is probably already installed, but do this anyway):

sudo yum install curl libcurl libcurl-devel

GD Extension

I had to do the following to get the GD extension setup the way I wanted:

sudo yum install libjpeg libjpeg-devel libpng libpng-devel libXpm libXpm-devel freetype freetype-devel

IMAP Extension

For the IMAP Extension you will need to do the following. Notice that the name of the directory for the extracted files may be different in your case (for example: “imap-2007e”):

cd /usr/local/src
sudo yum install libc-client-devel pam-devel
sudo ln -s /usr/lib64/openssl/engines/ /usr/local/ssl
sudo ln -s /usr/include/ /usr/local/ssl/include
sudo wget ftp://ftp.cac.washington.edu/imap/imap.tar.gz
sudo tar -zxvf imap.tar.gz
sudo rm imap.tar.gz
cd imap-2007f
sudo make lnp SSLTYPE=unix EXTRACFLAGS=-fPIC
sudo mkdir lib
sudo mkdir include
sudo cp c-client/*.c lib/
sudo cp c-client/*.h include/
sudo cp c-client/c-client.a lib/libc-client.a
cd ..

MCrypt Extension

Assuming you setup the EPEL repository earlier:

sudo yum install libmcrypt libmcrypt-devel

MySQLi Extension

I needed the MySQLi extension, but received the following error when compiling it for some PHP versions:

configure: error: wrong mysql library version or lib not found. Check config.log for more information.

Here are the steps I used to get it to work. It is much simpler than most of the “fixes” I found on Google, but if it doesn’t work for you then you may want to do a search. Alter symlink as needed for whatever is your preferred version of MySQL/MariaDB (MySQL 5.6/MariaDB 10.0 will both work well for this as they are the latest GA versions):

sudo yum install libtool-ltdl-devel
sudo ln -s /opt/mysql56/lib/libmysqlclient.so /usr/lib64/mysqld

Pspell Extension

sudo yum install aspell aspell-devel

Tidy Extension

sudo yum install libtidy libtidy-devel

XSL Extension

sudo yum install libxslt libxslt-devel

PDO IBM

If you installed DB2, you are going to need to build PHP with PDO IBM. This isn’t part of the base PHP archive, so you need to do the following:

cd /usr/local/src
sudo wget http://pecl.php.net/get/PDO_IBM
sudo gzip -d < PDO_IBM | sudo tar -xvf -
sudo mv PDO_IBM-* pdo_ibm
sudo rm PDO_IBM
sudo rm package.xml
sudo cp -r pdo_ibm php-5.3.29/ext
sudo cp -r pdo_ibm php-5.4.43/ext
sudo cp -r pdo_ibm php-5.5.27/ext
sudo cp -r pdo_ibm php-5.6.11/ext
sudo cp -r pdo_ibm php-7.0.0beta2/ext
export DB2DIR=/opt/ibm/db2
sed -i '$ a\\nexport DB2DIR=/opt/ibm/db2' ~/.bashrc

That last command will ensure the DB2DIR variable is set if you need to restart or log out. It isn’t necessary otherwise, but no harm including it.

PDO DBLIB

If you installed Sybase earlier you need to install FreeTDS in order to build the PDO DBLIB extension with PHP. The simplest way to install is from the EPEL repository we setup earlier (note that as of this writing EPEL has the 0.91 version from 2011 rather than the more recent 0.95 but 0.91 is the current stable version anyway):

sudo yum install freetds freetds-devel

PDO PGSQL

If you installed PostgreSQL you need to do this to get PDO PGSQL to compile properly (change directory name if needed—check with “ls -lh /usr/”). The second line will set the path permanently (in case you need to restart or log back in):

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/pgsql-9.4/lib
sed -i '$ a\\nexport LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/pgsql-9.4/lib' ~/.bashrc

PDO INFORMIX

If you wish to build PHP with PDO INFORMIX (which cannot coexist with PDO IBM), you will need to do the following. This is not required since PDO IBM can connect to Informix 11.10 or later. The process is similar to PDO IBM:

cd /usr/local/src
sudo wget http://pecl.php.net/get/PDO_INFORMIX
sudo gzip -d < PDO_INFORMIX | sudo tar -xvf -
sudo mv PDO_INFORMIX-* pdo_informix
sudo rm PDO_INFORMIX
sudo rm package.xml
sudo cp -r pdo_informix php-5.3.29/ext
sudo cp -r pdo_informix php-5.4.43/ext
sudo cp -r pdo_informix php-5.5.27/ext
sudo cp -r pdo_informix php-5.6.11/ext
sudo cp -r pdo_informix php-7.0.0beta2/ext
export INFORMIXDIR=/opt/informix
sudo ln -s $INFORMIXDIR/lib $INFORMIXDIR/lib64
sed -i '$ a\\nexport INFORMIXDIR=/opt/informix' ~/.bashrc

Leave a Reply