Instruction Guide to Install PHP5 from Source on Linux

by Ramesh Natarajan on July 31, 2008

PHP LogoAll Linux distributions comes 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

Apache web server should already be installed. Refer to my previous post on How to install Apache 2 on Linux. If you are planning to use PHP with MySQL, you should have My SQL already installed. I wrote about How to install MySQL on Linux.

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 http://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

Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

If you enjoyed this article, you might also like..

  1. Install Apache 2 from Source on Linux
  2. 9 Steps to Install and Configure PostgreSQL from Source on Linux
  3. Howto Install MySQL on Linux
  4. How To Install Or Upgrade LAMP: Linux, Apache, MySQL and PHP Stack Using Yum
  5. IFTOP Guide: Display Network Interface Bandwidth Usage on Linux
  

Vim 101 Hacks Book

Tags: , ,

{ 2 trackbacks }

The Geek Stuff » How To Install Or Upgrade LAMP: Linux, Apache, MySQL and PHP Stack Using Yum
September 15, 2008 at 5:18 pm
Instruction Guide to Install PHP5 from Source on Linux
May 20, 2009 at 10:59 pm

{ 3 comments… read them below or add one }

1 Keith Dsouza July 31, 2008 at 1:56 pm

I installed Apache, PHP and Mysql on a ubunutu hardy recently and it was fun, this article makes it to my bookmakrs for future use :D

2 Ajtih Edassery August 1, 2008 at 12:00 pm

Good stuff. Me not a Linux fan/expert yet :) though installing PHP/MySQL/Apache is always fun on my Windows itself…

Btw, I have set some targets for August mainly on the Technorati rank front. You might (and other readers as well) want to help me with that (sorry for the shameless self promotion).

Cheers,
Ajith

3 alex February 5, 2010 at 1:16 pm

Hi,
I have installed MySQL, Apache2 and PHP based on your documentations here. Everything has been working fine basically. Your papers are excellent.
Thanks a lot!!

Leave a Comment

Previous post:

Next post: