<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>PHPFastCGI</title>
    <description>PHPFastCGI is a collection of libraries that can be used to build FastCGI applications in PHP. Unlike normal PHP applications, these applications can stay alive between request cycles - improving speed and lowering resource use.
</description>
    <link>http://phpfastcgi.github.io//</link>
    <atom:link href="http://phpfastcgi.github.io//feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Fri, 02 Dec 2016 11:27:45 +0000</pubDate>
    <lastBuildDate>Fri, 02 Dec 2016 11:27:45 +0000</lastBuildDate>
    <generator>Jekyll v3.3.1</generator>
    
      <item>
        <title>280 rq/s to 1770 rq/s - Speed up Symfony, Silex and Slim with PHPFastCGI</title>
        <description>&lt;h3 id=&quot;the-what&quot;&gt;The “What”&lt;/h3&gt;

&lt;p&gt;Let me start by clearing up a possible source of confusion for those of you familiar with FastCGI. This project is not about PHP-FPM. PHP-FPM is a great way of improving the performance of PHP applications, but the way it works is different to PHPFastCGI.&lt;/p&gt;

&lt;p&gt;PHP-FPM keeps the &lt;strong&gt;PHP interpreter&lt;/strong&gt; alive between HTTP request cycles.&lt;/p&gt;

&lt;p&gt;PHPFastCGI keeps the &lt;strong&gt;PHP application&lt;/strong&gt; alive between HTTP request cycles.&lt;/p&gt;

&lt;p&gt;That’s all of your services, configuration… everything.&lt;/p&gt;

&lt;h3 id=&quot;the-why&quot;&gt;The “Why”&lt;/h3&gt;

&lt;p&gt;For a simple 500 page Symfony application, PHPFastCGI can take the performance from 280 rq/s to 1770 rq/s. If you’re interested, check the &lt;a href=&quot;/general/2015/08/24/phpfastcgi-benchmarks-symfony-silex-slim.html&quot;&gt;post on how the benchmarks were conducted&lt;/a&gt;. To summarize, I tried to make the Symfony application as fast as I could (OPCache enabled, PHP-FPM, NGINX). Then, I ran it as a PHPFastCGI application and compared the performance.&lt;/p&gt;

&lt;p&gt;Please do not take those benchmarks too seriously (though I believe that they are significant). The top results were collected using a PHP extension that is currently lacking a few features (and not as stable as the default PHP implementation of the protocol). Also, a very helpful commenter has pointed out some ways that I could improve the benchmarks. As a result, I will be re-running them shortly.&lt;/p&gt;

&lt;p&gt;Regardless, any application that has to reload itself on &lt;strong&gt;every HTTP request cycle&lt;/strong&gt; will be significantly slower than a version of the same application that does not.&lt;/p&gt;

&lt;h3 id=&quot;the-how&quot;&gt;The “How”&lt;/h3&gt;

&lt;p&gt;Before doing this with an application, please read the post on &lt;a href=&quot;/general/2015/08/21/things-to-consider-using-phpfastcgi.html&quot;&gt;things to consider when using PHPFastCGI with your application&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If your still think that your application is ready to be run in this way, it is &lt;strong&gt;really easy&lt;/strong&gt; to create a FastCGI application using PHPFastCGI. With a Symfony application, you can do it by just &lt;a href=&quot;/general/2015/07/14/making-symfony-faster.html&quot;&gt;installing a bundle&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The project also includes a &lt;a href=&quot;http://github.com/PHPFastCGI/SilexAdapter&quot;&gt;Silex Adapter&lt;/a&gt; and a &lt;a href=&quot;http://github.com/PHPFastCGI/SlimAdapter&quot;&gt;Slim Adapter&lt;/a&gt;. These provide wrappers for the core application classes of each framework.&lt;/p&gt;

&lt;p&gt;You can also use the project without a framework, using the &lt;a href=&quot;http://github.com/PHPFastCGI/FastCGIDaemon&quot;&gt;core FastCGIDaemon&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A framework-less PHPFastCGI application looks like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// command.php
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Include the composer autoloader
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;require_once&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dirname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;__FILE__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/../vendor/autoload.php'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PHPFastCGI\FastCGIDaemon\ApplicationFactory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PHPFastCGI\FastCGIDaemon\Http\RequestInterface&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Zend\Diactoros\Response\HtmlResponse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// A simple kernel. This is the core of your application
&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$kernel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;RequestInterface&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// $request-&amp;gt;getServerRequest()         returns PSR-7 server request object
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// $request-&amp;gt;getHttpFoundationRequest() returns HTTP foundation request object
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;HtmlResponse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'&amp;lt;h1&amp;gt;Hello, World!&amp;lt;/h1&amp;gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Create your Symfony console application using the factory
&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$application&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ApplicationFactory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createApplication&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Run the Symfony console application
&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$application&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;references&quot;&gt;References&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://github.com/PHPFastCGI/FastCGIDaemon&quot;&gt;FastCGIDaemon&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://github.com/PHPFastCGI/SpeedfonyBundle&quot;&gt;Speedfony Bundle&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://github.com/PHPFastCGI/SlimAdapter&quot;&gt;Slim v3 Adapter&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://github.com/PHPFastCGI/SilexAdapter&quot;&gt;Silex Adapter&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have any suggestions or queries, please use the comment section below, &lt;a href=&quot;http://github.com/PHPFastCGI/FastCGIDaemon&quot;&gt;GitHub&lt;/a&gt; or &lt;a href=&quot;http://twitter.com/AndrewCarterUK&quot;&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Also - as a cheeky request - if you like the sound of this project it would make me very happy if you shared it with your developer friends or gave it a star on GitHub.&lt;/p&gt;
</description>
        <pubDate>Wed, 04 Nov 2015 01:00:00 +0000</pubDate>
        <link>http://phpfastcgi.github.io//general/2015/11/04/introducing-phpfastcgi.html</link>
        <guid isPermaLink="true">http://phpfastcgi.github.io//general/2015/11/04/introducing-phpfastcgi.html</guid>
        
        
        <category>general</category>
        
      </item>
    
      <item>
        <title>PHPFastCGI Benchmarks - Symfony, Silex and Slim</title>
        <description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;I have conducted a series of benchmark tests to illustrate the performance increases achievable when using the PHPFastCGI project. All of these tests have been conducted in good faith.&lt;/p&gt;

&lt;p&gt;My purpose is not to advise you which is the best framework to use, or even to convince you that using PHPFastCGI in your application would be a good idea. &lt;strong&gt;For many PHP developers, the number of requests per second that their application can handle is not important&lt;/strong&gt;. My purpose is only to prove that if you wish to develop high performance PHP applications, PHPFastCGI (and the technique it uses) can help.&lt;/p&gt;

&lt;h2 id=&quot;benchmarking-system&quot;&gt;Benchmarking System&lt;/h2&gt;

&lt;p&gt;Running inside VMWare Fusion: Ubuntu 64-bit Server 15.04, NGINX, PHP 5.6.4, 2GB RAM and 4 cores (Intel Core i7, 3.4 GHz).&lt;/p&gt;

&lt;p&gt;For each of the frameworks, the application was initially benchmarked using the PHP FastCGI Process Manager (PHP-FPM) with the OPcache enabled.&lt;/p&gt;

&lt;p&gt;For the PHPFastCGI benchmarks, a command line application was created for each of the frameworks using the appropriate adapter. Six instances of each application were then launched and NGINX’s inbuilt load balancer was used in round-robin mode. The benchmarks for the PHPFastCGI project were run twice, firstly using the PHP implementation of the FastCGI protocol and then using the php5-fastcgi extension.&lt;/p&gt;

