Multiple PHP version with Apache on CentOS 7

雨燕双飞 提交于 2020-01-31 05:07:25

问题


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..


回答1:


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



回答2:


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:

  • My PHP Workstation on Remi's blog
  • PHP Configuration Tips on RH developpers' blog



回答3:


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.




回答4:


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>



回答5:


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



来源:https://stackoverflow.com/questions/50004406/multiple-php-version-with-apache-on-centos-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!