There’s always a lag behind new releases of PHP and releases of packages for operating systems such as Ubuntu. This lag time means that you could be kept from upgrading to the latest and greatest PHP for a year or more, unless you use an outside repository like Dotdeb. Of course, even when using one of these outside sources, you may still find a lag in the security and bugfixes made available to you.
Instead, I roll my own version of PHP. It’s simple and easy to do, and something that any developer can do. Here’s my instructions for doing so on a fresh Ubuntu installation.
Why you should consider “rolling your own” PHP installation
There are a ton of great PHP packages out there. Some people use the default packages built into the package repositories; others use Dotdeb. But for true professional PHP developers, understanding how to install and compile PHP against their operating system is somewhat important.
In my case, I want to use the latest versions of PHP, since the tools I’m using often track the most recent releases (PHPUnit, PHPMD, etc.). Therefore, it’s important that the small improvements are available to me.
It’s worth noting that I started with a completely clean install of Ubuntu, that had never had Apache or PHP installed on it before. If you’re starting with a server that’s had either of these installed on it, you will need to modify your instructions.
I’m assuming that you’re running these commands as root; therefore, I do not include the sudo command. Add this command if you are not running as root (standard disclaimer applies about not running as root unless you know what you’re doing).
Preparing the environment
In order to properly install PHP, we’ll need to prepare a number of dependencies.
I like to have support for Apache, MySQL and Postgres, so we’ll want to make sure and install those things so that PHP can find them:
aptitude install build-essential vim aptitude install apache2 apache2-mpm-prefork apache2-prefork-dev apache2-utils apache2.2-common aptitude install postgresql-9.1 postgresql-client-9.1 postgresql-client-common postgresql-common postgresql-server-dev-9.1 aptitude install mysql-client mysql-client-5.5 mysql-common mysql-server mysql-server-5.5 mysql-server-core-5.5 libmcrypt-dev curl openssl
Note that I’m also installing some additional libraries here: openssl, curl, libmcrypt-dev, and build-essential (which is necessary for make and make install).
Building the dependencies for PHP
There are a number of other dependencies that PHP will rely upon, so it’s important to build those as well. Lucky for us, we can use a single, simple command to build the dependencies that PHP will need:
apt-get build-dep php5
This command will fetch and build all the dependencies that PHP 5 relies upon. Bear in mind that you can skip this step and install the libraries yourself, if you so desire (or if you need a special configuration).
If you don’t run this command or install the required libraries, you’ll get all kinds of unpleasant errors from the PHP configure command.
Configuring and installing PHP
Once you’ve got the dependencies dealt with, it’s time to go forward and build PHP. Once you’ve downloaded PHP 5.5 (see www.php.net for the latest RC1 build), untar it and change into that directory.
I like a pretty standard set of configuration options with my PHP installations. However, I do include a few unique settings that I will explain here:
- –enable-opcache: this setting is new in PHP 5.5. The Zend Opcache was moved into PHP 5.5’s core, and this flag will enable the opcache when you compile PHP 5.5. The opcache is disabled by default.
- –prefix=/opt/php: This prefix is used to tell the installer where to place the PHP files. I use /opt/php so that all the PHP files are stored in pretty much the same place. You can remove this and it will install the PHP files in the default locations on the operating system.
- –with-config-file-path=/opt: I found in PHP 5.4 that if I didn’t specify this, PHP couldn’t find my INI file. You can remove this if you wish.
./configure --enable-opcache --prefix=/opt/php --with-apxs2=/usr/bin/apxs2 --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pgsql=/usr --with-tidy=/usr --with-curl=/usr/bin --with-openssl-dir=/usr --with-zlib-dir=/usr --with-xpm-dir=/usr --with-pdo-pgsql=/usr --with-pdo-mysql=mysqlnd --with-xsl=/usr --with-ldap --with-xmlrpc --with-iconv-dir=/usr --with-snmp=/usr --enable-exif --enable-calendar --with-bz2=/usr --with-mcrypt=/usr --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --with-freetype-dir=/usr --enable-mbstring --enable-zip --with-pear --with-libdir=/lib/x86_64-linux-gnu --with-config-file-path=/opt
Once you have successfully run the ./configure command, you’ll need to run the make command to compile PHP.
After make is finished running, you’ll be prompted to run make test. If you’re compiling an unreleased version of PHP, do the community a favor and run these tests. Report any test failures you encounter. If you come across failures, run make clean and recompile.
After running make test, it’s time to install PHP. Run make install to install PHP to the destination you chose in configuration.
When you install PHP from a package, it usually takes care of installing the required configuration in Apache. However, it has been my experience that I need to insert the correct information into Apache for Apache to recognize PHP files. This is the configuration I use in apache.conf:
<IfModule php5_module> AddType application/x-httpd-php .php AddType application/x-httpd-php .phps <IfModule dir_module> DirectoryIndex index.html index.php </IfModule> </IfModule>
Be sure and restart Apache after this is completed.
PHP doesn’t automatically install an INI file, so you’ll want to do that. Note that there are two in the directory where you unpacked the tarball: php.ini-development, and php.ini-production. Select the correct configuration file for your server and copy it to the path you selected for the PHP INI directory.
You’ll need to add the location of your PHP binary to your path in order to run PHP from the command line; my PHP binary is in /opt/php/bin and this is in my path. Edit the ~/.profile file and add the following line:
This will add the PHP binary to your path. You may also need to add the path of the PHP binary to your sudoers file. Run visudo as root and edit the secure path to include the correct location for the PHP binary directory.
In order to use the opcode cache in PHP 5.5, you’ll need to add zend_extension=opcache.so to your php.ini file. You’ll also need to modify the opcache.enable=0 line to opcache.enable=1 (this was around line 1865 in my php.ini file).
Frustrated with your company’s development practices?
You don't have to be!
No matter what the issues are, they can be fixed. You can begin to shed light on these issues with my handy checklist.
Plus, I'll help you with strategies to approach the issues at the organization level and "punch above your weight."