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


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

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

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Tags: , ,

{ 7 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!!

4 seryi August 10, 2010 at 4:58 am

thx this is very helpful :)

just edit your post, there is a misstake

” /usr/local/bin/apache2/apachectl restart”
should be
/usr/local/apache2/bin/apachectl restart

5 g September 2, 2010 at 8:04 pm

can I request installing php 5.2.x from source code?
Because I already got the tar file.
But dunno how to install php 5.2.x dependencies
and dunno how to set the configuration files for GD.
I found out we need to change the debian folder, but i found nothing.

Anyone could forward me to solve this out?
I’m looking for compiling php 5.2.14 from source code.

6 jeronimo September 19, 2012 at 3:00 am

if i want to make this install from source, do i need to uninstall the php i already have installed on my server?
thanks

7 Sohal December 6, 2013 at 3:58 am

This article looks impressive. But may I know how to install PHP on thttpd web server?

Thanks

Leave a Comment

Previous post:

Next post: