Install the latest PHP from source package

Posted on Posted in PHP

As you may know the latest versions of the software packages are not available in the software repositories. It can take few months for any release upgrade to appear in the repository. Unfortunately in the real world the latest releases never appear in the official repositories of RHEL/CentOS Linux.

In my opinion it is extremely risky to install the packages that were pre-configured by someone else because such packages can contain back-doors or malware.

However you can always keep your PHP up to date using the package with the source code. There is nothing to be afraid of with such approach.

The following article describes the approach that can be used to compile the latest PHP using the source package.

I used fresh installation of the CentOS 6.7 to test this tutorial.

First of all make sure that your system has required packages:
yum install aspell-devel bzip2-devel curl-devel freetype-devel gcc gcc-c++ gmp-devel libc-client-devel libicu-devel libjpeg-devel libpng-devel libxml2-devel libxslt-devel make mhash-devel mysql-devel ncurses-devel net-snmp-devel openldap-devel openssl-devel pkgconfig postgresql-devel unixODBC-devel db4-devel pcre-devel sqlite-devel enchant-devel libXpm-devel libedit-devel recode-devel libtidy-devel

There are multiple brunches currently supported by php.net. The latest is PHP v.7, latest stable is PHP v.5.6, Old Stable is PHP v.5.5. Other releases are considered as outdated and are no longer supported.

It is highly recommended to upgrade to PHP v.5.6 if you are running PHP v.5.3 and older versions.

The latest source codes of PHP can be downloaded by running one of the following commands:
wget -O php-7.1.0.tar.gz http://php.net/get/php-7.1.0.tar.gz/from/this/mirror

wget -O php-5.6.24.tar.gz http://php.net/get/php-5.6.24.tar.gz/from/this/mirror

wget -O php-5.5.38.tar.gz http://php.net/get/php-5.5.38.tar.gz/from/this/mirror

The downloaded file should be uncompressed when done:
tar xf php-*.tar.gz

Next navigate to the source folder:
cd php-*

The next step is to configure the new PHP to make it ready for compilation.

The following huge and freaky configure command is the default for servers driven by Plesk panel, so I am going to use it to compile the most complete PHP:
./configure --prefix=/usr/local/$(printf '%s\n' "${PWD##*/}") --exec-prefix=/usr/local/$(printf '%s\n' "${PWD##*/}") --bindir=/usr/local/$(printf '%s\n' "${PWD##*/}")/bin --sbindir=/usr/local/$(printf '%s\n' "${PWD##*/}")/sbin --sysconfdir=/usr/local/$(printf '%s\n' "${PWD##*/}")/etc --datadir=/usr/local/$(printf '%s\n' "${PWD##*/}")/share --includedir=/usr/local/$(printf '%s\n' "${PWD##*/}")/include --libdir=/usr/local/$(printf '%s\n' "${PWD##*/}")/lib64 --libexecdir=/usr/local/$(printf '%s\n' "${PWD##*/}")/libexec --localstatedir=/var --sharedstatedir=/var/lib --mandir=/usr/share/man --infodir=/usr/share/info --cache-file=../config.cache --with-libdir=lib64 --with-config-file-path=/usr/local/$(printf '%s\n' "${PWD##*/}")/etc --with-config-file-scan-dir=/usr/local/$(printf '%s\n' "${PWD##*/}")/etc/php.d --disable-debug --with-pic --disable-rpath --without-pear --with-bz2 --with-exec-dir=/usr/local/$(printf '%s\n' "${PWD##*/}")/bin --with-freetype-dir=/usr --with-png-dir=/usr --with-xpm-dir=/usr --enable-gd-native-ttf --without-gdbm --with-gettext --with-gmp --with-iconv --with-jpeg-dir=/usr --with-openssl --with-pcre-regex=/usr --with-zlib --with-layout=GNU --enable-exif --enable-ftp --enable-magic-quotes --enable-sockets --enable-sysvsem --enable-sysvshm --enable-sysvmsg --with-kerberos --enable-ucd-snmp-hack --enable-shmop --enable-calendar --without-sqlite --with-libxml-dir=/usr --enable-xml --with-system-tzdata --enable-force-cgi-redirect --enable-pcntl --with-imap=shared --with-imap-ssl --enable-mbstring=shared --enable-mbregex --with-gd=shared --enable-bcmath=shared --enable-dba=shared --with-db4=/usr --with-xmlrpc=shared --with-ldap=shared --with-ldap-sasl --with-mysql=shared,/usr --with-mysqli=shared,/usr/lib64/mysql/mysql_config --enable-dom=shared --with-pgsql=shared --enable-wddx=shared --with-snmp=shared,/usr --enable-soap=shared --with-xsl=shared,/usr --enable-xmlreader=shared --enable-xmlwriter=shared --with-curl=shared,/usr --enable-fastcgi --enable-pdo=shared --with-pdo-odbc=shared,unixODBC,/usr --with-pdo-mysql=shared,/usr/lib64/mysql/mysql_config --with-pdo-pgsql=shared,/usr --with-pdo-sqlite=shared,/usr --with-sqlite3=shared,/usr --enable-json=shared --enable-zip=shared --without-readline --with-libedit --with-pspell=shared --enable-phar=shared --with-tidy=shared,/usr --enable-sysvmsg=shared --enable-sysvshm=shared --enable-sysvsem=shared --enable-posix=shared --with-unixODBC=shared,/usr --enable-fileinfo=shared --enable-intl=shared --with-icu-dir=/usr --with-enchant=shared,/usr

I am using the following construction to capture the name of the current directory. It should be either php-7.1.0, php-5.6.24 or php-5.5.38. The aim of this tutorial is to unify the approach in compiling the PHP:
$(printf '%s\n' "${PWD##*/}")

You don't need to change anything in this configure command no matter if you are installing PHP v.7 or v.5.6 or v.5.5.

The configure command from the about doesn't compile the Apache2 module. This was done to prevent overwriting the existing php module if you will decide to compile php on the production server.

If you need to have it included into the php installation, than please include the '--with-apxs2' into the list of configure arguments. Also make sure that httpd-devel is installed.

The remaining steps are to make and install the php:
make
make install

It is going to take a while to have it built and installed however at the end you will have the complete php installation located in the following folder:

/usr/local/php-{7.1.0,5.6.24,5.5.38}

The final output will look like the following:

Installing shared extensions:     /usr/local/php-5.6.24/lib64/20131226/
Installing PHP CLI binary:        /usr/local/php-5.6.24/bin/
Installing PHP CLI man page:      /usr/share/man/man1/
Installing PHP CGI binary:        /usr/local/php-5.6.24/bin/
Installing PHP CGI man page:      /usr/share/man/man1/
Installing build environment:     /usr/local/php-5.6.24/lib64/build/
Installing header files:           /usr/local/php-5.6.24/include/php/
Installing helper programs:       /usr/local/php-5.6.24/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/share/man/man1/
  page: phpize.1
  page: php-config.1
/usr/local/src/php-5.6.24/build/shtool install -c ext/phar/phar.phar /usr/local/php-5.6.24/bin
ln -s -f phar.phar /usr/local/php-5.6.24/bin/phar
Installing PDO headers:           /usr/local/php-5.6.24/include/php/ext/pdo/

Leave a Reply

Your email address will not be published. Required fields are marked *