Call PHP from a virtual / user "web server"

Basically, I'm trying to figure out how PHP can be called from a "web server".

I read the documentation, but that didn't help much.

As far as I can tell, there are three ways to call PHP:

  • through the command line (for example: php -f "/path/to/script.php" )
  • via CGI (??) / via FastCGI (???)
  • through a web server (ex: Apache)

So, let's start with CGI . Maybe I'm just blind, but the specification does not mention how on the ground the web server transmits data (headers and callbacks) for the implementation of CGI. The situation is even worse with FastCGI .

Next, we have server modules, which, I don’t even know what to look for, since all the bends eventually did not go away.

+6
source share
2 answers

Calling a CGI script is pretty simple. PHP has several features, but you basically need to set up a list of environment variables, and then call the PHP-CGI binary:

 setenv GATEWAY_INTERFACE="CGI/1.1" setenv SCRIPT_FILENAME=/path/to/script.php setenv QUERY_STRING="id=123&name=title&parm=333" setenv REQUEST_METHOD="GET" ... exec /usr/bin/php-cgi 

Most of them are templates. SCRIPT_FILENAME is how to pass the actual php file name to the PHP interpreter, and not as the exec parameter. Also decisive for PHP is the non-standard variable REDIRECT_STATUS=200 .

For a GET request, you only need environment variables. For a POST request, you simply pass the body of the HTTP request as stdin to the php-cgi binary executable. The returned stdout is a CGI response consisting of an incomplete HTTP header, \ r \ n \ r \ n, and the body of the page.

(Only from memory. Perhaps a few more errors).

+7
source

FastCGI is probably the best option, since it is so wisely used, it will give you language independence (for example, you could look into Ruby later), and well documented with lots of examples .

You can actually write your own Server API , but it is more complicated than the FastCGI implementation and has several drawbacks.

I would not even bother with direct CGIs at all, FastCGI exists for some reason.

+1
source

Source: https://habr.com/ru/post/894954/


All Articles