All 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
Comments on this entry are closed.
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 😀
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
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!!
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
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.
if i want to make this install from source, do i need to uninstall the php i already have installed on my server?
thanks
This article looks impressive. But may I know how to install PHP on thttpd web server?
Thanks
Thanks! Very useful!