Kategorie-Archiv: Wiki

Downgrade PHP 5.5 to 5.3 in Ubuntu 14 and use both PHP Versions parallel

In this tutorial we will parallel install PHP as fastCGI and as mod_php (default) on Ubuntu 14 using phpfarm. We will first download and compile PHP 5.3.29 (or any other version you like) and then configure a virtual host with fastCGI.

downgrade_php5.5_to_5.3_parallel_fastcgi_ubuntu14

Note: We assume that you have installed LAMP with PHP 5.5x, MySQL and Apache2 using mod_php. If not please first install it. There are many tutorials on how to install LAMP on Ubuntu 14!

1. Updating system

sudo apt-get update
sudo apt-get ugrade
sudo apt-get install build-essential

Note: You can leave out „sudo apt-get upgrade“, if you dont want to upgrade your system.

Install some libs needed for PHP Compilation:
sudo apt-get install libxml2 libxml2-dev libssl-dev
sudo apt-get install libcurl4-openssl-dev pkg-config
sudo apt-get install libcurl4-gnutls-dev libjpeg-dev libpng12-dev libmysqlclient-dev

2. Download phpfarm via git

sudo apt-get install git
cd /opt
sudo git clone https://github.com/cweiske/phpfarm

Note: If „git clone“ doesn’t work, download phpfarm here and manually extract it to the right location.

3. Configure PHP 5.3.29 compile options

PHP compile options are setted in „options.sh“ but can be overwritten by a custom options file. We will create a new custom file in this case.
cd /opt/phpfarm/src
sudo nano custom-options-5.3.29.sh

Type in or copy this:

#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 \
--disable-debug \
--with-mysql \
--with-zlib \
--with-gettext \
--with-pdo-mysql \
--with-curl \
--with-openssl \
$gcov"

4. Compile PHP and verify it

cd /opt/phpfarm/src
sudo ./compile.sh 5.3.29

Verify installation:
cd /opt/phpfarm/inst/bin
./php-5.3.29 --version

5. Install FastCGI

sudo apt-get install libapache2-mod-fastcgi apache2-mpm-worker apache2-suexec
sudo a2enmod actions fastcgi suexec
sudo service apache2 restart

sudo mkdir /var/www/cgi-bin
cd /var/www/cgi-bin
sudo nano php-cgi-5.3.29

Put the following code into „php-cgi-5.3.29“:

#!/bin/sh
PHPRC="/etc/php5/cgi/5.3.29/"
export PHPRC
PHP_FCGI_CHILDREN=3
export PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_MAX_REQUESTS
exec /opt/phpfarm/inst/bin/php-cgi-5.3.29

Make this file executable and chown rights to apache user („www-data“ by default):
sudo chmod +x /var/www/cgi-bin/php-cgi-5.3.29
sudo chown -R www-data:www-data /var/www/cgi-bin

6. Create and configure virtual host

Create a new virtual host „cgidemo“
cd /etc/apache2/sites-available
sudo nano cgidemo.conf

Put this into „cgidemo.conf“:

<VirtualHost *:80>
	 ServerName mysite.dev
	 DocumentRoot /var/www/mysite
	 ErrorLog /var/log/apache2/error_log
	 LogLevel debug
	 CustomLog /var/log/apache2/access_log combined

	 <Directory "/var/www/mysite">
		Options Indexes FollowSymLinks MultiViews
		AllowOverride All
		Require all granted
	  </Directory>
</VirtualHost>

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	ServerName cgidemo.dev
	DocumentRoot /var/www/cgidemo

	#php-cgi setup
	#used for multiple php versions
	<Directory /var/www>
		Options Indexes FollowSymLinks
		AllowOverride None
		Require all granted
	</Directory>
	FastCgiServer /var/www/cgi-bin/php-cgi-5.3.29
	ScriptAlias /cgi-bin-php/ /var/www/cgi-bin/

	<Directory "/var/www/cgidemo">
		Options Indexes FollowSymLinks MultiViews
		AllowOverride All
		Require all granted

	    	AddHandler php-cgi .php
	    	Action php-cgi /cgi-bin-php/php-cgi-5.3.29
	    	<FilesMatch "\.php$">
	      		SetHandler php-cgi
	    	</FilesMatch>
  	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error_cgidemo.log
	CustomLog ${APACHE_LOG_DIR}/access_cgidemo.log combined
</VirtualHost>

Add the domains to /etc/hosts
sudo nano /etc/hosts

Add:
127.0.0.1 cgidemo.dev
127.0.0.1 mysite.dev

Enable site and restart apache

sudo a2ensite cgidemo.conf
sudo service apache2 restart

If you got an error on Apache complaining about mod_fast already defined go /etc/apache2/mods-available/fastcgi.conf and comment out the last line inside the IfModule block:

<IfModule mod_fastcgi.c>
  AddHandler fastcgi-script .fcgi
  #FastCgiWrapper /usr/lib/apahce2/suexec
  #FastCgiIpcDir /var/lib/apache2/fastcgi
</IfModule>

7. Test with phpinfo()

Create new file „index.php“ in „/var/www/cgidemo/“ and „/var/www/mysite“:
sudo mkdir /var/www/cgidemo/
sudo nano /var/www/cgidemo/index.php
sudo chown -R www-data:www-data /var/www/cgidemo

and

sudo mkdir /var/www/mysite/
sudo nano /var/www/mysite/index.php
sudo chown -R www-data:www-data /var/www/mysite

Put this into index.php files

 
<?php phpinfo(); ?> 

Go to browser, open 2 tabs and type in domains:

mysite.dev

and

cgidemo.dev

If everything works, you should see one tab with PHP 5.5.x and the Server API „Apache 2 Handler“ and one tab with PHP 5.3.29 with Server API „CGI/FastCGI“.

If you want to install more then 2 PHP versions just compile your version with phpfarm compile script and configure the virtual host.

DONE!

Sources:

https://gist.github.com/gmodarelli/5887778
https://github.com/cweiske/phpfarm
http://cweiske.de/tagebuch/Introducing%20phpfarm.htm