Multiple PHP versions with Apache 2, FastCGI, PHPFarm on Ubuntu

This tutorial is a part of the “Setting up your local development environment” series. It is based on this article while keeping in mind that in previous apache tutorial we compiled apache from source. So I’m definitely not taking all the credit for it. But there are some modifications.
Starting with PHPFarm, we need Git installed in order to check it out. If you don’t have it installed:
sudo apt-get install git-core
Now create a directory in which PHP versions will reside. The directory of my liking is /usr/local/php
, so I will use that. You can of course change that.
cd /usr/local
Clone the phpfarm repository.
git clone git://git.code.sf.net/p/phpfarm/code php
Now we have to edit the configuration options which phpfarm will use to compile php.
cd php/src
We can use more general configuration options or very strict ones. Default configuration options.sh can be overridden by creating a file called custom-options.sh
or a bit more specific like custom-options-5.2.sh
or very strict like custom-options-5.3.18.sh
. Since the configurations for 5.2 and 5.3 are different and I like to be a strict person, I will create two configurations:
- custom-options-5.2.17.sh
- custom-options-5.3.18.sh
custom-options-5.2.17.sh contents (these config values were also mentioned in previous tutorial):
configoptions="
--enable-cli \
--with-pear \
--with-openssl=/usr \
--with-iconv \
--with-curl \
--with-mysqli \
--enable-mbstring \
--enable-exif \
--with-jpeg-dir=/usr \
--with-zlib \
--with-zlib-dir \
--with-png-dir=/usr \
--with-gd \
--with-gettext \
--enable-gd-native-ttf \
--with-mhash \
--enable-ftp \
--with-pspell \
--with-mcrypt \
--enable-bcmath \
--with-mime-magic \
--with-pdo-mysql \
--enable-sockets \
--enable-soap \
--enable-calendar \
--enable-fastcgi \
--enable-force-cgi-redirect \
"
custom-options-5.3.18.sh contents:
configoptions="
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-cli \
--with-pear \
--with-openssl=/usr \
--with-iconv \
--with-curl \
--enable-mbstring \
--enable-exif \
--with-zlib \
--with-zlib-dir \
--with-gd \
--with-gettext \
--enable-gd-native-ttf \
--with-mhash \
--enable-ftp \
--with-pspell \
--with-mcrypt \
--enable-bcmath \
--enable-sockets \
--enable-soap \
--enable-calendar \
--with-png-dir=/usr \
--with-jpeg-dir=/usr
"
For PHP 5.3 we don’t need the cgi parameters, since these are already enabled by default.
On Ubuntu 11.04, the configuration might fail with error messages about libjpeg and/or libpng. What you can do is the following:
sudo apt-get install libjpeg8-dev
Install the libjpeg8-dev package. This will solve the libjpeg error message.
sudo ln -s /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/libpng.so
Create a symbolic link to the libpng.so in the /usr/lib directory. This will solve the libpng error message.
Next step – compiling PHP.
sudo ./compile.sh 5.2.17
sudo ./compile.sh 5.3.18
Where the number after the ./compile.sh
is the PHP version you wish to compile.
Now that we have compiled PHP, we can move on to set up apache. If you followed the apache tutorial, first of we need to recompile it.
./configure \
--prefix=/usr/local/apache2.2.22 \
--with-included-apr \
--enable-mods-shared=all \
--enable-so \
--enable-mod-rewrite \
--enable-suexec \
--with-suexec-caller=daemon
These are just my configuration options. The options you have to pay attention to for this topic are:
- –enable-so
- –enable-suexec
- –enable-suexec-caller=daemon – You can change this according to which user your apache runs under.
make
sudo make install
Apache should be up and running. Next stop – FastCGI module.
Download and extract the latest (current) source from http://www.fastcgi.com/dist/.
tar xzf mod_fastcgi-current.tar.gz
cd mod_fastcgi-2.4.6
Next thing to do is rename Makefile.AP2 to Makefile, so we can compile it for apache 2.
cp Makefile.AP2 Makefile
Compile and install the module.
make top_dir=/usr/local/apache2.2.22
sudo make install top_dir=/usr/local/apache2.2.22
The top_dir
parameter is optional. If you compiled apache to the more usual location like /usr/local/apache2
, you do not need to set this option.
Now that we have mod_fastcgi module compiled, we can load it as a shared object. Add the following line to your httpd.conf:
LoadModule fastcgi_module modules/mod_fastcgi.so
If you had previously ran PHP as an apache module, you must disable it. You can only run PHP either as CGI or an apache module. So in httpd.conf, comment out this line:
# LoadModule php5_module modules/libphp5.so
Create a .conf file (you can call it what you want) – i.e. php-fcgi.conf and put the following content inside:
<IfModule mod_fastcgi>
FastCgiWrapper /usr/lib/apache2.2.22/bin/suexec
FastCgiConfig -idle-timeout 110 -killInterval 120 -pass-header
HTTP_AUTHORIZATION -autoUpdate
ScriptAlias /php-fcgi/ /var/www/cgi-bin/
</IfModule>
And include it in your main httpd.conf:
# Include FastCGI configuration
Include conf/php-fcgi.conf
Now create a directory for CGI scripts.
sudo mkdir /var/www/cgi-bin
And create scripts.
sudo nano /var/www/cgi-bin/php-cgi-5.2.17
sudo nano /var/www/cgi-bin/php-cgi-5.3.18
And put something like the following in. Just change the version of PHP.
#!/bin/sh
version="5.3.18"
PHPRC=/usr/local/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 /usr/local/php/phpfarm/inst/php-${version}/bin/php-cgi
These scripts should be executable. So we set permissions to 0755.
cd /var/www/cgi-bin
sudo chmod +x *
Now we configure apache virtualhosts. Each virtualhost will have it’s own subdomain and PHP version.
NameVirtualHost localhost.53:80
<VirtualHost localhost.53:80>
ServerName localhost.53
DocumentRoot /var/www
<Directory "/var/www">
AddHandler php-cgi .php
Action php-cgi /php-fcgi/php-cgi-5.3.18
</Directory>
</VirtualHost>
Do the same for all versions of PHP.
We are almost there. Since these subdomains (asumingly) do not exist yet, we must point them to your localhost IP.
sudo nano /etc/hosts
Just add your subdomains after the localhost definition. It should look something like this:
127.0.0.1 localhost localhost.52 localhost.53
Everything is set, so just restart the apache.
sudo /usr/local/apache2.2.22/bin/apachectl -k restart
Put a file phpinfo.php in /var/www directory with the following line:
And test it. If everything went well, you should see different PHP versions on different subdomains.
- http://localhost.52/phpinfo.php – PHP 5.2.17
- http://localhost.53/phpinfo.php – PHP 5.3.18
Enjoy!
Post updated on November 12th, 2012th to reflect latest updates.
Hey, i hope you can help me.. i have follow your tutorials. with phpfarm. if i compile version php 5.3.5 after i have make a custom-options-5.3.sh.
the error there are come on the console is.
web02:/etc/php/phpfarm/src# ./compile.sh 5.3.5
5.3.5 5 3 5
Segmentation fault
my custom-options file is.
web02:/etc/php/phpfarm/src# nano custom-options-5.3.sh
#gcov=’–enable-gcov’
configoptions=”\
–enable-bcmath \
–enable-calendar \
–enable-exif \
–enable-ftp \
–enable-mbstring \
–enable-pcntl \
–enable-soap \
–enable-sockets \
–enable-sqlite-utf8 \
–enable-wddx \
–enable-zip \
–with-zlib \
–with-gettext \
–with-mysql=/usr/bin/mysql_config \
–with-mysqli=shared,mysqlnd \
–with-pdo-mysql=shared,mysqlnd \
$gcov”
i hope you can help me.
sorry about my english
Hey,
I can try. 🙂
These are the things I would do:
* Try to set the configoptions without the $gcov, just include the –enable-gcov at the end.
* Try to eliminate all the configuration, just leave one like –enable-bcmath, so you can see if one of those options triggers the segmentation fault.
* If that fails you can always just compile PHP manually which I described here – http://www.metod.si/setting-up-your-local-development-environment-php/ But then you must adjust a thing or two like the path to your php.ini and php-cgi binary.
I hope one of these points help you out! 😉
sudo ./compile.sh 5.2.17 returns this error
5.2.17 5 2 17
configure: warning: –enable-cli: invalid host type
configure: warning: –with-pear: invalid host type
configure: error: can only configure for one host and one target at a time
configure.sh failed.
I couldn’t get this to work..
I’ve try to remove all compile options that doesn’t work like –with-openssl and at the end I get this message.
PHP Warning: Unexpected character in input: ‘\’ (ASCII=92) state=1 in
/usr/local/php/inst/php-5.2.17/pyrus.phar on line 50
Looks like pyrus.phar doesn’t work with php 5.2
Any help wold be appreciate
Pyrus only works for PHP 5.3+, yes.
As for the invalid host type error: Check that you don’t have any whitespaces between and that you typed the options correctly. It’s not how you see it here – I should fix it – the browser transforms -- into –.
So the option –with-openssl should actually be --with-openssl and so on 🙂
I am having some trouble with php 5.3 installations. My joomla startup check tells me, that there is no mysql as well as no zlib extension installed.
Do you have any idea what might be going wrong here? Any help is greatly appreciated!
Can you post your configure options? And did any error occur during the compilation?
No, there were no errors at all. This is my config:
configoptions=”\
–with-mysql=mysqlnd \
–with-mysqli=mysqlnd \
–with-pdo-mysql=mysqlnd \
–enable-cli \
–with-pear \
–with-openssl=/usr \
–with-iconv \
–with-curl \
–enable-mbstring \
–enable-exif \
–with-zlib \
–with-zlib-dir \
–with-gd \
–with-gettext \
–enable-gd-native-ttf \
–with-mhash \
–enable-ftp \
–with-pspell \
–with-mcrypt \
–enable-bcmath \
–enable-sockets \
–enable-soap \
–enable-calendar \
–with-png-dir=/usr \
–with-jpeg-dir=/usr \
“
Hmmm, check your phpinfo() output. If the page says you have zlib and mysql support, then the problem is in Joomla.
Thanx for the tutorial, very helpful.
To get around the error messages of libjpeg I also had to run:
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/libjpeg.so
[…] http://www.metod.si/multiple-php-versions-with-apache-2-fastcgi-phpfarm-on-ubuntu/ […]
Thanks for the tutorial. I have it working on my Ubuntu Server 12.04LTS setup.
However, I’m totally new to Linux and I now have a totally newb question: How can I access the sites served by this installation over my home network?
I’m pretty sure something is not set up correctly, as I get two different results when accessing the machine by name or by IP in my browser, but neither of them are correct – access by name shows the same index as visiting localhost on the machine itself, except it’s plain text, not served PHP, and accessing by IP shows the apache htdocs/index.html
You should probably add another VirtualHost with the same configuration as you have for (i.e. localhost.53) with the name of the domain you want it accessible by.
Like if you have a this-is-my-domain53.com, make another NameVirtualHost this-is-my-domain.com53:80 and then …
I hope you get the idea.
Thanks for the help, Metod. However, I’m still having a problem. I will understand if you don’t want to get into this any further, so feel free to say so!
On my windows machine I have this in my hosts file: 192.168.0.104 test.com53
Then on my ubuntu machine I have this in my hosts file: 127.0.0.1 test.com53
Finally, in my vhosts file I have:
NameVirtualHost test.com53:80
ServerName test.com53
DocumentRoot /var/www/html/_test
AddHandler php-cgi .php
Action php-cgi /cgi-bin/php-cgi-5.3.28
SetHandler php-cgi
When I use: wget -O ~/test.html test.com53 from the ubuntu machine, the test.html file shows the correct content, but when I try to visit test.com53 from my windows machine, I see the index.php file in /var/www, and it is not being served as php, just plaintext.
Hi all,
I do use Unbuntu-server 12.04 on VPS.
Php by default is 5.3.10, but I need also Php 5.2.17.
I’m trying install Phpfarm, but:
/opt/phpfarm/src# ./compile.sh 5.2.17
Returns errors :
5.2.17 5 2 17
configure: warning: \: invalid host type
configure: warning: \: invalid host type
configure: error: can only configure for one host and one target at a time
configure.sh failed.
Please, could you help me ?
Gilles
[…] http://www.metod.si/multiple-php-versions-with-apache-2-fastcgi-phpfarm-on-ubuntu/ […]
[…] http://www.metod.si/multiple-php-versions-with-apache-2-fastcgi-phpfarm-on-ubuntu/ […]
anyone experience 404 not found issue ? most likely on ajax links ?
hope anyone can help me.
[…] – How to run multiple versions of PHP on the same computer Multiple PHP versions with Apach2, FastCGI, PHPFarm on Ubuntu Setting up your local development environment […]
[…] See for the complete installation the linked question. Don’t forget to look at the two links in the header as well, they look like nice tutorials (but less compressed). This and That […]
[…] – How to run multiple versions of PHP on the same computer Multiple PHP versions with Apach2, FastCGI, PHPFarm on Ubuntu Setting up your local development environment […]
Something is wrong.. it doesn’t work with -enable and -with at all , always gives errors. Something changed? I was doing this on Ubuntu 15. I had delete all options. Made only: configoptions=”\”
Ok first of all. New PHP have diffrent -enable mods during the configure. Best way is to enter the PHP-$version catalog and give ./configure –help to test what kind of options are avalaible. Second after compile important thing is to enable cgi mod by $ a2enmod cgi fastcgi / I had only fastcgi mod enabled And it didn’t worked for me. After enabling cgi it worked.
Hi, thanks for the tutorial.
After all the steps completed successfully, I get this error when accessing the website:
Not Found
The requested URL /php-fcgi/php-cgi-5.2.17/phpinfo.php was not found on this server.
Apache/2.4.18 (Ubuntu) Server at http://www.felipeescolta.com.br Port 80
Here are my configuration files:
cat /etc/apache2/conf-enabled/php-fcgi.conf
FastCgiWrapper /usr/lib/apache2/suexec
FastCgiConfig -idle-timeout 110 -killInterval 120 -pass-header
HTTP_AUTHORIZATION -autoUpdate
ScriptAlias /php-fcgi/ /var/www/cgi-bin/
cat /etc/apache2/sites-enabled/php5217.conf
ServerAdmin postmaster@website.com.br
ServerName http://www.website.com.br
DocumentRoot /var/www/website
Options FollowSymLinks
AllowOverride All
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
AddHandler php-cgi .php
Action php-cgi /php-fcgi/php-cgi-5.2.17
LogLevel warn
cat /var/www/cgi-bin/php-cgi-5.2.17
#!/bin/sh
version=”5.2.17″
PHPRC=/usr/local/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 /usr/local/php/phpfarm/inst/php-${version}/bin/php-cgi
root@ubuntu:/var/www#
Any help is appreciated, thanks