13

Can anyone here instruct me way to install and configure Multi PhP with one apache instance on CentOS 7, and the proper way to test it..

runwuf
  • 1,701
  • 1
  • 8
  • 15
Tabish
  • 171
  • 1
  • 1
  • 4
  • Multiple instance of PHP or multiple php projects in one php instance? – nerdlyist Apr 24 '18 at 14:49
  • @nerdlyist multi php instance on apache. I have found that the way is somewhere through CGI... but I am still searching for the right answer. – Tabish Apr 26 '18 at 15:48
  • Can I ask why? I can say it would not be simple to do on one host. My usual strategy is to run Virtual Machines (VM) on the host with VirtualBox. Or setting up multiple projects in one instance (pretty simple and well documented) – nerdlyist Apr 26 '18 at 17:54

6 Answers6

24

install all the necessary repos and packages

big thanks to https://rpms.remirepo.net/wizard/

the following commands assume you already sudo su - or you will have to add sudo to each of the commands:

yum install httpd -y
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install yum-utils -y
yum install php56 -y
yum install php72 -y
yum install php56-php-fpm -y
yum install php72-php-fpm -y

stop both fpm servers

systemctl stop php56-php-fpm
systemctl stop php72-php-fpm

by default it listens on 127.0.0.1 port 9000, make them listen on different ports

sed -i 's/:9000/:9056/' /etc/opt/remi/php56/php-fpm.d/www.conf
sed -i 's/:9000/:9072/' /etc/opt/remi/php72/php-fpm.d/www.conf

now two different version of fpm can be started on different ports

systemctl start php72-php-fpm
systemctl start php56-php-fpm
make script wrapper to call php56-cgi and php72-cgi
cat > /var/www/cgi-bin/php56.fcgi << EOF
#!/bin/bash
exec /bin/php56-cgi
EOF

cat > /var/www/cgi-bin/php72.fcgi << EOF
#!/bin/bash
exec /bin/php72-cgi
EOF

make them executable by apache

sudo chmod 755 /var/www/cgi-bin/php56.fcgi
sudo chmod 755 /var/www/cgi-bin/php72.fcgi

create php configuration for apache. by default it runs php56-fcgi handler

cat > /etc/httpd/conf.d/php.conf << EOF
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
AddHandler php56-fcgi .php
Action php56-fcgi /cgi-bin/php56.fcgi
Action php72-fcgi /cgi-bin/php72.fcgi

<Directory /var/www/html/php56>
    DirectoryIndex index.php
    AllowOverride all
    Require all granted
</Directory>
<Directory /var/www/html/php72>
    DirectoryIndex index.php
    AllowOverride all
    Require all granted
</Directory>
EOF

make test pages, create .htaccess to use php72-fcgi

mkdir -p /var/www/html/php56
mkdir -p /var/www/html/php72
echo "<?php phpinfo(); ?>" > /var/www/html/php56/index.php
echo "<?php phpinfo(); ?>" > /var/www/html/php72/index.php
echo "AddHandler php72-fcgi .php" > /var/www/html/php72/.htaccess

Now you should be able to test it

