CentOS 7 上带有 Apache 的多个 PHP 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50004406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple PHP version with Apache on CentOS 7
提问by Tabish
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..
这里的任何人都可以指导我在 CentOS 7 上使用一个 apache 实例安装和配置 Multi PhP 的方法,以及测试它的正确方法..
回答by runwuf
install all the necessary repos and packages
安装所有必要的存储库和软件包
big thanks to https://rpms.remirepo.net/wizard/
非常感谢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:
以下命令假设您已经sudo su -
或者您必须将 sudo 添加到每个命令中:
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
停止两个 fpm 服务器
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
默认情况下,它侦听 127.0.0.1 端口 9000,让它们侦听不同的端口
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
现在可以在不同的端口上启动两个不同版本的 fpm
systemctl start php72-php-fpm
systemctl start php56-php-fpm
制作脚本包装器以调用 php56-cgi 和 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
使它们可由 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
为 apache 创建 php 配置。默认情况下,它运行 php56-fcgi 处理程序
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
制作测试页面,创建 .htaccess 以使用 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)
( 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
回答by Remi Collet
As explained by @runwuf, this is possible using the sofware collections available in centos-sclrepository or in remirepository.
正如@runwuf 所解释的,这可以使用centos-scl存储库或remi存储库中可用的软件集合来实现。
But using SetHandler to fastcgi proxy seems a better and more modern way, thanks to httpd 2.4:
但是由于 httpd 2.4,使用 SetHandler 来进行 fastcgi 代理似乎是一种更好、更现代的方式:
SetHandler "proxy:fcgi://127.0.0.1:9000"
This is explained in some blog posts:
这在一些博客文章中有解释:
- My PHP Workstationon Remi's blog
- PHP Configuration Tipson RH developpers' blog
- Remi 博客上的我的 PHP 工作站
- RH 开发者博客上的PHP 配置技巧
回答by Morgan
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.
我必须将以下内容添加到目录语句中的 php.conf 中,以使 Apache 服务器 API 更改为 FPM/FastCGI 而不是 CGI/FastCGI - 不过您的解决方案几乎是完美的!现在,如果我能弄清楚如何让它使用套接字而不是 TCP,我会成为一名快乐的编码员。
# mod_proxy_fcgi options
<IfModule mod_proxy_fcgi.c>
<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9072"
</FilesMatch>
</IfModule>
回答by Shahnewaz Ul Islam Chowdhury
replying to runwuf
回复 runwuf
Hello, there is one problem with your approach regarding SELinux
您好,您关于 SELinux 的方法存在一个问题
either you disable SELinux (if you are not concerned with security) or you manage the SELinux Port Policy
要么禁用 SELinux(如果您不关心安全性)要么管理 SELinux 端口策略
In case you don't handle the SELinux, the php56-php-fpm won't start if SELinux is set to 'Enforcing' mode
如果您不处理 SELinux,如果 SELinux 设置为“强制”模式,php56-php-fpm 将不会启动
Run the following commands for making SELinux allow the ports
运行以下命令使 SELinux 允许端口
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
然后最后尝试启动 fpm 模块
回答by runwuf
It looks like what you are trying to do is similar to this:
看起来您正在尝试执行的操作与此类似:
running-two-php-versions-on-the-same-server
运行两个 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.
我个人不想在同一个 apache 实例上尝试两个 php 版本......我会通过 tarball 安装不同版本的 php,并在 tarball 安装的单独 apache 实例上运行它们,并将每个 httpd.conf 指向不同的php版本。