Nginx 和不同版本的 PHP FPM + PHP

myo*_*yol 5 php nginx php-fpm

由于马克和他之前的回答对我希望实现的目标有了更好的理解,我发布了一个(希望)更清晰且略有不同的变化,因为该线程已达到饱和;

我正在尝试在 nginx 服务器上运行多个 WordPress 站点,其中每个站点都需要不同版本的 PHP。我希望通过使用多个版本的 PHP-FPM 来实现这一点,每个版本都运行不同版本的 PHP,与 nginx 分开。

然后我想使用.conf文件来控制每个站点使用的 PHP-FPM 服务器,允许该站点在所需的 PHP 版本上运行。(根据评论区)

目前我的 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;

    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并且 sym 链接到/var/nginx/sites-enabled/testsite1. 每个服务器块都位于一个单独的文件中sites-available

testsite1
testsite2
testsite3
Run Code Online (Sandbox Code Playgroud)

我编译了不同版本的 PHP (5.3.3),但我不确定如何设置多个 PHP-FPM 服务器以及如何使每个“指向”不同版本的 PHP。我还需要有关如何设置多个.conf文件以定义每个 WordPress 站点将使用哪个 PHP-FPM 服务器的指导。

(本质上,我需要在整个过程中手牵手......)

Mar*_*ark 7

以我的经验,一个简单的服务器结构如下,对于你的情况来说已经足够了。

假设

您有大约两个小时的时间来设置它们。

服务器环境假设

1 x Nginx 服务器(前端,处理静态文件)

2 x PHP-FPM 服务器(后端,处理 PHP 脚本)

1 x 数据库服务器(在另一个独立的服务器或 Nginx 服务器中都可以)

Nginx服务器可以通过公网私网访问

