Compiling PHP from source on Ubuntu

From Helioviewer Wiki

Jump to: navigation, search

Contents

Overview

In many cases, it may be desirable to upgrade PHP to a more recent version than what is available in the Ubuntu repositories. For example, you may want to upgrade PHP in order to get the most recent security updates.

This guide describes the steps you can take to install the latest version of PHP on Ubuntu from source, while maintaining compatibility with pre-installed extensions and configuration settings. Although this guide is geared primarily towards Ubuntu installations, it is easily-adaptable to other Linux distributions.

The below instructions were tested on a Ubuntu 10.04 machine with PHP 5.3.2 installed via the Ubuntu repositories. The latest stable release of PHP at the time of writing is PHP 5.3.3.

Pre-installation

Before beginning the installation, it is a good idea to keep some records how your existing PHP installation is configured. If you do not already have a copy of PHP installed on your system, you can skip this step. Create a script that calls "phpinfo()", and save the output as HTML, PS, or a PDF. This is useful to see what settings PHP used, and which extensions were detected.

Alternatively, you can also run:

php -i > php-info.txt

to capture the same information from the command line.

Installation

Getting the latest version of PHP

Download latest stable release of PHP can be downloaded from [1] and extract the contents to a temporary directory:

tar xzvpf php-5.3.3.tar.gz -C /tmp

Apply Suhosin patch

The version of PHP that ships is a modified version which has been patched for extra security. In order to apply the same patch, first go to the Suhosin download page and download the patch that corresponds to the version of PHP you are compiling. Next, extract the patch into the PHP source code directory:

gunzip suhosin-patch-5.3.3-0.9.10.patch.gz
mv suhosin-patch-5.3.3-0.9.10.patch /tmp/php-5.3.3/

Change directories to the source code directory, here /tmp/php-5.3.3, and apply the patch using the command:

patch -p 1 -i suhosin-patch-5.3.3-0.9.10.patch

For more information see the Suhosin installation article.

Install pre-requisites

Install the necessary development headers for compiling PHP from source:

sudo aptitude build-dep php5

Run configure

While still in the PHP source directory, run:

./configure --with-config-file-path=/etc/php5/apache2 \
--with-pear=/usr/share/php \
--with-bz2 \
--with-curl \
--with-gd \
--enable-calendar \
--enable-mbstring \
--enable-bcmath \
--enable-sockets \
--with-libxml-dir=/usr \
--with-mysqli \
--with-mysql \
--with-openssl=/usr \
--with-regex=php \
--with-zlib \
--with-apxs2=/usr/bin/apxs2

This will setup the build to do several things:

  1. Your existing PHP configuration will be used ("--with-config-file-path=/etc/php5/apache2")
  2. Support will be enabled for: cURL, GD, Multibyte strings, MySQL, PEAR and zlib
  3. An Apache module will be created ("--with-apxs2=/usr/bin/apxs2")

Make and install

Finally, we are read to make and install the new version of PHP:

make
sudo make install

Configuration

Once you have built and installed PHP, you will need to modify your configuration so that previously installed extensions can be found. The location where extensions are stored for the version of PHP in the Ubuntu repositories may be different from the extension directory after compilation. For example, in my case the extension directory used by the base Ubuntu version of PHP was "/usr/lib/php5/20090626". After compiling PHP from source, the directory scanned became "/usr/local/lib/php/extensions/no-debug-non-zts-20090626". This means that modules that are located in /usr/lib/php5/20090626 but not in /usr/local/lib/php/extensions/no-debug-non-zts-20090626 will not be found and any attempts to use functionality provided by those modules will fail.

To determine the location of the extension directory, you can either look for the "extension_dir" parameter in phpinfo(), or the command-line tool "php-config", e.g.:

php-config --extension-dir
      

If there are modules which need are no longer available after compilation, depending on whether the extensions were install via the PECL or the Ubuntu repositories or elsewhere, you can take one of several steps to enable PHP to find the pre-existing extensions.

Using symbolic links

The easiest solution to enable support for extensions installed using either apt-get or by hand is to create symbolic links from the old extension directory to the new one. If there are no files in the "new" extension directory, or if that directory does not exist, then you can simply create a link to the old extension directory, e.g.:

sudo ln -s /usr/lib/php5/20090626/ /usr/local/lib/php/extensions/no-debug-non-zts-20090626

Otherwise, if there are already files in the new extension directory, you will need to create links for each individual shared library.

For example, if you installed the PHP ImageMagick extension using "apt-get install php-imagick", you can create a link to the library in the new extension directory using:

sudo ln -s /usr/lib/php5/20090626/imagick.so /usr/local/lib/php/extensions/no-debug-non-zts-20090626/imagick.so
  

Do this for each extension you plan to use that exists in the old extension directory, but not the new one.

Reinstalling extensions using PECL

For extensions installed through PECL, simply upgrading any packages which have updates available will cause the updated shared libraries to be placed in the new extension directory:

sudo pecl update-channels && sudo pecl upgrade
   

If the extensions were installed through PECL, but no new version is available, you can reinstall the extension using:

sudo pecl uninstall EXTNAME && sudo pecl install EXTNAME

Note that for extensions installed this way any old versions of the extension are removed so if you wish to go back to an older version of PHP in the future you will need to either reinstall or link to the new location of the extensions.

Manually specifying the extension directory location

Finally, if all of the extensions you wish to use are in a single location, you can edit your PHP configuration and specify the directory to use. To do this, edit /etc/php5/apache/php.ini, and find the line with "extension_dir". Uncomment and adjust the parameter to point to the directory where you extensions (the files ending in .so) are installed, e.g.:

extension_dir = /usr/lib/php5/20090626

Restart Apache

Run:

sudo service apache2 restart

You should now be running the latest version of PHP. Check /var/log/apache/error.log and try out some of the different Helioviewer API calls to make sure all of the functionality is working properly.

Handling Ubuntu PHP Updates

Trouble-shooting

Modeling your PHP configuration after the Ubuntu repository version

If you have a complex PHP setup and want to replicate the compilation steps used when creating the version of PHP available in the Ubuntu repositories, you can find more information in PHP5 source code available through the Ubuntu repos.

mkdir php-current-src && cd php-current-src 
apt-get source php5

This will download a copy of the PHP5 source code, along with some helper scripts used by Ubuntu. Note that in order for aptitude to be able to retrieve source code, you must have the "deb-src" renabled in /etc/apt/sources.list.

The file we are interested in is called "rules":

less php5-5.3.2/debian/rules

Although there is a lot going on in the file, the important bits are the configuration flags like "--with-pear=" etc. This will give you an idea of how to setup your "configure" command so that the resulting build is similar to what you would get had you installed PHP via apt-get.

Reverting to older version of PHP

If you were originally using the version of PHP from the Ubuntu repositories and wish to revert back to it, you can reinstall the module using apt-get:

sudo apt-get --reinstall install libapache2-mod-php5

You may need to follow the steps above to re-establish links to existing extensions, but otherwise everything should work as before.

Timezone complaints

If you find that after installing the new version of PHP you are getting warnings along the lines of:

date() [function.date]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier.

Edit your PHP configuration (/etc/php5/apache/php.ini) and specify the timezone you wish to use. For example:

date.timezone=America/New_York

Getting Xdebug working

If you installed Xdebug either through the Ubuntu repositories or using PECL, you may not be able to load it using the recommended "zend_extension" setting. To get Xdebug to work properly, download and compile the extension from source. To simplify the process, follow the directions from the Xdebug tailored installation instructions tool.

buy lasix online
Personal tools