Skip to content

Instantly share code, notes, and snippets.

@maaarghk
Last active August 22, 2017 23:16
Show Gist options
  • Save maaarghk/11f149e52dcd3f4e50be to your computer and use it in GitHub Desktop.
Save maaarghk/11f149e52dcd3f4e50be to your computer and use it in GitHub Desktop.
Dual PHP - Run PHP 5.3 using mod_php and PHP 5.6 using php_fpm on Apache 2.2, as distributed by CentOS 6.5. Also tested on CentOS 6.3!

Dual PHP - Apache 2.2 running PHP 5.3 mod_php and PHP 5.6 php_fpm on CentOS 6.5

Introduction

We have a CentOS 6.5 server in production, running sites which are not compatible with PHP 5.4+. For this reason, we are running Apache 2.2 + PHP 5.3 (mod_php). We also run some newer sites on the server and we would like to run them on PHP 5.6 in order to take advantage of the performance improvements which have been realised in the newer versions of PHP. To minimize pain, we will run both versions side by side. This will require mod_php to be overridden in the VirtualHost by mod_fastcgi. We will install PHP 5.6 FPM to achieve this.

This procedure has also been tested and works on an up to date install of CentOS 6.3!

Part 1: get everything in place

We have to install a bunch of development libraries, and then we're going to build PHP 5.6 from source. We will install PHP 5.6 in its own folder, /opt/php56. This bit is essentially possible to complete by rote:

sudo yum install gcc-c++ libicu libicu-devel.x86_64 libjpeg-devel libvpx-devel libpng-devel libxml2-devel curl-devel
tar -xvf php-5.6.1.tar.gz
cd php-5.6.1
 ./configure --prefix /opt/php56 --with-mysqli --enable-mysqlnd --with-pdo-mysql --enable-soap --enable-mbstring --enable-intl --with-openssl --with-gd --enable-opcache --enable-phar --enable-fpm --with-curl
make
sudo make install
sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php56-fpm
sudo cp php.ini-production /opt/php56/lib/php.ini
cd /opt/php56
sudo cp etc/php-fpm.conf.default etc/php-fpm.conf
sudo touch var/log/php-error.log

Part 2: Configure

First make changes to php.ini. We're using vim because we have our shit together.

sudo vim lib/php.ini
  • Find error_log and set it to /opt/php56/var/log/php-error.log
  • Find [opcache] and uncomment all the options below.
  • Change opcache.enable to 1.

Now we need to configure php-fpm.

sudo vim etc/php-fpm.conf 
  • under [global] - uncomment pid = run/php-fpm.pid
  • under [www] - uncomment and update listen = 127.0.0.1:9000 to read listen = /var/run/php56-fpm.sock
  • under [www] - uncomment and update user and group, both to apache.
  • under [www] - uncomment and update listen.user and listen.group, also both to apache.
  • under [www] - uncomment listen.mode and update to 0600.

Part 3: Enable system service

sudo chmod u+x /etc/init.d/php56-fpm
sudo /etc/init.d/php56-fpm start
sudo /sbin/chkconfig php56-fpm on

Part 4: Configure apache

As mentioned in the Introduction, we want to minimize pain. That's why we are going to continue to use Apache 2.2. Sadly this does not eliminate pain entirely as we now have to configure mod_fastcgi which is basically crap. So crap it isn't even available in our package manager. Let's get it from a third party.

wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
sudo rpm -ivh rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
sudo yum install mod_fastcgi
sudo service httpd reload

We now need to prepare ourselves for a totally stupid workaround. Just run with this. Google FastCgiExternalServer for more details if the desire takes you. Or use Apache 2.4 and mod_proxy_fcgi.

cd /var/www/
ln -s vhosts-fpm vhosts

Check out the following config file -

sudo vim /etc/httpd/conf.d/fastcgi.conf

and make sure that FastCgiWrapper is set to Off.

Now, update the VirtualHost you want to run PHP 5.6 under.

sudo vi /etc/httpd/vhost.d/example.com.conf

Add the following lines inside the <VirtualHost/> block. This assumes that your DocumentRoot is set to /var/www/vhosts/example.com.

AddHandler php56-fcgi .php
Action php56-fcgi /php56-fcgi virtual
Alias /php56-fcgi /var/www/vhosts-fpm/example.com/php56-fcgi
FastCgiExternalServer /var/www/vhosts-fpm/example.com/php56-fcgi -socket /var/run/php56-fpm.sock -pass-header Authentication

Now reload Apache:

sudo service httpd reload

et voilà~~~~!

@cobrachaos
Copy link

Also, maybe that package wasn't correct... because now I just keep getting this when I try to use php 5.6 for a host and then stopping/starting httpd:
Starting httpd: Syntax error on line 15 of /etc/httpd/conf.d/fastcgi.conf:
FastCgiIpcDir /var/run/mod_fastcgi: already defined as "/etc/httpd/logs/fastcgi"

Any Ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment