Short story: I made the initial implementation of SCGI protocol in PHP. Check it out!
Long story:
Some time ago I was ranting about PHP and it’s “way” of handling FastCGI. The idea is to have persistent PHP-application which would handle requests from inside. This would allow us have “real” persistent cache in application (persistent connection to arbitary resources, preparsed XSLT’s in memory) and reply to queries really fast as we wouldn’t need to load all the classes on each request (classes would be loaded only once, during app initialization). And no, APC and similiar technologies do not completely solve this problem.
I finally found some time (and inspiration) to do something in direction of implementing FastCGI the way I see it. Initially, I was going to implement FastCGI-functions in php-extension, but that would require more time than I currently have, so I started with a simplier task: I implemented SCGI protocol (which is way simplier than FastCGI) in pure php-code (which is easier, again, and let’s me change API faster, during development).
This thing already works and you can test it if you have SCGI-enabled server (apache and lighttpd will do) and PHP 5.2.1+ (cli). At first, you need to get latest sources from my google-code project. Sources come with an example.
Some comments to ease understanding:
- Start by including SCGI/autoload.php file — it will take care of including all SCGI-classes
- Create your class by extending SCGI_Application. It needs to have two methods: public function __construct and protected function requestHandler. Constructor should call parent::__construct optionally with the stream-url. By default, application will listen on tcp://127.0.0.1:9999.
- requestHandler is the function which is called by application on every request. This is the point where most of the things should happen. Request-data is available from $this->request() and Response-data should be given to $this->response(). See example for details
- To see some result you need to tell your web-server, where your application’s socket is. Example for Lighttpd is available in repository
Warning: This is the pre-pre-release. API will be changing.
UPDATE: Code of this project was moved to github: http://github.com/indeyets/appserver-in-php

Hello Alexey,
I have started to use your library and has been working great.
I have found some problems with the HTTP/PostRequest.class.php
apparently you have to replace throw new RuntimeException to throw new RuntimeException else the system looks for it in the MFSAppServerHTTP namespace.
PS: I would like to join on your google project for appserver in php.
Similarly, I believe throw new LogicalException should be throw new LogicException (Logic not Logical), and for these to be caught in MyApp, _construct needs try/catch:
try {
parent::__construct($socket_url);
}
catch (LogicalException $e) {
echo 'Caught exception: ', $e->getMessage(), “n”;
}
With Cheroke http server you can define multiple instances (Information Sources — type=Remote Host) of the AppServer running on different ports and let its built-in Round Robin capability distribute the load.
I'm new but open to the idea of running PHP this way. Keep up the great work!
Thanks for the heads up.
I am doing the same the cherokee+10 instances of the app per server.
The PostRequest.class.php has to be sorted out though as it is not accepting requests from pear's HTTP::Request class (from php4)
PS: This is only when the request is using multipart/form-data.
we will see what can be done to fix it.
best regards and thanks for your input.
I just moved project to github. You can find it here: http://github.com/indeyets/appserver-in-php/
feel free to make github-fork of it. I would be glad to pull relevant patches from your tree.
issue with exceptions is fixed there. tell me if I missed anything
I fixed the issue with exceptions. new code can be found at github here: http://github.com/indeyets/appserver-in-php/
regarding catching LogicalExceptions — I am not 100% sure, that is a good idea. LogicalException means something really bad, so should be treated as such. Anyway, you are free to do whatever you want in your App-classes
Thanks. I will check it out.
The next thing would be to fix the multipart post handling.
We are getting many error when posting from pear_http_client in php4 to the
scgi app.
I will see when I can give you more informations and test case for it.
I am totally new to Exceptions, so any such “tips” are appreciated.
In my case the LogicException was trying to tell me “You have to run this from the command line!” Oh, you mean it's actually running all the time like a… server. Heh.
It's so counterintuitive — I love it.
Loading all your variables, objects, arrays, XML, etc. before receiving a single request. Brilliant! Leaves me wondering why this approach is not more common.
I recently coded fcgi php application server! It works fine with NGINX web server on FreeBSD. I tested it on sample application and got no memory leakage at all. Here is the link to project's google code page http://code.google.com/p/ultramilk/
As I promised 5 months ago, I have successfully finished my master thesis Distributed web framework. Basically, in your terms it is a distributed application server for PHP. It is currently using the SCGI protocol to communicate with web servers and the MCGI protocol (multiplexed SCGI) for inter node communication.
Check it out on http://bondari.net/?p=133
For those who interested I recommend to start with the presentation and not this the paper.
Big thanks to vintikzzz for the FCGI implementation.
I read your presentation. That's quite a beast. Reminded me of http://xscript.opensource.yandex.net/
Do you plan to make any user-level tutorials for you project? I mean: how difficult would it be to write “hello world” app?
Cool. I will look at it.
I noticed, that you use LGPL license. Is that a prinicipal moment?
My code is BSD-licensed, so, I won't be able to use your code in my project, for example
Paper chapter Evaluation contains the description of several examples, short but functional applications, including the very basic content management system. These applications are very small and pretty well documented.
General audience won't read paper. Then need short and easy step-by-step examples
Hmmm… I want to use this code with framework that under LGPL licence too!
You can use BSD-licensed code in any project (BSD, LGPL, GPL or even Close-sourced), as long as license is mentioned.
BSD-license is really simple an gives maximum number of freedoms, which is, why I prefer it for “core” components.
http://en.wikipedia.org/wiki/BSD_License
I read it. I found that BSD license is GPL compatible! Maybe I miss something?
It means that BSD code can be used in GPL-project. Because BSD code can be used in ANY project.
Unfortunately, it doesn't work other way round. You can't use (L)GPL components in BSD project, because (L)GPL is a viral license
Thesis paper is usually less formal and more user friendly rather than a typical research paper. It assumes reader has only some basic-to-average knowledge of the area. It also contains some very well illustrated and explained subsystems and example applications.
I think PHP is not an ideal thing for a process that:
- holds ONE connection with the webserver
- forks off it's own children and watches them in an intelligent way, so that their number matches the current load for instance
- bridge communication between every child and webserver connection (IMHO that's the worst part for doing in php)
What would be cool is a general SCGI multiplexer (ideally written in C or derivatives) that listens on a scgi port, forks off child processes and forwards all comunication between children and web server. Actually as far as I see it, this program does not have to know anything about SCGI protocol. To use your php app server without modification, the multiplexer could simply let every child start your runner.php listening on a different TCP port, immediately connect to this port and blindly relay communication with web server. Anyone knows of some general software capable of acomplishing such a thing? This is not classical tcp server, because tcp is used instead of IPC
BTW, just to be sure, do I see it right, that your php-app solution as it is – is only capable of processing all requests in a sequential order within one process, that is runner.php?
You are right. Current app-server components work sequentially.
Regarding multiplexer: Actually, I think, that such multiplexer should be the part of the server. And, I believe, lighttpd can do this (it is possible to specify pool of scgi-daemons, instead of one daemon). Anyway, I didn't try it, but that sounds like a proper way of doing things.
If not.. Well, it should be trivial to write one using your description
Hey, thanks for fast reply!
You people have to stop trying to do everything with php. Learn other languages and use them for such cases, there are better solutions than this. It's not my intention to flame, I'm just making a point: In theory I could wipe my ass with a piece of sandpaper, but that's not what it was made for.
You are underestimating both me and php
I know quite a lot of programming languages. Enough to stop thinking about learning new language as about the achievement.
And php really can be used for much more than simple web-scripting. It made a long way since php3-days and is suitable for this kind of tasks