Install PHP 5.x from repository on Ubuntu

Install build tool

Here are detailed steps for installing the compile tool for ubuntu.

Install dependencies

In most cases, quite a lot of dependencies will be missing. You can install most of them using the next command

sudo apt-get install libxml2-dev libpcre3-dev libbz2-dev libcurl4-openssl-dev libdb4.8-dev libjpeg-dev libpng12-dev libxpm-dev libfreetype6-dev libmysqlclient-dev postgresql-server-dev-9.1 libt1-dev libgd2-xpm-dev libgmp-dev libsasl2-dev libmhash-dev unixodbc-dev freetds-dev libpspell-dev libsnmp-dev libtidy-dev libxslt1-dev libmcrypt-dev

Compile and install PHP

Clone the PHP repository

sudo mkdir /opt/source && cd /opt/source
git clone git@github.com:php/php-src.git && cd php-src

Choose the version of PHP you want to compile and install

  PHP 5.3:  git checkout PHP-5.3
  PHP 5.4:  git checkout PHP-5.4
  PHP 5.6:  git checkout PHP-5.6
  PHP HEAD: git checkout master

To building PHP from a git repository, first, you have to run sudo ./buildconf .

./buildconf generates the configuration script. This may take several minutes.

 Create the folder where you want to install the PHP:

Suggestion:

sudo mkdir -p /opt/php-5.6
sudo mkdir -p /opt/php-5.5

 Configure build

Once the ./configure script is generated via the step above, you can use it to customize your PHP build. You can list all supported options using –help:

 ./configure \
--with-freetype=/usr/local \
--with-config-file-path=/opt/php-5.6/config.d \
--with-config-file-scan-dir=/opt/php-5.6/conf.d \
--prefix=/opt/php-5.6 \
--with-zlib-dir \
--with-freetype-dir \
--enable-mbstring \
--with-libxml-dir=/usr \
--enable-soap \
--enable-calendar \
--with-curl \
--with-mcrypt \
--with-zlib \
--with-gd \
--disable-rpath \
--enable-inline-optimization \
--with-bz2 \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--with-mhash \
--enable-zip \
--with-pcre-regex \
--with-mysql \
--with-pdo-mysql \
--with-mysqli \
--with-png-dir=/usr \
--enable-gd-native-ttf \
--with-openssl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-libdir=lib \
--enable-ftp \
--with-imap \
--with-imap-ssl \
--with-kerberos \
--with-gettext \
--with-gd \
--with-xpm-dir=/usr \
--with-ldap=/usr \
--with-jpeg-dir=/usr/lib/ \
--enable-fpm \
--with-fpm-user=www-data \
--with-fpm-group=www-data \


Compile PHP

Now, you can use make to perform the actual compilation:

make

 Install PHP

Now you can run “make install” to install PHP into “/usr/local” (default) or another directory by using the --prefixconfiguration. In this case, it’s /opt/php-5.6

sudo make install

Please note that make install will not create an ini file.

/opt/php-5.6/bin/php --ini
Configuration File (php.ini) Path: /opt/php-5.6/lib
Loaded Configuration File:         (none)
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

Copy config files

sudo cp /opt/source/php-src/php-fpm.conf.default /opt/php-5.6/etc/php-fpm.conf
sudo cp /opt/source/php-src/php.ini-production /opt/php-5.6/lib/php.ini

Open /opt/php-5.6/etc/php-fpm.conf and adjust the value in the listen line. You must change to an unused port (e.g. 9001)

Init script setup

You will probably want to create an init script for your new “php-fpm”. Luckily, PHP 5.3+ already provides it for you, simply copy the init script to your directory and change permissions:

1
2
sudo cp /opt/source/php-src/sapi/fpm/init.d.php-fpm /etc/init.d/php5.6-fpm
sudo chmod 755 /etc/init.d/php5.6-fpm

Your init script is ready. Now, you are able to start, stop, and reload php-fpm:

1
2
3
sudo /etc/init.d/php5.6-fpm start
sudo /etc/init.d/php5.6-fpm stop
sudo /etc/init.d/php5.6-fpm reload

Create an alias for every command:

alias p56start='sudo /etc/init.d/php5.6-fpm start'
alias p56stop='sudo /etc/init.d/php5.6-fpm stop'
alias p56r='sudo /etc/init.d/php5.6-fpm restart'

alias p53start='sudo /etc/init.d/php5.3-fpm start'
alias p53stop='sudo /etc/init.d/php5.3-fpm stop'
alias p53r='sudo /etc/init.d/php5.3-fpm restart'

alias nginxr='sudo service nginx restart'