How To: Install PHP from source code on Linux

How To: Install PHP from source code on Linux

All Linux distributions come with PHP. However, it is recommended to download latest PHP source code, compile and install on Linux. This will make it easier to upgrade PHP on an ongoing basis immediately after a new patch or release is available for download from PHP. This article explains how to install PHP5 from source on Linux.

1. Prerequisites

  • A Linux machine.
  • Apache web server installed (optional).
  • MySQL server installed (optional).

2. Download PHP

Download the latest source code from PHP Download page. Current stable release is 5.2.6. Move the source to /usr/local/src and extract is as shown below.

# bzip2 -d php-5.2.6.tar.bz2
# tar xvf php-5.2.6.tar

3. Install PHP

View all configuration options available for PHP using ./configure –-help (two hyphen in front of help). The most commonly used option is –-prefix={install-dir-name} to install PHP on a user defined directory.

# cd php-5.2.6
# ./configure --help

In the following example, PHP will be compiled and installed under the default location /usr/local/lib with Apache configuration and MySQL support.

# ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql
# make
# make install
# cp php.ini-dist /usr/local/lib/php.ini

4. Configure httpd.conf for PHP

Modify the /usr/local/apache2/conf/httpd.conf to add the following:

<FilesMatch "\.ph(p[2-6]?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>

Make sure the httpd.conf has the following line that will get automatically inserted during the PHP installation process.

LoadModule php5_module modules/libphp5.so

Restart the apache as shown below:

# /usr/local/bin/apache2/apachectl restart

5. Verify PHP Installation

Create a test.php under /usr/local/apache2/htdocs with the following content

# vi test.php
<?php phpinfo(); ?>

Go to https://local-host/test.php , which will show a detailed information about all the PHP configuration options and PHP modules installed on the system.

6. Trouble shooting during installation

Error 1: configure: error: xml2-config not found:
While performing the ./configure during PHP installation, you may get the following error:

# ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql
Configuring extensions
checking whether to enable LIBXML support... yes
checking libxml2 install dir... no
checking for xml2-config path...
configure: error: xml2-config not found. Please check your libxml2 installation.

Install thelibxml2-devel and zlib-devel as shown below to the fix this issue.

# rpm -ivh /home/downloads/linux-iso/libxml2-devel-2.6.26-2.1.2.0.1.i386.rpm /home/downloads/linux-iso/zlib-devel-1.2.3-3.i386.rpm
Preparing...                ########################################### [100%]
1:zlib-devel             ########################################### [ 50%]
2:libxml2-devel          ########################################### [100%]

Error 2: configure: error: Cannot find MySQL header files.
While performing the ./configure during PHP installation, you may get the following error:

# ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql
checking for MySQL UNIX socket location... /var/lib/mysql/mysql.sock
configure: error: Cannot find MySQL header files under yes.
Note that the MySQL client library is not bundled anymore!

Install the MySQL-devel-community package as shown below to fix this issue.

# rpm -ivh /home/downloads/MySQL-devel-community-5.1.25-0.rhel5.i386.rpm
Preparing...                ########################################### [100%]
1:MySQL-devel-community  ########################################### [100%]

References: PHP Manual

Comments

comments