How to Compile php v.5.5.10 from Sources

This note covers how to install the latest version of PHP on your server from source, using PHP 5.5.10 + CentOS 6.5 as an example.

First, download the source code from an official mirror. I live in Ukraine, and php.net offered me this list of mirrors.
Screenshot from 2014-03-18 10:58:40

You can follow this link, and based on your location it’ll suggest mirrors located in your country.

I’ll walk through installing php v.5.5.10 on a clean CentOS 6.5 server.

So, download and unpack:

wget -O php-5.5.10.tar.gz http://ua1.php.net/get/php-5.5.10.tar.gz/from/this/mirror  
tar xf php-5.5.10.tar.gz && cd php-5.5.10

To make it all work we need a C++ compiler.
Screenshot from 2014-03-18 11:07:07

There isn’t one on a clean server, so we need to install it:

yum install gcc make

At this stage the configurator will basically run and die with this error:

checking libxml2 install dir no  
checking for xml2-config path  
configure: error: xml2-config not found. Please check your libxml2 installation.

We need to get libxml on board:

yum install libxml2-devel

At this stage the configure script runs without errors. It finds what’s on the system and what it can build php with. I won’t go into detail on the options enabled by default, I’ll just say that with that set you won’t get far. There’s nothing in that list that would help us run even a simple little site.

I’ll be building php with support for the following modules:

  • curl
  • mysql
  • mysqli
  • mhash
  • pdo-mysql
  • gd
  • mcrypt
  • mbstring
  • openssl
  • pcre
  • soap
  • Apache module
  • zlib

First let’s enable the Epel repository:

wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm  
rpm -Uvh epel-release-6*.rpm

Next we need to get the required packages:

yum install mysql-devel httpd-devel gd-devel libmcrypt-devel mcrypt bison-devel mhash-devel libcurl-devel

Now we can build:

./configure -bindir=/usr/bin -with-config-file-path=/etc -with-curl -with-mhash -with-mysql -with-mysqli -with-gd -with-pdo-mysql -with-mcrypt -enable-mbstring -with-openssl -with-pcre-regex -enable-soap -with-apxs2 -with-zlib

a bit of an explanation:
--bindir - path to save the binaries
--with-config-file-path - path where php.ini is stored

By default php gets put in the /usr/local folder.

Next we install:

make && make install

During the install, make should add this line to httpd.conf. If it doesn’t - do it yourself:
LoadModule php5_module /usr/lib/httpd/modules/libphp5.so

The source code folder provides 2 types of php.ini. Depending on what the server is used for, run one of the following commands:

cp php.ini-development /etc/php.ini

or

cp php.ini-production /etc/php.ini

Teach Apache to work with php files:
nano /etc/httpd/conf.d/php.conf

<FilesMatch \.php$>  
	SetHandler application/x-httpd-php  
</FilesMatch>

When restarting Apache I got this error:

Starting httpd: httpd: Syntax error on line 216 of /etc/httpd/conf/httpd.conf: Cannot load /usr/lib/httpd/modules/libphp5.so into server: /usr/lib/httpd/modules/libphp5.so: cannot restore segment prot after reloc: Permission denied

That’s because Selinux blocks adding modules to Apache. You can temporarily disable SeLinux with the command:

/usr/sbin/setenforce 0

To disable SeLinux for good, in the /etc/selinux/config file
Replace:
SELINUX=enforcing
with
SELINUX=disabled

Now you can create a php.php file in the /var/www/html folder with the following content:

<?php phpinfo() ?>

And open it in a browser. If everything works - you’ll see this picture:
Screenshot from 2014-03-18 14:58:42