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

30 Gedanken zu „Downgrade PHP 5.5 to 5.3 in Ubuntu 14 and use both PHP Versions parallel

  1. Keith

    I have followed this tutorial to the letter, and I am still getting php 5.5.9 no matter what I do. Any suggestions? I would like to get 5.4 working on an internally used Ubuntu 14.04 for some old scripts.

  2. Marko

    Thanks for nice tutorial!
    Everithing was fine until i restart Apache after
    I get error when reatarting Apache:
    sudo service apache2 restart
    * Restarting web server apache2 [fail]
    * The apache2 configtest failed.
    Output of config test was:
    AH00526: Syntax error on line 27 of /etc/apache2/sites-enabled/cgidemo.conf:
    Invalid command ‚FastCgiServer‘, perhaps misspelled or defined by a module not included in the server configuration
    Action ‚configtest‘ failed.

    line 27, 28 in cgidemo.conf file:
    FastCgiServer /var/www/cgi-bin/php-cgi-5.3.29
    ScriptAlias /cgi-bin-php/ /var/www/cgi-bin/

    Not sure, if i have something wrong with virtual hosts or need to reinstall some module?

    1. admin Artikelautor

      @Marko

      I think Apache needs a module or configuration so you can use FastCgi-Servers. In your enviroment this command is unknown by apache.
      You should search how to use „fastcgiserver“ in apache.

  3. Danu

    Please help
    dhan@ubuntu:~$ sudo service apache2 restart
    sudo: unable to resolve host ubuntu
    * Restarting web server apache2
    AH00557: apache2: apr_sockaddr_info_get() failed for ubuntu
    AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1. Set the ‚ServerName‘ directive globally to suppress this message

    1. admin Artikelautor

      @phpcore

      You have to add the fastcgiserver to the default virtual host which is mostly located in „/etc/apache2/sites-available/default.conf“

      It must be something like this:



      ServerAdmin webmaster@localhost
      DocumentRoot /var/www

      FastCgiServer /var/www/cgi-bin/php-cgi-5.3.29
      ScriptAlias /cgi-bin-php/ /var/www/cgi-bin/


      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Require all granted

      AddHandler php-cgi .php
      Action php-cgi /cgi-bin-php/php-cgi-5.3.29

      SetHandler php-cgi

      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined


      1. phpcore

        Thank you for the quick reply.
        Another question: I have to run cron jobs with php scripts, how can I achieve this with the current settings (php-cgi 5.3.29) ?

        thanks in advance !

  4. Sonu

    Hi I am using scotch box for lamp environment by deafult php version is 5.6 when i am using these step everything is fine but error while restarting server.

    root@scotchbox:/opt/phpfarm/src# service apache2 restart
    * Restarting web server apache2
    …fail!
    * The apache2 configtest failed.
    Output of config test was:
    AH00526: Syntax error on line 27 of /etc/apache2/sites-enabled/cgidemo.conf:
    FastCgiServer: „/var/www/cgi-bin/php-cgi-5.3.29“ access for server (uid 1000, gi
    d 1000) not allowed: execute not allowed by owner
    Action ‚configtest‘ failed.
    The Apache error log may have more information.

    please help

    1. admin Artikelautor

      @Sonu

      I think this is just an a file permission error. The user which is used for apache (default: www-data) must have the rights to access „/var/www/cgi-bin/php-cgi-5.3.29“

  5. Cris

    I’m not able to install libapache2-mod-fastcgi. I have apache 2.4, and I get unmet dependencies. apach32.2-common (>=2.2.4) but it is not installable.

    I tried using mod_fastcgi instead , but I can’t figure out how to get it to work with phpfarm.

  6. gsint

    I’m getting those errors in error_cgidemo.log :

    [Tue Feb 24 17:01:59.242681 2015] [fastcgi:error] [pid 14309] [client 192.168.10.119:65336] FastCGI: comm with server „/var/www/cgi-bin/php-cgi-5.4.26“ aborted: idle timeout (30 sec)
    [Tue Feb 24 17:01:59.242768 2015] [fastcgi:error] [pid 14309] [client 192.168.10.119:65336] FastCGI: incomplete headers (0 bytes) received from server „/var/www/cgi-bin/php-cgi-5.4.26″

    this is my php-cgi-5.4.26 file
    #!/bin/sh
    #PHPRC=“/etc/php5/cgi/5.4.26/“
    PHPRC=“/opt/phpfarm/inst/php-5.4.26/lib/php.ini“
    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.4.26/bin/php-cgi

  7. admin Artikelautor

    If you need GD Graphics library:

    1. Install libpng12-dev:

    sudo apt-get install libpng12-dev
    sudo apt-get install libjpeg8-dev

    2. Edit compile options:

    cd /opt/phpfarm/src
    sudo nano custom-options-5.3.29.sh

    Add newlines just before „$gcov“

    --with-gd=yes \
    --enable-gd-native-ttf \
    --with-jpeg-dir=/usr \
    --with-png-dir=/usr

Schreibe einen Kommentar