&lt;p&gt;Whilst PHP-FPM and PHPFastCGI sound similar, there is a very important difference:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;PHP-FPM uses FastCGI to keep the PHP process alive between HTTP requests.&lt;/li&gt;
  &lt;li&gt;PHPFastCGI uses FastCGI to keep the PHP process &lt;strong&gt;and application&lt;/strong&gt; alive between HTTP requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because of this difference, &lt;strong&gt;applications using PHPFastCGI have to be developed very carefully&lt;/strong&gt;. Read this article on &lt;a href=&quot;/general/2015/08/21/things-to-consider-using-phpfastcgi.html&quot;&gt;things to consider when using PHPFastCGI with your application&lt;/a&gt; if you wish to know more.&lt;/p&gt;

&lt;p&gt;The ‘ab’ benchmarking tool was used to conduct the tests. It was configured for 50000 requests at a concurrency level of 20.&lt;/p&gt;

&lt;h2 id=&quot;the-tests&quot;&gt;The Tests&lt;/h2&gt;

&lt;p&gt;The first three tables show the benchmark results for simple ‘Hello, World!’ applications. The number of requests per second is a pretty useless figure here; what is more important is the &lt;strong&gt;reduction in the time taken to handle each request&lt;/strong&gt;, as this indicates the amount of time shaved off the bootstrapping of the framework.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Symfony&lt;/th&gt;
      &lt;th&gt;Requests Per Second&lt;/th&gt;
      &lt;th&gt;Time Per Request (ms)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;NGINX + PHP-FPM&lt;/td&gt;
      &lt;td&gt;551.59&lt;/td&gt;
      &lt;td&gt;1.813&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;NGINX + PHPFastCGI&lt;/td&gt;
      &lt;td&gt;1739.16&lt;/td&gt;
      &lt;td&gt;0.575&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;NGINX + PHPFastCGI (ext)&lt;/td&gt;
      &lt;td&gt;2469.36&lt;/td&gt;
      &lt;td&gt;0.405&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Silex&lt;/th&gt;
      &lt;th&gt;Requests Per Second&lt;/th&gt;
      &lt;th&gt;Time Per Request (ms)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;NGINX + PHP-FPM&lt;/td&gt;
      &lt;td&gt;1424.30&lt;/td&gt;
      &lt;td&gt;0.702&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;NGINX + PHPFastCGI&lt;/td&gt;
      &lt;td&gt;2332.22&lt;/td&gt;
      &lt;td&gt;0.429&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;NGINX + PHPFastCGI (ext)&lt;/td&gt;
      &lt;td&gt;4148.08&lt;/td&gt;
      &lt;td&gt;0.241&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Slim (v3)&lt;/th&gt;
      &lt;th&gt;Requests Per Second&lt;/th&gt;
      &lt;th&gt;Time Per Request (ms)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;NGINX + PHP-FPM&lt;/td&gt;
      &lt;td&gt;2058.91&lt;/td&gt;
      &lt;td&gt;0.486&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;NGINX + PHPFastCGI&lt;/td&gt;
      &lt;td&gt;2734.21&lt;/td&gt;
      &lt;td&gt;0.366&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;NGINX + PHPFastCGI (ext)&lt;/td&gt;
      &lt;td&gt;5562.47&lt;/td&gt;
      &lt;td&gt;0.180&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;In general, &lt;strong&gt;if the application is doing more there are greater potential time savings to be had&lt;/strong&gt;. This is why the increase in performance of the micro-frameworks is less significant than that of the full stack Symfony framework. This is also why ‘Hello, World!’ benchmarks are actually the &lt;strong&gt;least impressive&lt;/strong&gt; benchmarks for the PHPFastCGI project.&lt;/p&gt;