PHP-FPM 服务器和 DB 服务器只能被 Nginx 服务器访问(私有网络

公共网络,这意味着有互联网的人可以访问。

Private Network,可以在特定的网络组中看到。(A 类、B 类、C 类。例如,192.xx.xx.xx 或 10.xx.xx.xx 或 172.xxx.xxx.xxx)

如果你在Linode和DigitalOcean上使用VPS,它们都提供私网IP,你可以按照他们给出的说明进行设置。

如果没有,您可以建立自己的VPN(虚拟专用网络)或使用您的路由器建立一个,这很容易,您可以GOOGLE所有您需要的。

如果您使用的是 Ubuntu,那么制作一个带有配置的 VPN 只需不到 5 分钟。

在下一步之前,建议您设置防火墙规则以防止潜在的攻击。(通过使用 IPTABLES 或其包装器 FirewallD)

虽然我们把 PHP-FPM 做成了 Nginx 专用的,但是网站的 PHP 文件不能同时通过 TCP Port 和 Unix Socket。

因此,您必须管理 Web 服务器的根文件夹。

管理网站文件夹的选项

  1. 使用相同的文件夹路径将您的网站上传到 Nginx 服务器和 PHP-FPM 服务器

  2. 编写脚本以将文件同步到所有服务器

  3. 在所有服务器上使用 GIT。

  4. 在 Nginx 或其他专用服务器上创建 NFS(网络文件系统)

如果您使用的是 *nix 系统,我建议您使用第四个选项,因为,

首先,在一台服务器上管理所有网站文件

二、非常容易维护

三、分分钟备份(这应该是另一个问题)

? NFS 服务器充当您网站的纯存储服务器

有些人可能会考虑使用 NFS 会导致网络延迟,但是对于多个等待管理的网站,NFS 是一种高效且简单的方式。

你可以GOOGLE“Linux上的NFS”来完成这一步的安装和配置,更新大约需要1小时。

但是,请注意 NFS 服务器也应位于专用网络中。

当 NFS、PHP-FPM 和 Nginx 都在同一个Private Network 中时,网络延迟应该更小。

然后让我们配置 nginx.conf

假设

你的 Nginx 公网 IP 是 202.123.abc.abc,收听 80(或 443,如果启用 SSL)

你的 PHP-FPM 5.5 在 192.168.5.5,听 9008

你的 PHP-FPM 5.6 在 192.168.5.6,听 9008

(附加示例)您的 HHVM 3.4 在 192.168.5.7 上,听 9008

你认为 PHP 5.5 是你最常用的 PHP 版本

    server {

         listen 80 default server;
         server_name frontend.yourhost.ltd;
         #root PATH is where you mount your NFS
         root /home/www;   
         index index.php;

    location ~ \.php$ {
        try_files  $uri $uri/ = 404;
        fastcgi_pass   192.168.5.5:9008;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PHP_VALUE  open_basedir=$document_root:/tmp/:/proc/;
        include fastcgi_params;
        fastcgi_buffer_size 512k;
        fastcgi_buffers 256 4k;
        fastcgi_busy_buffers_size 512k;
        fastcgi_temp_file_write_size 512k;
        fastcgi_intercept_errors on;

    }

    }
#Here to set up you vhosts
include vhosts/*.conf; 
Run Code Online (Sandbox Code Playgroud)

上面的行应该放在最后一个之前}

然后,在文件夹vhosts内创建一个名为nginx.conf

假设

您还有一个应用程序正在使用 PHP 5.6

您有另一个应用程序正在使用 HHVM

对于 PHP 5.6 (vhosts/app1.conf)

       server {
         server_name app1.yourhost.ltd;
         listen 202.123.abc.abc:80;
         index index.php;
         #root PATH is where you mount your NFS
         root /home/app1;
         #Include your rewrite rules here if needed
         include rewrite/app1.conf;

         location ~ \.php($|/){
             try_files  $uri $uri/ = 404;

             fastcgi_pass   192.168.5.6:9008;
             fastcgi_index  index.php;
             include        fastcgi_params;
             set $path_info "";
             set $real_script_name $fastcgi_script_name;
               if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                   set $real_script_name $1;
                   set $path_info $2;
                }
             fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
             fastcgi_param SCRIPT_NAME $real_script_name;
             fastcgi_param PATH_INFO $path_info;
            fastcgi_param  PHP_VALUE         open_basedir=$document$
         }
      location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
                    expires      30d;
      }

       location ~ .*\.(js|css)?$ {
                    expires      12h;
       }

        access_log  /var/wwwlog/app1/access.log access;
        error_log  /var/wwwlog/app1/error.log error;
     }
Run Code Online (Sandbox Code Playgroud)

对于 HHVM (vhosts/app2.conf)

       server {
         server_name app2.yourhost.ltd;
         listen 202.123.abc.abc:80;
         index index.php;
         #root PATH is where you mount your NFS
         root /home/app2;
         #Include your rewrite rules here if needed
         include rewrite/app2.conf;

         location ~ \.hh($|/){
             try_files  $uri $uri/ = 404;

             fastcgi_pass   192.168.5.7:9008;
             fastcgi_index  index.hh;
             include        fastcgi_params;
             set $path_info "";
             set $real_script_name $fastcgi_script_name;
               if ($fastcgi_script_name ~ "^(.+?\.hh)(/.+)$") {
                   set $real_script_name $1;
                   set $path_info $2;
                }
             fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
             fastcgi_param SCRIPT_NAME $real_script_name;
             fastcgi_param PATH_INFO $path_info;
             fastcgi_param  PHP_VALUE         open_basedir=$document$
         }
      location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
                    expires      30d;
      }

       location ~ .*\.(js|css)?$ {
                    expires      12h;
       }

        access_log  /var/wwwlog/app2/access.log access;
        error_log  /var/wwwlog/app2/error.log error;
     }
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您甚至可以为您的虚拟主机添加不同的 SSL 证书。

重启你的服务器,尽情享受吧!

已编辑

要安装不同版本的 PHP-FPM,您可以自己编译,也可以使用现有堆栈。

建议https://github.com/centos-bz/EZHTTP/archive/master.zip 节省您的时间

使用方法,(假设你的机器已经安装了WGET和UNZIP)

$ wget --no-check-certificate https://github.com/centos-z/EZHTTP/archive/master.zip?time= $(date +%s) -O server.zip

$ 解压 server.zip

$ cd EZHTTP-master

$ chmod +x start.sh

$ ./start.sh

在第一个屏幕中选择 1

在第二个屏幕中选择 1 (LNMP)

询问您要安装哪个版本的 nginx 时选择 1(do_not_install)

问你要安装哪个版本的mysql时选择1(do_not_install)

在询问您要安装哪个版本的 PHP 时,选择您想要的版本

保留所有设置为默认值(让您以后轻松管理 PHP-FPM)

选择你想要的额外扩展,当然你可以忽略,因为所有常见的扩展都会在以后安装。

让这个shell开始编译!