16

I want to be able to run multiple php versions on my development box running Ubuntu 12.04. What I want to accomplish is that when I use localhost as a domain a default is used (let's say php 5.3.17). When I use 547.localhost as domain php 5.4.7 is used. I've seen some tutorials to get this working using fastcgi but until now I haven't been able to get it to work. I've looked at these tutorials:

  1. http://dbforch.wordpress.com/2010/05/21/apache2-fastcgi-multiple-php-versions-ubuntulucid-10-04/
  2. http://www.metod.si/multiple-php-versions-with-apache-2-fastcgi-phpfarm-on-ubuntu/

For as far as I can see I have done everything that is needed. The problem is that php simply doesn't run. When I go to http://localhost/somephpfile.php it just outputs the source of the php file. The same for http://547.localhost/somephpfile.php.

I'll break down what steps I took in the hope that someone is able to spot what I missed.

  1. First I installed a default lamp stack using sudo apt-get install lamp-server^ phpmyadmin. After this, I had a working development server running the repository version of php.

  2. Then I used phpfarm to create two php installs, one for 5.3.17 and one for 5.4.7. The localion of phpfarm is /etc/php/phpfarm, so the executables are in /etc/php/phpfarm/inst/php-{version}/bin

  3. Then I enable suaxec and fastcgi for apache and disable mod_php with sudo a2enmod fastcgi actions suexec && sudo a2dismod php5

  4. Next, I edited /etc/apache2/mods-enabled/fastcgi.conf to read:

    <IfModule mod_fastcgi.c> FastCgiIpcDir /var/lib/apache2/fastcgi FastCgiWrapper /usr/lib/apache2/suexec FastCgiConfig -idle-timeout 110 -killInterval 120 -pass-header HTTP_AUTHORIZATION -autoUpdate ScriptAlias /php-fcgi/ /var/www/cgi-bin/ </IfModule> 
  5. Then in /var/www/ I created a folder cgi-bin and in this folder two files, for each of the two php versions as follows (I show only the one for 5.3.17 /var/www/php5317.fcgi):

    #!/bin/sh # you can change the PHP version here. version="5.3.17" # php.ini file location, */php-5.2.13/lib equals */php-5.2.13/lib/php.ini. PHPRC=/etc/php/phpfarm/inst/php-${version}/lib/php.ini export PHPRC PHP_FCGI_CHILDREN=3 export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=5000 export PHP_FCGI_MAX_REQUESTS # which php-cgi binary to execute exec /etc/php/phpfarm/inst/php-${version}/bin/php-cgi 
  6. The last step was to create virtual hosts. In the end I have three files in /etc/apache2/sites-enabled: 000-default, php5.3.17 and php5.4.7 With the following contents:

    default:

    <VirtualHost *:80> ServerName localhost DocumentRoot /var/www <Directory "/var/www"> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all AddHandler php-cgi .php Action php-cgi /php-fcgi/php5317.fcgi </Directory> </VirtualHost> 

    php5.3.17:

    <VirtualHost *:80> ServerName 5317.localhost DocumentRoot /var/www <Directory "/var/www"> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all AddHandler php-cgi .php Action php-cgi /php-fcgi/php5317.fcgi </Directory> </VirtualHost> 

    php5.4.7:

    <VirtualHost *:80> ServerName 547.localhost DocumentRoot /var/www <Directory "/var/www"> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all AddHandler php-cgi .php Action php-cgi /php-fcgi/php547.fcgi </Directory> </VirtualHost> 
  7. Finally I changed /etc/hosts to read

    127.0.0.1 localhost 127.0.0.1 547.localhost 127.0.0.1 5317.localhost # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 

Now I would expect things to work, but sadly they don't. Instead of that, a php file runs through php it just outputs the raw file.

There must be something I missed here, but I have gone through the process many times and I can't figure out where it goes wrong.

10
  • 1 thing I encountered was that if I named the cgi files like you did it did not work. Try renaming /var/www/cgi-bin/php-cgi-5.4.7 to /var/www/cgi-bin/php54.fcgi for example or just /var/www/cgi-bin/php.fcgi. Can u check if that helps? Just for the 5.4.7 version. Commented Sep 25, 2012 at 12:14
  • I have checked and unfortunately this doesn't help. I have changed the virtualhost to reflect this change as well so the the action line read Action php-cgi /php-fcgi/php547.fcgi Commented Sep 25, 2012 at 12:21
  • I've updated the post to reflect these changes since I think is good to have it like that anyway Commented Sep 25, 2012 at 12:31
  • Hmm, I'll check for other posibilities then. :) Commented Sep 25, 2012 at 12:32
  • I was wondering, probably I don't need the suexec stuff since I don't need different users for each php version right? Commented Sep 25, 2012 at 13:34

2 Answers 2

2

In your virtualhosts you added a handler, but you didn't set the handler which is why it isn't processed. Add:

<FilesMatch "\.php$"> SetHandler php-cgi </FilesMatch> 

before </Directory> in each virtual hosts file.

0

I liked this short step-by-step overview by SeeDoubleYou and wanted to try it myself. What was missing was

  • sudo chmod +x /var/www/cgi-bin/*
    • Since this wasn't executed, apache wasn't able to execute the cgi scripts
    • This is needed for the line Action php-cgi /php-fcgi/php<version>.fcgi in the vhost.conf file
  • sudo apt-get install apache2-suexec-pristine libapache2-mod-fastcgi
    • suexec executable and fastcgi module were missing, they had to be installed first
    • The suexec executable is needed for the line FastCgiWrapper /usr/lib/apache2/suexec in the fastcgi.conf file
  • I also had to remove the line FastCgiIpcDir /var/lib/apache2/fastcgi
    • otherwise I got the error message FastCgiIpcDir /var/lib/apache2/fastcgi: already defined as "/var/lib/apache2/fastcgi"

For anyone trying to set php_admin_value and php_value via .htaccess or vhosts files: This isn't possible with FCGI. Instead you can use .user.ini files to set directory specific php.ini configurations.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.