(http://127.0.0.1/php56)
(http://127.0.0.1/php72)

If you want to startup these instance automatically after server reboot

sudo systemctl enable httpd
sudo systemctl enable php56-php-fpm
sudo systemctl enable php72-php-fpm
Community
  • 1
  • 1
runwuf
  • 1,701
  • 1
  • 8
  • 15
  • cgi wrapper is (IMHO) a deprecated wau, better to use the SetHandler to proxy way, will add another answer. – Remi Collet May 04 '18 at 05:14
  • Worked for me (with many changes) in Centos 6.9 and apache 2.2. Upvoted :) – Javier S Aug 17 '18 at 19:28
  • eh - don't mix it with other repos (e.g. webtatic^^) - after disabling that other repo, it worked smooth, thanks a lot! – bebbo Oct 24 '19 at 08:46
12

As explained by @runwuf, this is possible using the sofware collections available in centos-scl repository or in remi repository.

But using SetHandler to fastcgi proxy seems a better and more modern way, thanks to httpd 2.4:

SetHandler "proxy:fcgi://127.0.0.1:9000"

This is explained in some blog posts:

Remi Collet
  • 6,198
  • 1
  • 20
  • 25
  • 1
    Thank you Remi, great info! So to rewrite my proposed solution with `SetHandler` it would be something like this? ` SetHandler "proxy:fcgi://127.0.0.1:9056" SetHandler "proxy:fcgi://127.0.0.1:9072" ` – runwuf May 08 '18 at 04:24
  • @RemiCollet In a server with dozens of sites using .htaccess, should mod_php be disabled? What would have to be done is simply rename all ".htaccess" to ".user.ini"? – Noberto Pessôa Aug 20 '18 at 22:45
  • No, .user.ini have not the same syntax (no need of the php_flag directive) – Remi Collet Aug 21 '18 at 05:15
  • At my hosting company, there is a machine that has CentOS and PHP 5.6, this was fine, then PHP 7 came out, and I needed to have some vHosts run on PHP 5 and others on PHP 7, so they setup the "SetHandler" in the vHost for PHP 7, but now the X-FRAME-OPTION header I send from my application don't make it through the handler to be sent out to the browser, any suggestions? – Craig London Sep 07 '19 at 04:19
2

I had to add the following to my php.conf inside the directory statement to make the Apache Server API change to FPM/FastCGI instead of CGI/FastCGI - your solution was almost perfect though! Now if I could just figure out how to make it use a socket instead of TCP, I'd be one happy coder.

# mod_proxy_fcgi options
<IfModule mod_proxy_fcgi.c>
    <FilesMatch \.php$>
       SetHandler "proxy:fcgi://127.0.0.1:9072"
    </FilesMatch>
</IfModule>
Morgan
  • 44
  • 4
  • Did you try this? SetHandler "proxy:fcgi://127.0.0.1:9056" SetHandler "proxy:fcgi://127.0.0.1:9072" – runwuf Mar 07 '19 at 03:37
  • @runwuf - yes, that is what I did, but that uses a TCP connection instead of a socket... IF I could use a socket, that would be preferable, unfortunately I never figured out the correct way to do so... – Morgan Jul 22 '19 at 16:17
0

It looks like what you are trying to do is similar to this:

running-two-php-versions-on-the-same-server

I personally would not want to attempt two php version on the same apache instance... I would install different version of php by tarball and run them on separate instance of apache that is also installed by tarball and point each httpd.conf to the different version of php.

runwuf
  • 1,701
  • 1
  • 8
  • 15
  • yes the url you shared is what i want.. but doesnot show complete procedure.. and i guess it is for ubuntu.. I am a newbie.. Can't do it without help.. – Tabish Apr 26 '18 at 15:51
  • Is there any reason that you need to run two versions of php within the same apache? is it not feasible that you run them in two different apache but the same machine? I think in most use case that is very close to what you need and a much easier and safer solution to manage in a long term. – runwuf Apr 26 '18 at 22:02
  • actullay it is a task given to me by me teacher.. I want to do it very badly.. I can't just backoff.. Please let me know if you find a solution for cgi mod..!! – Tabish Apr 28 '18 at 03:56
  • I spent a bit of time to try this out and have it working on centos7, see my new answer below, hope it helps! – runwuf Apr 28 '18 at 18:05
0

replying to runwuf

Hello, there is one problem with your approach regarding SELinux

either you disable SELinux (if you are not concerned with security) or you manage the SELinux Port Policy

In case you don't handle the SELinux, the php56-php-fpm won't start if SELinux is set to 'Enforcing' mode

Run the following commands for making SELinux allow the ports

  semanage port -a -t http_port_t -p tcp 9072
  semanage port -a -t http_port_t -p tcp 9056

and then finally try to start the fpm modules

Evgeny Minkevich
  • 2,319
  • 3
  • 28
  • 42
0

I wandered here looking for a solution. CentOS now has a PHP Select which let's you run different version of PHP on the server.

More information can be found here: http://forum.centos-webpanel.com/php/php-selector/

ibyte
  • 463
  • 4
  • 17