如何在外部机器上以tcp模式使用php-fpm?

如何在外部机器上以tcp模式使用php-fpm?

我通过 TCP 使用 php-fpm,因为我的目的是对其进行负载平衡。

我是否应该在每台服务器上都有 PHP 文件?

或者我可以将其存储在 Web 服务器 (Lighttpd) 上吗?404 Error当我尝试请求一个简单的 php 文件时,我收到了。该文件存储在 lighttpd 服务器上,而 php-fpm 在另一台机器上运行。

这是我在 lighttpd 上的配置:

server.modules += ( "mod_fastcgi" )
fastcgi.debug = 1


fastcgi.server += ( ".php" =>
        ((
                "host" => "10.5.0.3",
                "port" => "9000",
                "broken-scriptfilename" => "enable"
        ))
)

以及 php-fpm 池配置:

[www]
listen = 127.0.0.1:9000
user = www-data
group = www-data
pm = ondemand
pm.max_children = 5
pm.start_servers = 2
pm.process_idle_timeout = 10s;
pm.max_requests = 500

PHP-FPM 日志:

[13-Sep-2017 20:22:54] NOTICE: fpm is running, pid 1
[13-Sep-2017 20:22:54] NOTICE: ready to handle connections
10.5.0.2 -  13/Sep/2017:20:24:50 -0300 "GET /info.php" 404

lighttpd 日志如下:

2017-09-13 20:24:50: (configfile-glue.c.615) 7 (uncached) result: false
2017-09-13 20:24:50: (response.c.350) -- splitting Request-URI
2017-09-13 20:24:50: (response.c.351) Request-URI     :  /info.php
2017-09-13 20:24:50: (response.c.352) URI-scheme      :  http
2017-09-13 20:24:50: (response.c.353) URI-authority   :  localhost
2017-09-13 20:24:50: (response.c.354) URI-path (raw)  :  /info.php
2017-09-13 20:24:50: (response.c.355) URI-path (clean):  /info.php
2017-09-13 20:24:50: (response.c.356) URI-query       :
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 1 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 2 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 3 (cached) result: true
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 4 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 5 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 6 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 7 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 1 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 2 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 3 (cached) result: true
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 4 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 5 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 6 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 7 (cached) result: false
2017-09-13 20:24:50: (mod_access.c.148) -- mod_access_uri_handler called
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 1 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 2 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 3 (cached) result: true
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 4 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 5 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 6 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 7 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 1 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 2 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 3 (cached) result: true
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 4 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 5 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 6 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 7 (cached) result: false
2017-09-13 20:24:50: (response.c.490) -- before doc_root
2017-09-13 20:24:50: (response.c.491) Doc-Root     : /var/www
2017-09-13 20:24:50: (response.c.492) Rel-Path     : /info.php
2017-09-13 20:24:50: (response.c.493) Path         :
2017-09-13 20:24:50: (response.c.542) -- after doc_root
2017-09-13 20:24:50: (response.c.543) Doc-Root     : /var/www
2017-09-13 20:24:50: (response.c.544) Rel-Path     : /info.php
2017-09-13 20:24:50: (response.c.545) Path         : /var/www/info.php
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 1 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 2 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 3 (cached) result: true
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 4 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 5 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 6 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 7 (cached) result: false
2017-09-13 20:24:50: (response.c.562) -- logical -> physical
2017-09-13 20:24:50: (response.c.563) Doc-Root     : /var/www
2017-09-13 20:24:50: (response.c.564) Basedir      : /var/www
2017-09-13 20:24:50: (response.c.565) Rel-Path     : /info.php
2017-09-13 20:24:50: (response.c.566) Path         : /var/www/info.php
2017-09-13 20:24:50: (response.c.583) -- handling physical path
2017-09-13 20:24:50: (response.c.584) Path         : /var/www/info.php
2017-09-13 20:24:50: (response.c.591) -- file found
2017-09-13 20:24:50: (response.c.592) Path         : /var/www/info.php
2017-09-13 20:24:50: (response.c.753) -- handling subrequest
2017-09-13 20:24:50: (response.c.754) Path         : /var/www/info.php
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 1 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 2 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 3 (cached) result: true
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 4 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 5 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 6 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 7 (cached) result: false
2017-09-13 20:24:50: (mod_access.c.148) -- mod_access_uri_handler called
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 1 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 2 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 3 (cached) result: true
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 4 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 5 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 6 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 7 (cached) result: false
2017-09-13 20:24:50: (mod_compress.c.839) -- handling file as static file
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 1 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 2 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 3 (cached) result: true
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 4 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 5 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 6 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 7 (cached) result: false
2017-09-13 20:24:50: (mod_fastcgi.c.3500) handling it in mod_fastcgi
2017-09-13 20:24:50: (mod_fastcgi.c.2875) got proc: pid: 0 socket: tcp:10.5.0.3:9000 load: 1
2017-09-13 20:24:50: (mod_fastcgi.c.2543) FastCGI-stderr: Primary script unknown
2017-09-13 20:24:50: (mod_fastcgi.c.1642) released proc: pid: 0 socket: tcp:10.5.0.3:9000 load: 0
2017-09-13 20:24:50: (response.c.122) Response-Header: \nHTTP/1.1 404 Not Found\r\nCache-Control: no-cache\r\nX-Powered-By: PHP/7.1.9\r\nContent-type: text/html; charset=UTF-8\r\nContent-Length: 16\r\nDate: Wed, 13 Sep 2017 23:24:50 GMT\r\nServer: lighttpd/1.4.45\r\n\r\n
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 1 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 2 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 3 (cached) result: true
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 4 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 5 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 6 (cached) result: false
2017-09-13 20:24:50: (configfile-glue.c.694) === start of condition block ===
2017-09-13 20:24:50: (configfile-glue.c.622) 7 (cached) result: false

注意:它在 docker 内部运行以模拟该情况,如果需要一些有关 docker 的附加信息来调试此行为,请告诉我。

答案1

您需要在运行 PHP-FPM 的服务器上拥有 PHP 文件。Lighttpd 仅将请求传递给 PHP-FPM,而不是包含内容的实际文件。

您还需要拥有 PHP 脚本在 PHP-FPM 服务器上可能使用的所有资源。

实现此目的的一个选项是让 lighttpd 计算机通过网络文件系统共享文件。但是,这可能会带来性能问题。

集群文件系统是另一种选择。

我使用lsyncd守护进程将文件从单个系统同步到多个目标。这样,所有 PHP-FPM 服务器都拥有脚本的本地副本,并且文件访问不会产生任何性能开销。

相关内容