Compiling PHP from source code can achieve many things, more efficient instance of php, reducing the footprint by only having what you need etc. So here is a walk through of how to do just that.
First of you need some packages that will do the configuration, compiling etc. I use Debian for my OS, so the packaging manager is
apt which would be the same for Ubuntu, but for CentOS and Fedora you'll need to use
yum. The package names with yum maybe slightly different than for apt.
Code:
$ apt-get install gcc g++ make autoconf patch flex
You could just start compiling from here, but there are a few extra steps which will make php compile better suited to your hardware. You first need to find out what type of CPU is installed, which you can see by looking at /proc/cpuinfo
Code:
$ cat /proc/cpuinfo | grep ^model\ name
model name : Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz
model name : Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz
model name : Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz
model name : Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz
This tells you that there is an
Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz installed and that there are four of them, based on your CPU you need to set to environment variables. There is a handy page for gentoo which applies the same here [ur]http://gentoo-wiki.com/Safe_Cflags[/url].
The environment variables for my
VPS Hosting are
Code:
CHOST="i686-pc-linux-gnu"
CFLAGS="-march=prescott -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=prescott -O2 -pipe -fomit-frame-pointer"
You can set these using export in your current shell session, or if you want to keep them for every session add them to /etc/profile for all users and /root/.bashrc for root.
There is one last thing to do make the compiling faster,
make accepts an option to do parallel jobs -j#. This is personal preference, but I set mine to 2 more than the number of CPU's, so -j6. So to set this I create an alias, again for this session on the command line is fine, but for every session edit the same files as for environment variables.
Code:
alias make='make -j6'
Now you can actually get into compiling.
Download php from
www.php.net, currently version 5.2.5, extract the source code and change into the new directory.
Code:
$ wget http://uk3.php.net/get/php-5.2.5.tar.bz2/from/uk.php.net/mirror
$ tar jxf php-5.2.5.tar.bz2
$ cd php-5.2.5
Inside this directory is a script which sets up your configuration, called configure. If you run that with --help as a parameter you will see the available options to you for compiling. To keep this simple, I will give an example for compiling for
apache2, mysql, gd and soap, to add in more it's pretty much more of the same.
Here I will be installing php into /usr/local/php, pear directory as /usr/local/php/pear and php.ini to be located in /etc/php, which is done with the following options.
Code:
--prefix=/usr/local/php --with-pear=/usr/local/php/pear --with-config-file-path=/etc/php
Now you need to find apache extension tool, apxs, which can be located with the
which command
Code:
$ which apxs
/usr/sbin/apxs
so to add apache2 support, the following option is used
Code:
--with-apxs2=/usr/sbin/apxs
For mysql, you need to know where MySQL Server libraries are installed (the lib/mysql directory), which is normally in /usr, but you can check this with
mysql_config
Code:
$ which mysql_config
$ /usr/bin/mysql_config
Usage: /usr/bin/mysql_config [OPTIONS]
Options:
--cflags [-I/usr/include/mysql]
--include [-I/usr/include/mysql]
--libs [-L/usr/lib/mysql -lmysqlclient -lz -lcrypt -lnsl -lm]
--libs_r [-L/usr/lib/mysql -lmysqlclient_r -lz -lpthread -lcrypt -lnsl -lm -lpthread]
--socket [/tmp/mysql.sock]
--port [3306]
--version [5.0.51]
--libmysqld-libs [-L/usr/lib/mysql -lmysqld -lz -lpthread -lcrypt -lnsl -lm -lpthread -lrt]
You can see that the libraries are installed in /usr. So now the following commands will add mysql, mysqli and pdo support into php
Code:
--with-mysql=/usr --with-mysqli=/usr/bin/mysql_config --with-pdo-mysql=/usr
After that, it's just adding gd, which fortunately is bundled with php source code, so it just need to install the header files for png and jpeg support. You can add more image types, [ur]http://www.php.net/gd[/url] explains further options.
Code:
$ apt-get install libpng12-dev libjpeg62-dev
This will install the headers into /usr, so now the following options will add gd support.
Code:
--with-gd --with-png-dir=/usr --with-jpeg-dir=/usr
Soap depends on libxml, so you first need to install the library.
Code:
$ apt-get install libxml-dev
The options to include soap is
So to configure php with all these options, you run
Code:
./configure --prefix=/usr/local/php --with-pear=/usr/local/php/pear --with-config-file-path=/etc/php \
--with-apxs2=/usr/sbin/apxs \
--with-mysql=/usr --with-mysqli=/usr/bin/mysql_config --with-pdo-mysql=/usr \
--enable-soap
Once you have ran this command, you should see something like
Code:
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
Now you can compile and install php
Code:
$ make
$ make install
This should automatically configure apache to use php, but to make sure, check your httpd.conf file has the following
Code:
LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
The last line is optional, having it will make .phps files render as code with syntax high-lighting in your browser.
Then you just need to restart apache
Code:
$ apachectl restart
Hope this is helpful you all of you.
