Friday, August 25, 2017

How to configure/Run Multiple PHP version on Single Linux machine.

PHP: -
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

PHP is mainly focused on server-side scripting, so you can do anything any other CGI program can do, such as collect form data, generate dynamic page content, or send and receive cookies. But PHP can do much more.


There are three main areas where PHP scripts are used:
  • Server-side scripting. This is the most traditional and main target field for PHP. You need three things to make this work: the PHP parser (CGI or server module), a web server and a web browser. You need to run the web server, with a connected PHP installation. You can access the PHP program output with a web browser, viewing the PHP page through the server. All these can run on your home machine if you are just experimenting with PHP programming.
  • Command line scripting. You can make a PHP script to run it without any server or browser. You only need the PHP parser to use it this way. This type of usage is ideal for scripts regularly executed using cron (on *nix or Linux) or Task Scheduler (on Windows). These scripts can also be used for simple text processing tasks.
  • Writing desktop applications. PHP is probably not the very best language to create a desktop application with a graphical user interface, but if you know PHP very well, and would like to use some advanced PHP features in your client-side applications you can also use PHP-GTK to write such programs. You also have the ability to write cross-platform applications this way. PHP-GTK is an extension for PHP, not available in the main distribution.




























Prerequisites:- We need following before start configuring multiple PHP. 
  • Linux Machine
  • One user with Sudoers privileges.
  • Working Internet on that machine.
Step 1-  Check Linux release
Before, We start configuring PHP we need to check our Linux Machine version. Use the command below to check.
[root@cent ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)
In my current scenario, I am using CentOS 6.8 Final release. 
Step 2- Install Webserver (HTTPD).
We need to install Web server httpd. let use yum command to start the installation.
[root@cent ~]# yum install httpd
Start httpd service
[root@cent ~]# service httpd start
Step 3- Enable Name Virtual Host
In order to setup multiple Virtual Host, we need to enable NameVirtualHost in the httpd.conf file.
[root@cent ~]# vi /etc/httpd/conf/httpd.conf
Let's uncomment following line.
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
Save and Exit from the File.
Restart httpd service to apply changes.
[root@cent ~]# service httpd restart
Step 4- Enable port 80 to allow access from outside.
In order to access the Web server from outside we need to enable Port 80, let's use the command below.
[root@cent ~]# vi /etc/sysconfig/iptables
Append this line to this file and save it.
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
Restart iptables service to apply changes.
[root@cent ~]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]
Step 5- Install PHP5.3
I am going to install PHP version 5.3 and PHP 5.6. let's follow the steps below to install both the version.
5A- Install PHP5.3 Version.
[root@cent ~]# yum install php php-cli php-common php-gd php-curl

=============================================================================================================================================================================================
 Package                                        Arch                                       Version                                            Repository                                Size
=============================================================================================================================================================================================
Installing:
 php                                            x86_64                                     5.3.3-49.el6                                       base                                     1.1 M
 php-cli                                        x86_64                                     5.3.3-49.el6                                       base                                     2.2 M
 php-common                                     x86_64                                     5.3.3-49.el6                                       base                                     530 k
 php-gd                                         x86_64                                     5.3.3-49.el6                                       base                                     111 k

Transaction Summary
=============================================================================================================================================================================================
Install       4 Package(s)

Total download size: 3.9 M
Installed size: 13 M
Is this ok [y/N]: Y
5B- Check PHP Version
[root@cent ~]# php -v
PHP 5.3.3 (cli) (built: Mar 22 2017 12:27:09)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
PHP Version 5.3 has been installed with default repo.
Step 6- Install PHP5.6
PHP5.6 is not available in default repo, For this, we need to add an additional repository, Let's use the command below to install repo.
6A- Install additional repo:
you can get required repo as per your OS from the following link.
[root@cent ~]# yum install http://rpms.remirepo.net/enterprise/remi-release-6.rpm

Dependencies Resolved

=============================================================================================================================================================================================
 Package                                      Arch                                   Version                                           Repository                                       Size
=============================================================================================================================================================================================
Installing:
 remi-release                                 noarch                                 6.9-1.el6.remi                                    /remi-release-6                                  12 k

Transaction Summary
=============================================================================================================================================================================================
Install       1 Package(s)

Total size: 12 k
Installed size: 12 k
Is this ok [y/N]: y
6B- Install PHP56 Version
[root@cent ~]# yum install php56  php56-php-cli php56-php-common php56-php-pear  php56-php-process  php56-php-xml  php56-php-gd
5C- Check PHP Version
[root@cent ~]# php56 -v
PHP 5.6.31 (cli) (built: Jul  6 2017 07:38:45)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
Both the PHP Versions has installed successfully.
Step 7- Configure  Virtual Host for  PHP version 5.3
We are going to use PHP-CGI to execute both the PHP version simultaneously.
7A-  First, rename php.conf file if exist. 

[root@cent ~]# mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.bak
7B- Setup FastCGI for PHP version 5.3
 First, create a folder under Virtual host document root directory.
[root@cent ~]#  mkdir -p /home/php53/public/cgi-bin/
7C- Create FastCGI configuration file.

Let's find your php.ini for PHP version 5.3 and PHP-CGI using find command.
To find php.ini
[root@cent ~]# find / -name php.ini
/etc/php.ini
/opt/remi/php56/root/etc/php.ini
To find php-cgi
[root@cent ~]# find / -name php-cgi
/opt/remi/php56/root/usr/bin/php-cgi
/usr/bin/php-cgi
7C- Create FastCGI configuration file.
[root@cent ~]#  vi /home/php53/public/cgi-bin/php.fastcgi
Append following line as per above finding. In my case for PHP 5.3 is the deault path.

#!/bin/bash
PHPRC="/etc/php.ini"
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
export PHPRC
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec /usr/bin/php-cgi
Save and Exit from the File.
7D- Make the script executable.
[root@cent ~]#  chmod +x /home/php53/public/cgi-bin/php.fastcgi
7E- Setup Virtual Host 
[root@cent ~]#  vi /etc/httpd/conf.d/php53.conf
Add following lines 

<VirtualHost *:80>
    ServerAdmin asingh@ishir.com
    DocumentRoot /home/php53/public/
    ServerName php53.local
    ErrorLog /home/php53/logs/error.log
    CustomLog /home/php53/logs/access_log common
        ScriptAlias /cgi-bin/ "/home/php53/public/cgi-bin/"
    <Directory "/home/php53/public/">
            Options +Indexes FollowSymLinks +ExecCGI
            AddHandler php5-fastcgi .php
            Action php5-fastcgi /cgi-bin/php.fastcgi
            AllowOverride All
            Order allow,deny
            Allow from All
    </Directory>
</VirtualHost>

Save and Exit from the file.
7F- restart httpd.

[root@cent ~]#  service httpd restart
7G- Check PHP version.

[root@cent ~]# vi /home/php53/public/info.php
Append the following content.

<?php
phpinfo();
?>
Save and exit from the file.
7H- Test PHP page. Hit the below URL to check php info.
http://php53.local/info.php
Step 8- Configure  Virtual Host for  PHP version 5.6:
Follow the steps below to configure FastCGI and Virtual Host.
8A- Setup FastCGI for PHP version 5.6
First, create a folder under Virtual host document root directory.

[root@cent ~]#  mkdir -p /home/php56/public/cgi-bin/
8B- Create FastCGI configuration file.

[root@cent ~]#  vi /home/php56/public/cgi-bin/php.fastcgi
Append following line

#!/bin/bash
PHPRC="/opt/remi/php56/root/etc/php.ini"
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
export PHPRC
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec /opt/remi/php56/root/usr/bin/php-cgi

Save and Exit from the file.
8C- Make the script executable.

[root@cent ~]#  chmod +x /home/php56/public/cgi-bin/php.fastcgi
8d- Setup Virtual Host 

[root@cent ~]#  vi /etc/httpd/conf.d/php56.conf
Add following lines 

<VirtualHost *:80>
    ServerAdmin asingh@ishir.com
    DocumentRoot /home/php56/public/
    ServerName php56.local
    ErrorLog /home/php56/logs/error.log
    CustomLog /home/php56/logs/access_log common
        ScriptAlias /cgi-bin/ "/home/php56/public/cgi-bin/"
    <Directory "/home/php56/public/">
            Options +Indexes FollowSymLinks +ExecCGI
            AddHandler php5-fastcgi .php
            Action php5-fastcgi /cgi-bin/php.fastcgi
            AllowOverride All
            Order allow,deny
            Allow from All
    </Directory>
</VirtualHost>

Save and Exit from the file.

8E- restart httpd.

[root@cent ~]#  service httpd restart
8F- Check PHP version.

[root@cent ~]# vi /home/php56/public/info.php
Append the following content.

<?php
phpinfo();
?>

Save and exit from the file.
8G- Test PHP page- Hit the below URL to check php info.
http://php56.local/info.php

Both the PHP Version Configured successfully and working as expected.


Thank you for Reading this blog!!!

No comments:

Post a Comment