Multiple php versions on Ubuntu 12.10 for ZF2

This is what needs to be done in order to have possibility to run chosen apps on different php versions. It seems straightforward now, but I’ve spend whole day tweaking solutions from other blogs. This should be enough to have Zend Framework 2 app running. Also this is info extracted from http://www.bobulous.org.uk/coding/apache-php-cgi.html – thanks!

0. I assume that you have default Ubuntish apache+php install

In my case it’s php 5.4, and I unfortunately need 5.3
(apache + mod_php installed more or less by this: https://help.ubuntu.com/community/ApacheMySQLPHP)

1. Get desired php version

http://php.net/downloads.php#v5, for me it’s 5.3
extract and inside the directory

./configure --with-mysql --with-pdo-mysql
make
make test
# at some point it may complain about missing mysql library, 
# in my case it was necessary  to
sudo apt-get install libmysql15-dev
# or just post your problem here, or google it out

If you need only def 5.4 and additional 5.3 with no more options run

sudo make install

See the output where it copied php-cgi bin, and save this path for later

In case you need more php versions don’t run make install but move/remember path to php-cgi, which you may find in:

./sapi/cgi/php-cgi

2. Configure apache to allow “redirecting” workflow to this cgi file

in /etc/apache2/conf.d/ ad a file (name is up to you, I called it multiple-php.conf):

<Directory "/usr/local/bin">
    Order allow,deny
    Allow from all
</Directory>

ScriptAlias /local-bin /usr/local/bin
AddHandler application/x-httpd-php5-3 .php
Action  application/x-httpd-php5-3  /local-bin/php-cgi
  • Directory section tells apache that it is ok to use this dir (/usr/local/bin/ is a dir where php-cgi is).
  • ScriptAlias tells apache that this dir can be used for script execution.
  • AddHandler adds application/x-httpd-php-5-3 mime type to all .php files.
  • Action says that files with application/x-httpd-php5-3 mime type should be pushed to php-cgi and the output should be returned

3. Override handler for all php files in your vhost conf file (/etc/apache2/sites-available/whatever-site-to-use-5.3.localhost)

        <FilesMatch "\.(php)$">
                SetHandler application/x-httpd-php5-3
        </FilesMatch>

That’s it. Thanks to source: http://www.bobulous.org.uk/coding/apache-php-cgi.html.

4. Last tweaks

xdebug will work fine, just follow this great wizard and you’re good. In case one needs more PHP version, tell apache to use different php.ini files with xdebug paths.

I’ve noticed that php_cgi handles GET params differently. On JSON passed from my ExtJS app, on php_mod it was like:

{"param":"value"}

but on cgi, it looked like:

{\"param\":\"value\"}

So I need to preprocess this before running Json::decode, like this:

$filter = str_replace("\\\"","\"", $this->params()->fromQuery('filter'));

That’s a hack but this code works on both php_mod and php_cgi versions. Maybe there’s some conf option, to switch this params handling, but I couldn’t find it (please comment if you know the solution)

Update: gpc_magic_quotes are set to “on” by default for cgi mode. So by changing this option to Off you may get rid of the hack above. Tanks Wiktor.

Share Button

The easiest is somehow the hardest

The easiest way to increase your app quality (and tests coverage) is by adding a failing test for every bug before you attempt fixing it. This is a magic spice to fight regression.

It’s hard though to do it like described above. To fight the urge to dive in because you have the feeling (or you actually know) whats wrong. It’s harder to create test for a bug than to fix a bug.

But it’s not an accomplishment to fight a bug once. It’s a challenge to
- not to create new ones by doing so
- make it go away for good

Closing tip: at some point, when you’re skilled with testing, you start to notice you could explore the bug with tests… and you kill two birds with one stone (this is one awful idiom, isn’t it?).

Share Button

Components injecting in PHP 5.4 with traits

I wrote this in a fever of creation, take a look:

On the second look – it’s already done for instance in CakePHP (Models behaviors, Controllers component). The only new thing here is the use of traits.

At first I thougt rails is better than PHP only because of environment (bundler, dev server, migrations, testing, gems). But for that it’s easy to catch up (say coposer for a good start). What I really miss is more dynamic nature of php itself.

So I guess I was chasing that shadow ;) Anyway – maybe someone could use this example for whatever reason. Traits gives you static Aspects (or horizontal inheritance if you prefer), this piece of code is an example of how this could be achieved dynamically.

Share Button