Search code examples
phpc++webserver

What should I do to execute php scripts on my own web server?


I want to create own web server to accept HTTP requests and send response to client.

I have idea to call PHP scripts by CLI interface but then PHP scripts executed in CLI mode. What I need do for call scripts by my own server (not in CLI mode)? Because in CLI mode some PHP abilities is disabled. Maybe I need to write own PHP SAPI?

Please help me to start.

Thanks


Solution

  • No, you don't need to write your own PHP SAPI, as there has already been one written specifically for interfacing with a web server. It's called FastCGI.

    It's important to note that PHP is both extensible and embedded by design. So, while some SAPIs like the Apache 2.0 Handler (a.k.a mod_php) might be embedded in the httpd server directly, it is not typically necessary to do so in order to have the web server talk to PHP.

    The difference is you still need some process to manage the underlying PHP interpreter and deal with things like recycling workers or managing the number worker processes available for the webserver to talk to. For example, php-fpm does this quite nicely and many people use php-fpm to manage the PHP workers and just tie that to their webservers like nginx or httpd via the fastcgi protocol. The PHP workers forked by php-fpm listen on a fastcgi socket and the webserver can freely send and receive information from and to PHP.