Multiple php versions simultaneously on Ubuntu

21,687

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.

Share:
21,687

Related videos on Youtube

SeeDoubleYou
Author by

SeeDoubleYou

Updated on September 18, 2022

Comments

  • SeeDoubleYou
    SeeDoubleYou almost 2 years

    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.

    • Admin
      Admin over 11 years
      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.
    • Admin
      Admin over 11 years
      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
    • Admin
      Admin over 11 years
      I've updated the post to reflect these changes since I think is good to have it like that anyway
    • Admin
      Admin over 11 years
      Hmm, I'll check for other posibilities then. :)
    • Admin
      Admin over 11 years
      I was wondering, probably I don't need the suexec stuff since I don't need different users for each php version right?
    • Admin
      Admin over 11 years
      You wouldn't believe how stupid I feel at this point. It works, and it would't because I used <? instead of <?php and I did not set short open tag on. Anyway, it works as described. Thank you all for thinking along!
    • Admin
      Admin over 11 years
      Ha! Nice catch! Good to know the tutorial still works. ;)
    • SeeDoubleYou
      SeeDoubleYou over 11 years
      There is however one more strange thing. localhost/phpmyadmin doesn't go to phpmyadmin anymore. Instead it just outputs the raw index.php file. I think I remember reading somewhere that suexec doesn't allow for anything to be outside the /var/www folder (or actually the root set for suexec and it doesn't allow symlinks. Php is of course outside this folder since it is an install from the ubuntu repositories.
    • SeeDoubleYou
      SeeDoubleYou over 11 years
      Phpmyadmin is "fixed" when I add AddHandler php-cgi .php and Action php-cgi /php-fcgi/php5317.fcgi to its apache.conf. The things is now that I cannot login to mysql from phpmyadmin. I don't know whether this has anything to do with this setup, but it did work before I started with the whole fastcgi part
    • SeeDoubleYou
      SeeDoubleYou over 11 years
      Actually, mysql has stopped working completely, it fails with this error [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock).