多个版本的 PHP 通过 nginx

myo*_*yol 4 php nginx php-fpm

编辑:以供将来参考

我正在使用 PHP 5.5.12 运行带有 LEMP 堆栈的 Ubuntu 14.10。我有许多需要 PHP 5.3.3 的旧版 WordPress 站点以及一些使用相当新版本的 PHP 的 WP 站点,它们都在我本地机器上的 nginx运行。

我的手被虚拟机和沙箱束缚了,我只能玩 nginx,因此这个问题。我了解人们的安全问题,但我需要这些网站在本地运行,以便我可以在将它们更新到最新的 PHP/WP 版本时测试损坏的功能。

我想让 nginx 根据 WordPress 站点运行正确版本的 PHP(使用 php-fpm)。根据另一个SF 问题,实现此目的的一种方法是让不同的 PHP 版本在不同的端口/套接字上运行,并配置 nginx 服务器块以使用相应的端口/套接字。

我已经手动编译了 PHP 5.3.3 以包含 php-fpm 但这是我得到的最远的。

实际上,我希望有人更详细地解释这个答案。我不太清楚如何“在不同的端口(或套接字)上运行每个版本的 php-fpm”“在 nginx 服务器块的 fastcgi_pass 参数中配置适当的端口”

我的一个服务器块看起来像这样以供参考

server {
    listen 80;
    listen [::]:80;

    root /usr/share/nginx/html/testsite1;
    index index.php index.html index.htm;

    server_name local.testsite1.com;

    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我在单独的文件中使用单独的服务器块设置每个站点,符号链接在/var/nginx/sites-available/testsite1和之间/var/nginx/sites-enabled/testsite1。所以var/nginx/sites-available包含;

 testsite1
 testsite2
 testsite3
 ...
Run Code Online (Sandbox Code Playgroud)

所以理想情况下,我想知道是否有可能(因为这类似于如何使用不同的 PHP 版本设置 apache)

testsite1 - 运行旧版本的 PHP

 server {
    listen 80;
    listen [::]:80;

    root /usr/share/nginx/html/testsite1;
    index index.php index.html index.htm;

    server_name local.testsite1.com;

    ...settings to use older PHP version...

    ...remaining nginx settings...
}
Run Code Online (Sandbox Code Playgroud)

testsite2 - 运行当前版本的 PHP

 server {
    listen 80;
    listen [::]:80;

    root /usr/share/nginx/html/testsite2;
    index index.php index.html index.htm;

    server_name local.testsite2.com;

    ...settings to use currrent PHP version (if needed)...

    ...remaining nginx settings...
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?我问的原因是我宁愿避免将所有php文件重命名php2为以运行(使版本控制变得痛苦)。我不介意编辑nginx.conf文件或执行任何步骤,只要我不必重命名文件。

我也相信(fastcgi_pass unix:/var/run/php5-fpm.sock;)由于 WordPress,我需要通过端口使用套接字(尽管我对所有建议持开放态度)。

Mar*_*ark 11

好的,你想通过 nginx 运行多个 PHP 版本,配置文件应该包含你将 PHP 脚本放在不同版本或扩展名的特定路径。

但是,我想解释一下您帖子中给出的 SF 问题链接。

这个问题的答案是给你一个方法来修改conf你的nginx,它假设提问有背景nginx

通过在 中提供特定端口conf,nginx 将使脚本通过该 FastCGI 通道执行。

让我们假设你已经在你的服务器上安装不同的PHP版本,并且修改了portphp-fpm.conf

您希望php在版本中执行的所有文件5.5.0php25.6.0.

nginx的示例配置如下,

    listen       8080;
    server_name  localhost;

    root   html;

    location / {
        index  index.php index.php2;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  "${document_root}${fastcgi_script_name}";
        include        fastcgi_params;
    }

    location ~ \.php2$ {
        fastcgi_pass   127.0.0.1:9001;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  "${document_root}${fastcgi_script_name}";
        include        fastcgi_params;
    }
Run Code Online (Sandbox Code Playgroud)

在这种情况下,php通过端口处理9000php2转到9001

这只是一个简单的例子,对于高级的你可以创建两个不同的文件夹来存储 php 文件,例如/home/php55/home/php56,然后编辑你的nginx.conf.

对于您编辑的问题

如果您想尝试添加不同的服务器来处理不同版本的脚本,当然可以这样做,因为 nginx 可以作为负载均衡器,它也可以处理此类问题。

第一次申请

server{
listen 80;
server_name php1.localhost; 

root   /home/www/php5.5;

location / {
  index  index.php;
}

location ~ \.php$ {
  fastcgi_pass   127.0.0.1:9002; (You can change it to your private IP in the future)
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  include        fastcgi_params;
}
}
Run Code Online (Sandbox Code Playgroud)

第二次申请

server{
listen 80;
server_name php2.localhost; 

root   /home/www/php5.6;

location / {
  index  index.php;
}

location ~ \.php$ {
  fastcgi_pass   127.0.0.1:9004; (You can change it to your private IP in the future)
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  include        fastcgi_params;
}
}
Run Code Online (Sandbox Code Playgroud)

通过上面的配置,你可以很方便的切换PHP脚本的不同版本结果,而且你可以使用NFS(如果你在*nix环境下)挂载磁盘,在这种情况下,你就不需要放文件了手动放入两个文件夹。

至于Fastcgi的传递方式,我建议使用PORT而不是Socket。

我们都知道 Unix Socket 有更好的性能,因为 TCP 端口即使在同一台机器上也使用整个网络堆栈。

但是,节省的时间很少,而且在高流量的时候使用Socket时可能会遇到这个问题,

connect() to unix:/var/run/php5-fpm.sock failed or apr_socket_recv: Connection reset by peer (104)

此外,如果将 nginx 和 php-fpm 放在单独的服务器中,TCP 端口可以为您提供更快的管理方式。

我正在使用小型笔记本电脑来编辑这篇文章,所以代码并不漂亮,但我尝试过....

已编辑

记得修改你的/etc/hosts以匹配你的主机名(server_name in nginx.conf


小智 6

出于测试目的,我想将我的 nginx 配置为在同一服务器部分的不同位置下使用不同的 php 版本。最后它与此合作:

#Folder to run some tests with the new php-7
location ~ "^\/test_php7.0.3\/.*\.php$" {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php7-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

#Folder to run some tests with a custom compiled version of php5.6
location ~ "^\/test_php5.6.18\/.*\.php$" {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php56-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

#The default php version:
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
Run Code Online (Sandbox Code Playgroud)

希望这对其他人有帮助:)