Skip to content

Instantly share code, notes, and snippets.

@manoj-nandakumar
Last active November 25, 2016 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-nandakumar/d591acd076aa3d1661fdad60474b72dd to your computer and use it in GitHub Desktop.
Save manoj-nandakumar/d591acd076aa3d1661fdad60474b72dd to your computer and use it in GitHub Desktop.
The Gist describes how to install and use multiple versions of php with Apache in Ubuntu 14.04
**Step 1** – Install supporting necessary libraries:
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 libmysqlcli
**Step 2** – Install the PHPFarm
We grab the PHPFarm from PHPFarm Git repo
cd ~
sudo git clone https://github.com/cweiske/phpfarm
This will clone PHPFarm at ~/phpfarm directory
Once, we have the PHPFarm, lets compile some version of PHP.
**Step 3** – Custom Configurations
First, we specify some custom configurations for new php version. Go to the ‘src’ directory of PHPFarm and create file of certain naming
cd ~/phpfarm/src
sudo nano custom-options-5.3.29.sh
For some example of custom options, please see AmacGregor Git Repo
In our case, we use the following custom options by inserting them in the ~/phpfarm/src/custom-options-5.3.29.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 \
--disable-debug \
--with-mysql \
--with-zlib \
--with-gettext \
--with-pdo-mysql \
--with-curl \
--with-openssl \
$gcov"
**Step 4** – Compile PHP
Next, we compile new PHP version with our custom options:
cd ~/phpfarm/src
sudo ./compile.sh 5.3.29
This will generate new version of PHP 5.3.29. Afterwards, test that it is working
cd ~/phpfarm/inst/bin
./php-5.3.29 --version
**Step 5** – Install supporting modules of PHPFarm for Apache
The FastCGI is technology to incorporate PHPFarm into Apache, so we are installing supporting libraries
sudo apt-get install libapache2-mod-fastcgi apache2-mpm-worker apache2-suexec
sudo a2enmod actions fastcgi suexec
sudo service apache2 restart
After restarting Apache service, the newly installed modules are included
**Step 6** – Mapping CGI to PHPFarm
Next, we create web directory with configurations to map each PHP Version to the CGI service of Apache
sudo mkdir /var/www/cgi-bin
cd /var/www/cgi-bin
sudo nano php-cgi-5.3.29
Afterwards, we are adding mapping in form of Bash script named php-cgi-5.3.29 as follows
#!/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 /home/tools/phpfarm/inst/bin/php-cgi-5.3.29
As you see the last line it maps to our PHP version. The variable “PHPRC” also specifies the location of php.ini file to use. By default, it uses the php.ini in the dir of /home/tools/phpfarm/inst/php-cgi-5.3.29/lib.php
Make our file of mapping is 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
**Step 7** – Configure Virtual Host of Apache
At last, we configure particular application via Virtual Host Configurations at Apache to use the PHPFarm and our certain version of PHP by editing /etc/apache2/sites-enabled/some-domain.conf
<VirtualHost *:80>
DocumentRoot /var/www/
ServerName minnehaha.com
ServerAlias www.minnehaha.com
#LogLevel debug
#FastCgiServer /var/www//cgi-bin/php-cgi-5.3.29
FastCgiServer /var/www//cgi-bin/php-cgi-5.4.27
ScriptAlias /cgi-bin /var/www/cgi-bin/
<Directory "/var/www/">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Require all granted
AddHandler php-cgi .php
Action php-cgi /cgi-bin/php-cgi-5.3.29
<FilesMatch "\.php$">
SetHandler php-cgi
</FilesMatch>
</Directory>
<Directory "/var/www//cgi-bin">
Order allow,deny
Allow from all
Require all granted
</Directory>
Errorlog "${APACHE_LOG_DIR}/error.txt"
CustomLog "${APACHE_LOG_DIR}/customLog.txt" common
</VirtualHost>
Here, all the highlighted lines involve configuring the PHPFArm. The line 7,8 includes the PHPFarm service. The lines 13-18, enables the certain version for our application. The lines 20-24 enables apache server to read our mapping configuration file which should be moved to general Apache configuration script since this directory may be shared between other Virtual hosts. In short if you have multiple Virtual Hosts then the following should be moved into general to declare only once:
...
FastCgiServer /var/www/cgi-bin/php-cgi-5.4.27
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
...
<Directory "/var/www/cgi-bin">
Order allow,deny
Allow from all
Require all granted
</Directory>
...
At last, restart Apache to load our newest changes:
sudoe services apache2 restart
**Step 8**– Test it
To test it, include the phpinfo command into the index.php file of the root dir(i.e./var/www/) of the application
<?php phpinfo();?>
This should display php version along other setting once pointing the browser to the application of virtual host configured above(i.e. minnehaha.com)
Switching PHP Versions
After compiling another version of PHP as we did in Step 4, the switching between PHP Versions is as easy as configuring apache virtual host to read another mapping as done in Step 8(see commented out php version 5.4.27) to point to another php version done by creating new mapping as done in Step 7.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment