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 --with-openssl --with-curl
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

Leave a Reply

Your email address will not be published. Required fields are marked *