phpdaemon installation, configuration and usage tips & tricks

July 16, 2011 Zend Framework

If you own a website with too many AJAX requests like autocomplete or real-time features like auction bidding that require quick response and you also use Zend Framework or any other PHP heavy library/framework on your backend, then you end up with way too many HTTP requests to the backend each of them requires framework bootstrapping. You can launch any PHP profiler like xdebug to see that 90% of time is spent on performing bootstrapping procedures. Therefore, high cpu load -> fewer requests processed. So the idea of keeping loaded application in memory seems obvious. Luckily, there are some libraries on the market to solve the problem. One of them is phpdaemon which can load your application into memory, launch several worker processes and act like HTTP or FastCGI server.

Let’s consider a web application built on Zend Framework – an online auction. We need to bid on any item, process the bid really fast and update user on other bids as quickly as possible. So the urls for these actions are “/deal/%auction_id%/buy” and “/pull/%auction_id%” (get the currently winning bidder). We’d like to send /pull requests every 150-200 ms. In this article we won’t consider such techniques as jssocket or jXMLSocket, we’ll use JSONP requests.

Same thing with “/bid/%auction_id%” url.

Installation of phpdaemon:








Now you should be done. You can try to “phpd start” and then “phpd fullstatus” commands , you should see something like :

We’ll use a separate web server for handling all these JSONP requests in order to not load (even with proxy_pass) our main web server. Nginx is a fast web server, so we decided to use it. Let’s see the config of it to support these URLs:

As you can see it handles only mentioned above requests (regexp location) and forwards them to a FastCGI server implemented by phpdaemon. And also sets an environment variable APPNAME which is necessary for the daemonized application.

In order to tell phpdaemon how to serve our app, we need to create a so called class responder where we give instructions to the daemon on how to start , shutdown and run our app. I used a class written by this guy and it can be used by any ZF powered PHP application. Here is the code of it:

You can use Daemon::log() handful method for debugging and logging in your app, however, I’d not recommend leaving any logging in onFinish method of HTTPRequest class as it will be launched on every request so your phpdaemon log will grow too fast. In the class we see an extended version of AppInstance class where we set bootstrapping and point to the class serving requests. In ZfconfRequest extedned from HTTPRequest we set ZF front controller request/response classes, tell ZF to return the response rather than put it to output and dispatch the request then. Be careful as dispatching requires correct setup of web server’s env variables like baseUrl, script name , etc. So you may refer to nginx config above to see how we pass SCRIPT_NAME variable. Also, pay attention at the $this->header line in onFinish() – we added it because we serve only json responses with this setup, if you’re planning to return html or something else, remove it then. This is just a demo of how to set an HTTP header from daemon.

Now if you start the daemon by /path/to/phpd start command, you should see that several workers (depending on phpd config, see /path/to/phpd/config.conf settings max-workers, min-workers, start-workers). Try to test it by navigating with your browser to any of urls with some parameters. In our case it is https://coolsite.com:8091/pull/11111. If you don’t see a response or there is an error you can debug your app with Daemon::log(). If you setup a separate PHP distribution to work with phpdaemon, be sure you install all PECL extensions you use in your app – I experienced an error that was hard to debug in this environment with absent memcache extension.

Greg

October 10, 2011 at 9:49 pm

This doesn't work. There is no instruction on WHERE to put the ZconfApp.php, and the server says nothing but ZconfApp doesn't exist. Does ANYONE have ANY working documentation for phpd?
Reply

    admin Post Author

    October 12, 2011 at 10:55 am

    Hello Greg, sorry, I missed this point when wrote the post. You can check this page of official documentation: https://github.com/kakserpom/phpdaemon/wiki/Configuration where it says you can set the app name in three different ways:

    • either edit defaultApp property in appResolver.php
    • or make webserver pass APPNAME parameter set to yourapplication:
    • for nginx set fastcgi_param APPNAME “yourapplication”; option (see also http://wiki.nginx.org/NginxHttpFcgiModule)
    We chose the third option and nginx config above in the article you can see this string: fastcgi_param APPNAME ZfconfApp; Also, you can put the application class code into this folder: /path/where/you/copied/phpdaemon/applications
    Reply


Leave a Reply

Your email address will not be published