&lt;p&gt;For a more realistic benchmark, I created a small 500 page website application using the Symfony framework. This has a single route configured that selects a random page from the database (MySQL) and renders it using Twig. The Doctrine entity repository is cleared after each request, so the framework is not caching the results of the queries. A much greater performance can be achieved by not doing this, but the purpose of this benchmark is to test when the application has actual work to do - so I have not cheated.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;500 Page Website&lt;/th&gt;
      &lt;th&gt;Requests Per Second&lt;/th&gt;
      &lt;th&gt;Time Per Request (ms)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;NGINX + PHP-FPM&lt;/td&gt;
      &lt;td&gt;282.55&lt;/td&gt;
      &lt;td&gt;3.539&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;NGINX + PHPFastCGI&lt;/td&gt;
      &lt;td&gt;1334.64&lt;/td&gt;
      &lt;td&gt;0.749&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;NGINX + PHPFastCGI (ext)&lt;/td&gt;
      &lt;td&gt;1769.94&lt;/td&gt;
      &lt;td&gt;0.565&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;author-notes&quot;&gt;Author Notes&lt;/h2&gt;

&lt;p&gt;Whilst I hope you agree that the result of these benchmarks is impressive, there is more work to be done! This project is still not stable, and I welcome any contributions that you may have. In particular, the &lt;a href=&quot;https://github.com/PHPFastCGI/php5-fastcgi&quot;&gt;php5-fastcgi extension&lt;/a&gt; would benefit from a review by someone with a knowledge of PHP internals.&lt;/p&gt;

&lt;p&gt;Also, if you are attending ConFoo and would like to hear more about this project then &lt;a href=&quot;http://confoo.ca/en/call-for-papers/speaker/andrew-carter&quot;&gt;please vote for my talk proposal&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/PHPFastCGI/SpeedfonyBundle&quot;&gt;Speedfony Bundle&lt;/a&gt; for Symfony&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/PHPFastCGI/SilexAdapter&quot;&gt;Silex Adapter&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/PHPFastCGI/SlimAdapter&quot;&gt;Slim Adapter&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/PHPFastCGI/SpeedfonyBenchmarkingApp&quot;&gt;Speedfony Benchmarking App&lt;/a&gt; (500 page website)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You may also be interested in the core &lt;a href=&quot;https://github.com/PHPFastCGI/FastCGIDaemon&quot;&gt;FastCGIDaemon Package&lt;/a&gt;, which can be used to create FastCGI applications without a framework (or with a different one).&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;https://github.com/PHPFastCGI/php5-fastcgi&quot;&gt;php5-fastcgi extension&lt;/a&gt; is still being developed and tested. Thus, there have been no releases of this software yet and the integration of this extension into the FastCGIDaemon core can only be found on the &lt;a href=&quot;https://github.com/PHPFastCGI/FastCGIDaemon/tree/php5-fastcgi-ext&quot;&gt;php5-fastcgi-ext branch&lt;/a&gt; of the repository.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was revised on 29th August 2015 to contain more accurate benchmarks. Changes include turning on the opcache and significant changes to the FastCGIDaemon core.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was revised again on 9th September 2015 to include benchmarks using the php5-fastcgi extension currently under development.&lt;/em&gt;&lt;/p&gt;
</description>
        <pubDate>Mon, 24 Aug 2015 00:25:00 +0000</pubDate>
        <link>http://phpfastcgi.github.io//general/2015/08/24/phpfastcgi-benchmarks-symfony-silex-slim.html</link>
        <guid isPermaLink="true">http://phpfastcgi.github.io//general/2015/08/24/phpfastcgi-benchmarks-symfony-silex-slim.html</guid>
        
        
        <category>general</category>
        
      </item>
    
      <item>
        <title>Things to consider when developing an application with PHPFastCGI</title>
        <description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;Let me start with something very important.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Not every application is a suitable candidate to become a FastCGI application.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In conventional PHP applications, HTTP requests are nicely separated from each other. Web servers handle each HTTP request as a different execution of an application. This provides a degree of &lt;strong&gt;safety&lt;/strong&gt; and &lt;strong&gt;security&lt;/strong&gt;. If you choose to run your PHP application as a FastCGI application, you forefit this for an increase in performance.&lt;/p&gt;

&lt;h2 id=&quot;fatal-errors-and-exceptions&quot;&gt;Fatal Errors and Exceptions&lt;/h2&gt;

&lt;p&gt;A properly written application should not respond to clients with uncaught errors and exceptions. The consequences, however, of doing this in a FastCGI application are much steeper.&lt;/p&gt;

&lt;p&gt;A fatal error will cause your application to terminate and stop listening for requests. Something will need to restart your application otherwise it will stay down.&lt;/p&gt;

&lt;p&gt;The best practice is to make sure that all errors and exceptions are appropriately caught and logged without terminating the application. You should also make sure that your application is being managed by a suitable process manager so that if it does crash, it will be promptly restarted.&lt;/p&gt;

&lt;p&gt;Apache’s &lt;a href=&quot;http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html&quot; title=&quot;Apache module: mod_fastcgi&quot;&gt;mod_fastcgi&lt;/a&gt; includes a process manager that is very easy to configure. If you are using a web server such as NGINX, you may need to use a process manager such as &lt;a href=&quot;http://supervisord.org/&quot; title=&quot;Supervisor: A process control system:&quot;&gt;supervisord&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;code-updates-and-deployment&quot;&gt;Code Updates and Deployment&lt;/h2&gt;

&lt;p&gt;Usually when we make changes to our application, these are recognised immediately as PHP rereads our file every time it executes it. In some scenarios, we may have to flush caches but that is about as tricky as it usually gets. When working with long running processes, we have to make sure that we shutdown and reload our application when we update its code.&lt;/p&gt;

&lt;p&gt;This can be an easy step to forget! Many times I have found myself staring blankly at error messages before remembering that I needed to reload the application whilst developing this very project!&lt;/p&gt;

&lt;p&gt;I am currently working on features to ensure that shutdown signals cause PHPFastCGI applications to exit cleanly after they have finished handling and responding to all open requests.&lt;/p&gt;

&lt;p&gt;When running multiple instances of your FastCGI application, you should desire a configuration that allows you to shutdown and restart instances individually to prevent down time. Running multiple instances of your application is also recommended for performance reasons.&lt;/p&gt;

&lt;p&gt;If you are using apache’s &lt;a href=&quot;http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html&quot; title=&quot;Apache module: mod_fastcgi&quot;&gt;mod_fastcgi&lt;/a&gt; to process manage your application, a graceful reload of the web server may be your best option.&lt;/p&gt;

&lt;h2 id=&quot;memory-segregation&quot;&gt;Memory Segregation&lt;/h2&gt;

&lt;p&gt;Static memory and global variables are well documented enemies. If you intend to create a FastCGI application, you should avoid these like the plague (that includes $_SESSION, $_POST, $_GET, etc…).&lt;/p&gt;

&lt;p&gt;Any scenario where a HTTP request could influence, interfere or read memory that is shared by a later (or earlier) HTTP request is an opportunity for an attacker.&lt;/p&gt;

&lt;h2 id=&quot;memory-leaks&quot;&gt;Memory Leaks&lt;/h2&gt;

&lt;p&gt;Although this is changing, PHP applications and frameworks are not usually designed to run for long periods of time. Anyone who has ever worked with a low level language (such as C or C++) will understand that the traditional PHP developer approach to memory allocation is incredibly lacking in any form of principal or organization.&lt;/p&gt;

&lt;p&gt;This is an approach that we cannot afford to take when developing long running applications. PHP itself is now very good at cleaning up memory automatically once it is no longer required. Nowadays, most memory leaks in our PHP applications can be traced to dangling references either inside the application or within its dependencies.&lt;/p&gt;

&lt;p&gt;For example, Doctrine’s SQL logger (enabled by default in the Symfony development environment) can very quickly cause a resource issue. If you are using the Doctrine Entity Manager, you must also take care to use the EntityManager::clear() method provided and remove references to entities that you no longer require it to manage.&lt;/p&gt;

&lt;p&gt;One potential approach for handling memory leaks is to restart application instances after they have been operating for a certain amount of time or handled a certain number of requests. This feature is scheduled for integration into the FastCGIDaemon core package and will be available before the first stable release. However, it is still considered best practice to try and develop applications without memory leaks.&lt;/p&gt;

&lt;h2 id=&quot;connection-timeouts&quot;&gt;Connection Timeouts&lt;/h2&gt;

&lt;p&gt;One of the advantages of using PHPFastCGI to keep your application alive is not having to reconnect to databases, cache layers and message queues for every HTTP request. However, should any of these connections timeout or otherwise disconnect, your application must contain logic to restablish these. Failure to do so will leave you with 5XX error codes and possibly useless instances of your application running.&lt;/p&gt;

&lt;p&gt;This is a particularly nasty bug, as you may not notice it until you have deployed to your production server and gone though a quiet patch in the early hours of the morning.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;There is lots to be gained and lots to be lost by daemonizing your PHP applications. It is very important that you are aware of the potential issues you could face and how to mitigate these. However, with a properly designed and carefully considered application - you can reach response speeds well beyond the dreaming limits of conventional PHP applications.&lt;/p&gt;

</description>
        <pubDate>Fri, 21 Aug 2015 09:00:00 +0000</pubDate>
        <link>http://phpfastcgi.github.io//general/2015/08/21/things-to-consider-using-phpfastcgi.html</link>
        <guid isPermaLink="true">http://phpfastcgi.github.io//general/2015/08/21/things-to-consider-using-phpfastcgi.html</guid>
        
        
        <category>general</category>
        
      </item>
    
      <item>
        <title>How to make Symfony faster</title>
        <description>&lt;p&gt;By turning your Symfony application into a FastCGI application, you can keep the application in memory between request cycles.&lt;/p&gt;

&lt;p&gt;Before doing this, please read the post on &lt;a href=&quot;/general/2015/08/21/things-to-consider-using-phpfastcgi.html&quot;&gt;things to consider when using PHPFastCGI with your application&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To do this, open the terminal in your project directory and run the following command:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;php composer.phar require &lt;span class=&quot;s2&quot;&gt;&quot;phpfastcgi/speedfony-bundle&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This command adds the &lt;a href=&quot;http://github.com/PHPFastCGI/SpeedfonyBundle&quot;&gt;Speedfony Bundle&lt;/a&gt; to your composer dependencies.&lt;/p&gt;

&lt;p&gt;Now you need to register the bundle in your AppKernel.php file:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
  public function registerBundles()
  {
    $bundles = array(
      // ...
      new PHPFastCGI\SpeedfonyBundle\PHPFastCGISpeedfonyBundle(),
    );

    // ...
  }
// ...&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Your application can now be run as a FastCGI application listening on FCGI_LISTENSOCK_FILENO (stdin) using this command:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;php app/console speedfony:run --env&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;prod&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;You can also configure the FastCGI application to listen on a TCP address using the ‘port’ and ‘host’ options:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;php app/console speedfony:run --env&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;prod&quot;&lt;/span&gt; --port 5000&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

</description>
        <pubDate>Tue, 14 Jul 2015 10:00:00 +0000</pubDate>
        <link>http://phpfastcgi.github.io//general/2015/07/14/making-symfony-faster.html</link>
        <guid isPermaLink="true">http://phpfastcgi.github.io//general/2015/07/14/making-symfony-faster.html</guid>
        
        
        <category>general</category>
        
      </item>
    
      <item>
        <title>Welcome to PHPFastCGI</title>
        <description>&lt;p&gt;Welcome to the website for the PHPFastCGI project. I’m going to use this space to post updates on the project, document features and show examples - all with the aim of making the software easier to use.&lt;/p&gt;

&lt;p&gt;Please watch this space if you find this project interesting.&lt;/p&gt;
</description>
        <pubDate>Mon, 13 Jul 2015 19:24:12 +0000</pubDate>
        <link>http://phpfastcgi.github.io//general/2015/07/13/welcome.html</link>
        <guid isPermaLink="true">http://phpfastcgi.github.io//general/2015/07/13/welcome.html</guid>
        
        
        <category>general</category>
        
      </item>
    
  </channel>
</rss>
