Run multiple PHP versions on the same server using php-fpm in linux

Run multiple PHP versions on the same server using php-fpm in linux, install with fastcgi(FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features (mostly) useful for heavy-loaded sites.
)

Install php 5.6 and 7.1 as fpm  in linux 16.04



sudo suadd-apt-repository ppa:ondrej/php
apt-get update

apt-get install libapache2-mod-fastcgi php5.6-fpm php5.6 php5.6-dev php5.6-mcrypt php5.6-mbstring php5.6-mysql php5.6-zip php5.6-gd php5.6-xml php7.1-fpm libapache2-mod-fastcgi php7.1-fpm php7.1 php7.1-dev php7.1-mbstring php7.1-mysql php7.1-zip php7.1-gd php7.1-xml php7.1-curl php7.1-intl php7.1-json php7.1-mcrypt
Enables apache modules to support FPM

a2enmod actions
a2enmod fastcgi
Add handlers for both version to your Apache vhost , Example of Php 5.6 version using virtual host

let's create a virtual host to access php version on each site different.

Create directory to for the website 

sudo mkdir var/www/html/example
create a file sudo nano /etc/apache2/sites-available/example.conf

<VirtualHost *:80> ServerName example.local ServerAlias www.example.local ServerAdmin admin@example.local DocumentRoot /var/www/html/example <Directory /var/www/html/example> Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Now, add this to use php.5.6  to example.conf

<IfModule mod_fastcgi.c> AddHandler php56-fcgi-www .php Action php56-fcgi-www /php56-fcgi-www Alias /php56-fcgi-www /usr/lib/cgi-bin/php56-fcgi-www FastCgiExternalServer /usr/lib/cgi-bin/php56-fcgi-www -socket /run/php/php5.6-fpm.sock -pass-header Authorization <Directory "/usr/lib/cgi-bin"> Require all granted </Directory> </IfModule> <IfModule mod_fastcgi.c> <FilesMatch ".+\.ph(p[345]?|t|tml)$"> SetHandler php56-fcgi-www </FilesMatch> </IfModule>
final complete output is example.conf is


<VirtualHost *:80>

    ServerName example.local
    ServerAlias www.example.local
    ServerAdmin admin@example.local

    DocumentRoot /var/www/html/test
    <Directory /var/www/html/test>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    <IfModule mod_fastcgi.c>
     AddHandler php56-fcgi-www .php
     Action php56-fcgi-www /php56-fcgi-www
     Alias /php56-fcgi-www /usr/lib/cgi-bin/php56-fcgi-www
     FastCgiExternalServer /usr/lib/cgi-bin/php56-fcgi-www -socket /run/php/php5.6-fpm.sock -pass-header Authorization
     <Directory "/usr/lib/cgi-bin">
      Require all granted
     </Directory>
    </IfModule>
    <IfModule mod_fastcgi.c>
        <FilesMatch ".+\.ph(p[345]?|t|tml)$">
           SetHandler php56-fcgi-www
        </FilesMatch>
     </IfModule>

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

</VirtualHost>
if you would like to use php7.1 Then use this code  and replace with php5.6 code in example.conf file

<IfModule mod_fastcgi.c>
 AddHandler php71-fcgi-www .php
 Action php71-fcgi-www /php71-fcgi-www
 Alias /php71-fcgi-www /usr/lib/cgi-bin/php71-fcgi-www
 FastCgiExternalServer /usr/lib/cgi-bin/php71-fcgi-www -socket /run/php/php7.1-fpm.sock -idle-timeout 1800 -pass-header Authorization
 <Directory "/usr/lib/cgi-bin">
  Require all granted
 </Directory>
</IfModule>
<IfModule mod_fastcgi.c>
 <FilesMatch ".+\.ph(p[345]?|t|tml)$">
  SetHandler php71-fcgi-www
 </FilesMatch>
</IfModule>
We can use the a2ensite tool to enable each of our sites like this:

sudo a2ensite test.conf
restart server

sudo service apache2 restart
Add to file  /etc/hosts

127.0.0.1       example.local www.example.local
Now run in the browser

example.local

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...