Compile small PHP + performance messures

As you may know, I'm pretty cheap with my server. As in this referenced blog post is already mentioned, I've optimized nearly every part of the lamp stack - besides the (l)inux and (p)hp part. This post focuses on the PHP part.

Current Server + My requirements

Since my last post, the server specs got better: now I have 400mb RAM and have KVM for virtualization, which allows swap space. So I'm not so much constrained by memory anymore. But still, when my php-fpm instances ran longer, they already took 25% of my memory. A second reason to compile PHP on my own is, that I want to utilize the opcode cache, so my webpages get faster.

My requirements are simply, that all my Yii-applications and my Wordpress blog still works.

What is possible

For tuning the performance of PHP there are not many options. The few things one can do without recompiling are:

  • disable unused extensions inside the php.ini
  • limit number of processes inside php-fpm.conf

So when your PHP is compiled with soap, sqlite, etc. you can only disable it by recompiling. - Which is the focus of this article.

Compiling PHP

Getting the source and setting up the environment

Mostly I followed this guide: howtoforge. Since I already compiled some other things, the only thing which I needed to do was: "apt-get build-dep php5". I used php5.5 because I wanted the zend opcache easily installed and also to have a sneak peek in the future of PHP.

configuring

This was the most difficult part. I didn't find a list in the internet, with important things a PHP binary should be compiled with. So I had to do "try and error" until my webpages worked again. To make things easier, I didn't write the ./configure on the commandline, but in a textfile, which can be read in the following:

[gist]https://gist.github.com/balrok/5542773 [/gist] Because I don't know how to mix comment + this commandline options, the following is a commented, but not working version of my configure script: [gist]https://gist.github.com/balrok/5542806[/gist]

It took my quite long to find out, that session support was missing, because it simply stopped the script without any error logged/displayed. All other options were quite easy to recognize.

php.ini and fpm

While keeping the configuration minimal, I tried this time to have also a minimal php.ini:

short_open_tag = On
date.timezone = 'Europe/Berlin'
pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock
zend_extension=opcache.so
opcache.enable=On

The opcache.so will be automatically found so no need to define the full directory.

For php-fpm I only updated the /etc/init.d/php-fpm where I changed DAEMON=/root/php5/opt/sbin/php-fpm

Performance comparison

I just test the same webpage with different configurations - I always take only the smallest value. The db-overhead is for each configuration 21 queries with 18ms.

php5.5-fpm with opcache php5.5-fpm without opcache php5.4 from debian (without opcache)
Execution Time: 87ms 110ms 138ms
Memory Usage: 6611.32kB 9680.59kB 18105.29kB

Since I can't yet compare it to a debian php-5.5 and don't want to compile php-5.4 it is not 100% clear from what those memory and performance improvements came from.. So do your own conclusions whether this helped :)

Update: I could now compare to a php-5.5 debian package - the new post can be found here: Small PHP vs Debian PHP package.

Commentaires: