如何使用php5-fpm池套接字执行php文件? [英] How to execute a php file using a php5-fpm pool socket?
问题描述
我需要从命令行执行php脚本,但是如果直接调用 php5 myfile.php,则会遇到一些安全问题(主要是openbasedir限制和用户组权限)。
I need to execute a php script from the command line, but if I call directly "php5 myfile.php", I will have some security issue (mainly the openbasedir restrictions and the user&group rights).
所以我想从与fpm进程相同的约束条件执行该php文件( /etc/php5/fpm/pool.d/specific_process.conf
)。此过程在 /var/run/php5-fpm-specific.sock
中有一个sock文件,我相信它会像在conf文件中一样受到约束(相同的用户&组,一些php_admin_value等)。
So I'd like to execute that php file from the same constraints as a fpm process (/etc/php5/fpm/pool.d/specific_process.conf
). This process has a sock file at /var/run/php5-fpm-specific.sock
, which, I believe, would be constrained like in the conf file (same user&group, some php_admin_value, etc).
但是我看不到如何从命令行以及提供一些参数来做到这一点。
But I can't see how I can do that from the command line, and by giving some arguments.
我尝试过类似的操作:
php5 --bindpath /var/run/php5-fpm-specific.sock -f /path/to/my/file.php param1 param2
但是当然,它不起作用。我该怎么办?
But of course it does not work. How can I do ?
注意:我正在调用的文件需要一些参数(在这里, param1
和 param2
)。
Note: The file I'm calling expects some parameters (here, param1
and param2
).
感谢您的帮助。
推荐答案
您将需要可执行文件 cgi-fcgi
(在 libfcgi0ldbl $ c $的Debian部分中) c>包),那么您可以通过执行以下命令来做到这一点(这是一行,其中
\
转义了换行符,您应该可以将其粘贴到您的shell中,例如此):
You will need the executable cgi-fcgi
(in Debian part of the libfcgi0ldbl
package), then you can do it by executing this command (this is one line with \
escaping the newlines, you should be able to paste this to your shell like this):
SCRIPT_NAME=/file.php \
SCRIPT_FILENAME=/path/to/my/file.php \
REQUEST_METHOD=GET \
QUERY_STRING=param1=x\¶m2=y \
cgi-fcgi -bind -connect /var/run/php5-fpm-specific.sock
然后您将收到输出,因为它将被发送到HTTP服务器,因此它将包含HTTP标头,例如包含<?php echo时间为,date( H:i:s);
:
You will then receive the output, as it would be sent to the HTTP server, so it will include the HTTP headers, for example for a script containing <?php echo "The time is ", date("H:i:s");
:
Content-type: text/html
The time is 13:46:35
还有两个参数,但是这些是最重要的参数(请参阅它们如何映射到 $ _ SERVER
数组,这就是后台发生的事情):
There are a couple of more parameters but these are the most essential ones (see how they map to the $_SERVER
array, that's what's happening in the background):
-
SCRIPT_NAME
这是从HTTP端看到的脚本名称。在我的示例中,可以通过http://localhost/file.php
-
SCRIPT_FILENAME访问文件
这是脚本的本地路径-这是HTTP服务器通常从URL确定的内容,在这里您需要自行指定 - <$ c如果您还想在URL中的
?
之后传递某些内容,可以使用$ c> QUERY_STRING ,请注意,我们在外壳中,因此您需要像这样转义&符号:\&
SCRIPT_NAME
this is the script name as it is seen from the HTTP side. In my example the file could have been accessed viahttp://localhost/file.php
SCRIPT_FILENAME
this is the local path to the script -- it's what the HTTP server will usually determine from the URL, here you need to specify it yourselfQUERY_STRING
can be used if you also want to pass in something that would be after the?
in a URL, be aware that we are in a shell, so you'd need to escape the ampersand like this:\&
另请参见:
- FastCGI示例以获取更多参数。
- 直接连接到PHP-FPM 。
- FastCGI开发人员工具包
- FastCGI Example in the Nginx documentation for more parameters.
- Directly connect to PHP-FPM.
- FastCGI Developer's Kit
这篇关于如何使用php5-fpm池套接字执行php文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!