dongren5293 2012-07-16 04:17
浏览 86
已采纳

如何从c ++调用php-cgi

So I'm writing a web server (I know, like the world needs another one!) And I want to call php through CGI. I've got a nice working CGI1.1 implementation good enough to run perl cgi scripts for example.

Now I want to run php. But when my cgi module calls the php program it returns a 404.

How do I get this working?

The details:

I'm running on ubuntu with php5. In my web server, I pass cgi requests to a handler that will find the program and call it with the sc ript as an argument. So here is the code that spawns the cgi process:

chdir( dir );
execve( argv[0], argv.data(), (char* const*) req.posixEnv() );

The command I'm running is equivalent to:

/usr/bin/php5-cgi /home/rafael/webserver/data/cgi-bin/test.php

This means I've set "dir" to:

/home/rafael/webserver/data/cgi-bin

And the cgi process should run in that directory. Argv[0] is "/usr/bin/php5-cgi", argv.data() returns a null terminated array with the following elements:

/usr/bin/php5-cgi
/home/rafael/webserver/data/cgi-bin/test.php
NULL

The process environment contains the CGI environment that includes a block of the usual CGI environment variables.

When I run the above command from a shell, that is by literally typing it into a shell, the script runs fine and php generates the test page. When I call it from CGI it returns:

Status: 404 Not Found
X-Powered-By: PHP/5.3.10-1ubuntu3.2
Content-type: text/html

No input file specified.

Now I know more or less what is happening. PHP is detecting if it is running in CLI or CGI mode, and searching for the script to run differently in each mode. In CGI mode it is probably using CGI environment variables like SCRIPT_FILENAME and DOCUMENT_ROOT to resolve the script name. I've tried a whole bunch of things - different settings for these variables. But none of it works.

What I do know: The whole spawning of PHP, and reading output works. For example if I remove certain CGI variables such that it thinks it is running in CLI mode it works great - generates the page and everything.

What I need to know is:

  1. what is the right way to set up the CGI environment variables for the php-cgi to find my script.
  2. what is the right way to call a php script from exec. I'm using full paths for everything as an effort to debug this. I'd prefer to use relative paths.

All the documentation I can find is about configuring php for cgi on particular web servers. I need to know this from the perspective of someone writing a server.

@EDIT: I have a simpler example that eliminates c++. I'm pretty sure that part is working correctly anyway. Say I make a script that calls php-cgi, and I try to emulate the cgi calling conventions in a shell.

This works:

#export GATEWAY_INTERFACE="CGI/1.1"                                                                                             
export REDIRECT_STATUS=200
export SERVER_PROTOCOL="HTTP/1.1"
export DOCUMENT_ROOT="/home/rafael/web"
#export SCRIPT_FILENAME="cgi-bin/test.php"                                                                                      
export SCRIPT_NAME="cgi-bin/test.php"

/usr/bin/php5-cgi test.php

But this doesn't:

export GATEWAY_INTERFACE="CGI/1.1"                                                                                             
export REDIRECT_STATUS=200
export SERVER_PROTOCOL="HTTP/1.1"
export DOCUMENT_ROOT="/home/rafael/web"
#export SCRIPT_FILENAME="cgi-bin/test.php"                                                                                      
export SCRIPT_NAME="cgi-bin/test.php"

/usr/bin/php5-cgi test.php

I think probably some combination of DOCUMENT_ROOT, SCRIPT_FILENAME and SCRIPT_NAME should work.

  • 写回答

1条回答 默认 最新

  • duanjiaolia97750 2012-07-19 12:55
    关注

    The thing that was messing it up was the setting of "doc_root" in php.ini. I unset that like so:

    doc_root =
    

    And now cgi environment variables work much more like the CGI spec says. Eg. The following thing now works:

    export GATEWAY_INTERFACE="CGI/1.1"
    export SERVER_PROTOCOL="HTTP/1.1"
    export SCRIPT_FILENAME="/home/rafael/web/cgi-bin/test.php"
    export SCRIPT_NAME="test.php"
    export REDIRECT_STATUS="200"
    
    /usr/bin/php5-cgi test.php
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置