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.
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!
Install some libs needed for PHP Compilation:
https://github.com/cweiske/phpfarm
http://cweiske.de/tagebuch/Introducing%20phpfarm.htm
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/5887778https://github.com/cweiske/phpfarm
http://cweiske.de/tagebuch/Introducing%20phpfarm.htm
Comments to „Downgrade PHP 5.5 to 5.3 in Ubuntu 14 and use both PHP Versions parallel“
Thanks for this, I’ve been needing a working Apache / FastCGI / PHP 5.3.29 to work on old code, and this was a very nice tutorial.
For anyone that was following this and ran into issues with SSLv3_server_method and SSLv3_client_method being undefined (SSLv3 support not present on your server). You can apply this patch to the PHP 5.3.29 source code to have make it successfully compile.
https://github.com/php/php-src/commit/640214701c9cf259c899d283ea769b3045d2553c
After I applied that patch, it all worked nicely. Great tutorial! =)
It works! Thank you for this!!!
Im pretty sure this is an issue with PHP 5.5 being installed on the new server where 5.3 was installed on the old server.
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.
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?
@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.
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
@Danu
I think it has something to do with „etc/host“. Are the entries there correct?
Great tutorial, it’s working fine.
I have a question: how can we let the default virtual host (the main) use php 5.3.29 instead of php 5.5.x ?
I mean when I request http://127.0.0.1 or http://localhost or http://ip, the current website use php 5.3.29
@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
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 !
@phpcore
Just leave out „ServerName ….“ in your „.. .conf“ file. This will enable the fast cgi server for all local websites.
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
@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“
I did everything written here and got no errors while installing but when I try to go to http://IP/cgidemo.dev or mysite.dev I get 404. Don’t know what I did wrong
@Bulent Yuksel
Why is there „IP“ in your adress?
Normally it should be „http://cgidemo.dev“.
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.
please excuse my english
I have a problem when i compile php
„sudo ./compile.sh 5.3.29“
i have this error
Makefile:257: recipe for target ’sapi/cgi/php-cgi‘ failed
make: *** [sapi/cgi/php-cgi] Error 1
make failed.
I found online (here : https://groups.google.com/forum/#!topic/openlitespeed-development/H3sTLANFu50) that it could be a problem whith „make clean“.
Do you have any idea ?
I can’t seem to compile PHP 5.3.29 with mysqli enabled. Am I missing something?
What Ubuntu Version do you have? What is the exact error ?
try:
sudo apt-get install libmysqlclient15-dev
This will install mysql libraries.
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
Maybe its a permission or configuration problem.
This might help:
http://serverfault.com/questions/527417/fastcgi-comm-with-server-aborted-idle-timeout-30-sec
did you resolve this error?
me too!!
I got message
FastCGI: incomplete headers (0 bytes) received from server „/var/www/cgi-bin/php-cgi-5.3.29“
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
i got this error in compile.sh in Linux Mint 17
Cannot find MySQL header files under yes
here the solution:
sudo apt-get install libmysqlclient15-dev
I am having trouble getting mod_rewrite to work in my htaccess file following the vhost example you have. Any thoughts?
@Andrew
Maybe you should try „AllowOverride All“ for yout webserver directory. In my enviroment mod_rewrite works fine for all PHP-Versions.
Try this:
First (within your virtual host directives):
AllowOverride All
Enable mod_rewrite if not:
sudo a2enmod rewrite
Restart apache:
sudo service apache2 restart