Installing PHP From Source
Posted December 4, 2006on:
Dynamic web pages are now getting easier to be made, since there has been a new technology, called server-side scripting which process the script on the server on the fly and request-based and returns the results to the client in the form of HTML pages. This makes the process becomes transparent to the client. One of the technology that is well known is PHP. The other technology includes ASP (Active Server Pages) from Microsoft, JSP (Java Server Pages), and ColdFusion. Each technology has different ways of installation process. For example, ASP has been bundled in the operating system itself (Windows 2000, Windows XP, and Windows 2003) and all you need to run the ASP files is by installing the IIS (Internet Information Service), a web server which is provided by Microsoft to process the ASP files. JSP in other hand, need Apache Tomcat web server (part of Apache Project) in order to process JSP files (also Java packages). Meanwhile, PHP can work with many web server available, such as IIS, Apache, Xitami, PWS (Personal Web Server, mini version of IIS for Windows 98 and Windows ME), and many other common web servers. This is one reason why PHP is so popular and being used by many people and developers. It has high portability in case of web server is being used.
PHP is mainly distributed in form of source code which can be downloaded from PHP’s web sites, but in many distro (GNU/Linux Distribution), the vendor has also provided the binary version of PHP. Most of times, the vendor splits the PHP packages into several packages, such as php-mysql, php-devel, php-gd, etc. Why does they do this? Well.. the answers is flexibility. If you only need basic PHP packages, all you need are php and mod_php (needed to configure Apache to parse .php files correctly), but if you need other functionality, such as MySQL support or GD features, you only need to add php-mysql and php-gd packages (and their dependencies). One disadvantage of using this approach is that we are dependent to vendor’s configuration parameter and also packages that are available. We can’t add extensions that were not provided by the vendors. In order to solve this, we can try to install PHP from the source code. This article will try to help you to do that. I will use PHP 4 for this article, since i’m quite happy with PHP 4 and there has been some changes made in PHP 5 which makes some problems with some extensions. You can apply this method to install PHP 5 also, but you will need to adjust some parameter in order to make it work, since some parameters probably has been changed and also some extensions have been modified (for example, MySQL extension is not a built in extension in PHP 5, so you will need to add it to your configuration parameter in order to work with MySQL).
Some basic packages which you need in order to install PHP is a web server (in this case it will be Apache) and PHP packages it self. You can use your distro’s Apache packages, just like i did with my Slackware system. I’m using Apache 1.3.34 in this case, since it’s the latest version of Apache found in Slackware’s repository. For the PHP packages, you will need to download it from PHP’s website and download the correct version. I’m using PHP 4.4.2 as this is the latest version when i’m writing this article. Enough for preparation. Let’s get down to business.
The first step is configuring your PHP. Extract your PHP in some directory, for example in your home directory using
tar -xjvf php-4.4.2.tar.bz2
Next, go into the PHP directory by using
Let’s start viewing all the configuration parameter by executing
Too much huh? Yeah.. i know it’s a lot. That’s why i use some tricky way to solve this problem. Before i use Slackware, i use Mandriva and i used the RPM packages for installing PHP. I installed all packages i need and then i made a file called phpinfo.php which consists of this lines
In top of page there are the configuration parameter that is being used to compile the PHP. I copied that and save it into some file. Next, when i have removed all the PHP packages, i used that parameter to compile the new PHP packages (from source). I will need to adjust some path in order to make it work with my Slackware system. For a reference, here is my compilation parameter
‘./configure’ ‘–prefix=/usr/local’ ‘–enable-discard-path’ ‘–disable-force-cgi-redirect’ ‘–enable-shared’ ‘–disable-static’ ‘–disable-debug’ ‘–disable-rpath’ ‘–enable-pic’ ‘–enable-inline-optimization’ ‘–enable-memory-limit’ ‘–with-config-file-path=/etc’ ‘–with-config-file-scan-dir=/etc/php’ ‘–with-pear=/usr/share/pear’ ‘–enable-magic-quotes’ ‘–enable-debugger’ ‘–enable-track-vars’ ‘–with-exec-dir=/usr/bin’ ‘–with-versioning’ ‘–with-mod_charset’ ‘–with-regex=php’ ‘–enable-track-vars’ ‘–enable-trans-sid’ ‘–enable-safe-mode’ ‘–enable-ctype’ ‘–enable-ftp’ ‘–with-gettext=/usr’ ‘–enable-posix’ ‘–enable-session’ ‘–enable-sysvsem’ ‘–enable-sysvshm’ ‘–with-openssl=/usr’ ‘–without-kerberos’ ‘–with-ttf’ ‘–with-freetype-dir=/usr’ ‘–with-zlib=/usr’ ‘–with-zlib=/usr’ ‘–with-zlib-dir=/usr’ ‘–with-apxs=/usr/sbin/apxs’ ‘–with-gd’ ‘–with-png-dir’ ‘–with-jpeg-dir’ ‘–with-zlib-dir’ ‘–with-tiff-dir’ ‘–enable-xslt’ ‘–with-xslt-sablot’ ‘–with-iconv-dir=/usr/local/lib’ ‘–with-curl’ ‘–with-mysql-sock=/var/run/mysql/mysql.sock’ ‘–with-mysql=/usr’ ‘–enable-mbstring=all’
Here are small explanation of above script
PHP will be installed in /usr/local (–prefix=/usr/local)
Use shared library and disabled static library (–enable-shared –disable-static)
Disable debugging (–disable-debug)
Use inline optimization and limit memory being used for compilation (–enable-inline-optimization –enable-memory-limit)
Configuration file will be be placed in /etc (–with-config-file-path=/etc)
Using PEAR which will be placed in /usr/share/pear (–with-pear=/usr/share/pear)
Enable Magic Quotes (–enable-magic-quotes)
Using PHP’s Regex engine (–with-regex=php)
Enable Safe Mode, FTP, Session, and OpenSSL support (–enable-safe-mode –enable-ftp –enable-session –with-openssl=/usr)
Enable GD and configure it to support JPEG, TIFF, Freetype fonts and PNG (–with-gd –with-png-dir –with-freetype-dir=/usr –with-jpeg-dir –with-tiff-dir)
Enable Zlib support for compression (–with-zlib-dir)
Enable XSLT and use iconv which has been installed in /usr/local/lib (–enable-xslt –with-xslt-sablot –with-iconv-dir=/usr/local/lib)
Enable Curl (–with-curl)
Enable MBString support. It is needed to support multi charachter set in MySQL and PHPMyAdmin (–enable-mbstring=all)
Use MySQL Client library which is installed in system instead of using bundled library. This is usefull if you want to use MySQL 4.1 and above because PHP’s default MySQL client library can’t be used to communicate with MySQL 4.1 and above. Please adjust this path with your own path (–with-mysql-sock=/var/run/mysql/mysql.sock –with-mysql=/usr)
Compilation and Installation
Execute above code and try it. You will have to adjust some path and if you are using Apache2, replace –with-apxs with –with-apxs2. If you don’t get any error messages, then start the compilation process by executing
This process will take some time, depending on the extensions that you have choosen to be installed. In most cases, the compilation will be finished in less than 5 minutes. When you are done with the compilation process without any error messages, you can start installing it by executing this command as root user
That’s it, the PHP itself has been installed, but the work isn’t finished yet. We still have to configure Apache to make it parse the PHP files correctly.
The first step is setting the php.ini files. By default, there are no php.ini files, so you will have to copy it manually, by executing
cp php.ini-dist /etc/php.ini
cp php.ini-recommended /etc/php.ini
You will have to adjust the directory where php.ini is located with the parameter you are using. Next, open your Apache configuration file (httpd.conf) and see Module section. Add this line if it doesn’t exist to load PHP 4 module
LoadModule php4_module libexec/libphp4.so
And in the AddModule section of httpd.conf, somewhere under the ClearModuleList, add this line if it doesn’t exist
Next, you will have to tell Apache to parse files with .php extension as PHP files by adding
AddType application/x-httpd-php .php .phtml
It’s also common to setup the .phps extension to show highlighted PHP source, this can be done with
AddType application/x-httpd-php-source .phps
After you have done all above steps, the installation process is done, but we have to test it first. Let’s restart your Apache first in order to read the new configuration file which has been modified just now. Then, create phpinfo.php file and put it in your root directory of your web server (in Slackware, it’s located in /var/www/htdocs. Please read your httpd.conf in order to find the exact location). Next, fire up your browser and open http://localhost/phpinfo.php (or any other name if you have define it) and see if the PHP screen shows up instead of downloading the PHP file. If you see the PHP configuration screen, then PHP is working great and you can start making a dynamic web pages using PHP.
If you use above script, you must be aware of one thing. If you upgrade your MySQL, you must re-configure your PHP (running the configuration script, recompile, and reinstall) again, so it would point to the correct MySQL version or else, the Apache won’t start because it won’t find the correct library version in MySQL. But, you don’t have to edit the httpd.conf again. All you need is running the above script, make, and make install and then restart your web server in order to load the new MySQL client library and your done. Simple huh??
Compiling PHP from source gives you more flexibility to add/remove extensions you needed in your development process. It can also be used to optimize your PHP behaviour and also increase your security setting. Installing PHP would never been so easy.