<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>funcptr - php</title>
    <subtitle>An engineer&#x27;s technical notebook</subtitle>
    <link rel="self" type="application/atom+xml" href="https://funcptr.net/language/php/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://funcptr.net/"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2013-08-25T13:05:00+00:00</updated>
    <id>https://funcptr.net/language/php/atom.xml</id>
    <entry xml:lang="en">
        <title>User sessions, what data should be stored where?</title>
        <published>2013-08-25T13:05:00+00:00</published>
        <updated>2013-08-25T13:05:00+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2013/user-sessions-and-the-data-to-be-stored/"/>
        <id>https://funcptr.net/2013/user-sessions-and-the-data-to-be-stored/</id>
        
        <content type="html" xml:base="https://funcptr.net/2013/user-sessions-and-the-data-to-be-stored/">&lt;p&gt;&lt;em&gt;This article has been updated. For the old version please check
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20130826031727&#x2F;http:&#x2F;&#x2F;funcptr.net&#x2F;2013&#x2F;08&#x2F;25&#x2F;user-sessions,-what-data-should-be-stored-where-&#x2F;&quot;&gt;Archive.org&lt;&#x2F;a&gt;&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;A couple of days ago on &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;reddit.com&#x2F;&quot;&gt;reddit.com&lt;&#x2F;a&gt;&#x27;s &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;reddit.com&#x2F;r&#x2F;netsec&#x2F;&quot;&gt;&#x2F;r&#x2F;netsec&lt;&#x2F;a&gt; a poster by the name
of &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;danweber.blogspot.com&#x2F;&quot;&gt;Dan Weber&lt;&#x2F;a&gt; posted what he believed to be an attack on PHP sessions:
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;danweber.blogspot.com&#x2F;2013&#x2F;08&#x2F;hacking-php-sessions-by-running-out-of.html&quot;&gt;Hacking PHP sessions by running out of memory&lt;&#x2F;a&gt; &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;netsec&#x2F;comments&#x2F;1k7khy&#x2F;i_know_its_not_that_hard_but_i_think_i_found_a&#x2F;&quot;&gt;(reddit link)&lt;&#x2F;a&gt;. The way
the &quot;attack&quot; works is as follows:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Create a new session&lt;&#x2F;li&gt;
&lt;li&gt;Assign some new data to the session, in this case a username which is used
to signify that the user is logged in.&lt;&#x2F;li&gt;
&lt;li&gt;Check to verify that the user should be logged in&lt;&#x2F;li&gt;
&lt;li&gt;If the user should not be logged in, destroy the session.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;The &quot;attack&quot; would be to run the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;php.net&#x2F;&quot;&gt;PHP&lt;&#x2F;a&gt; script out of memory on number 3, since
once something is set on the session it is immediately stored, so even if the
user is not supposed to be logged in, they are now logged in since their
session says they are.&lt;&#x2F;p&gt;
&lt;p&gt;I wouldn&#x27;t necessarily call this a PHP hack, this is really just bad practice
in terms of programming, the logic should be reversed.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Create a new session&lt;&#x2F;li&gt;
&lt;li&gt;Verify the user should be logged in&lt;&#x2F;li&gt;
&lt;li&gt;If so, set the session data&lt;&#x2F;li&gt;
&lt;li&gt;If the user should not be logged in, don&#x27;t set the session data&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;That would solve the problem at hand, and now there is no way for the user to
trick the PHP script into believing she is logged in when that is not the case.&lt;&#x2F;p&gt;
&lt;p&gt;However as the discussion went on on Reddit, it became even more clear that
there are no good resources on what you should store in the user session, and
what you shouldn&#x27;t store in the user session. Some of these things may seem
like common knowledge, but sadly this is something every single new person to
programming has to learn on their own.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;some-assumptions&quot;&gt;Some assumptions&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s get this out of the way, this is in no way limited to PHP, but it is the
one I will be using as an example. This can all apply to Ruby (&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;rubyonrails.org&quot;&gt;Ruby on
Rails&lt;&#x2F;a&gt;), Python (&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.pylonsproject.org&quot;&gt;Pyramid&lt;&#x2F;a&gt;) or many other frameworks.&lt;&#x2F;p&gt;
&lt;p&gt;The basic problem is that generally writing to the session is not an atomic
transaction based on the page accessed, so the assumption made in this article
is that when you write to the session it is instantly committed, and there is
no way to roll it back upon failure. If there was, our first example listed
wouldn&#x27;t be able to occur since upon running out of memory at step 3, the
session would have been rolled back and cleaned up.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-should-you-store-in-the-users-session&quot;&gt;What should you store in the users session?&lt;&#x2F;h2&gt;
&lt;p&gt;You should only really store anything in the session that if it were made
public it would do very little harm.&lt;&#x2F;p&gt;
&lt;p&gt;Really the list of items to be stored in a session are as follows:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;The users unique id (The ID that allows you to retrieve the users
information from storage)&lt;&#x2F;li&gt;
&lt;li&gt;Temporary state (i.e. Flash messages)&lt;&#x2F;li&gt;
&lt;li&gt;CSRF token&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;More importantly, don&#x27;t store permission bits, or group memberships, or
anything that is used to allow&#x2F;deny access to particular resources. You want to
store just enough information that upon a user accessing your site you are able
to retrieve the users information from storage, and based upon that information
from storage you then make decisions such as permissions&#x2F;group memberships.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;why-is-this-important&quot;&gt;Why is this important?&lt;&#x2F;h3&gt;
&lt;p&gt;One of the things that Dan Weber brought up in the Reddit post was storing the
users permission level and group membership in the session. If your code is
then relying on the session to always contain the right permission level, then
there is no way to expire someones access to the data.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;A user logs in&lt;&#x2F;li&gt;
&lt;li&gt;A user gets various permissions, and they are set in the session&lt;&#x2F;li&gt;
&lt;li&gt;The user has been fired from his job, and an administrator removes the
users permissions&lt;&#x2F;li&gt;
&lt;li&gt;Since the user is still logged in, the users permission bits are still
set, and he continues to have access to parts of the site&#x2F;information he
shouldn&#x27;t have access to.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;If instead on every page visit we simply pull out the users unique id and
verify the permissions upon access, as soon as the permissions are revoked by
the administrator the user no longer has access to the various resources.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;temporary-state&quot;&gt;Temporary state&lt;&#x2F;h3&gt;
&lt;p&gt;There has to be an easy way to remember something from page visit to page visit
that isn&#x27;t considered detrimental if the information gets lost. One of those
things is flash messages. Flash messages are generally used to provide the user
indication that something has changed, they are shown once and then never
again.&lt;&#x2F;p&gt;
&lt;p&gt;Storing these as session data makes sense. If the flash message gets set,
great, if it doesn&#x27;t get set, it doesn&#x27;t matter. Flash messages are simply a
notification tool, if the user misses them it isn&#x27;t important.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-you-shouldn-t-store-in-the-session&quot;&gt;What you shouldn&#x27;t store in the session&lt;&#x2F;h2&gt;
&lt;p&gt;Definitely don&#x27;t store any kind of permission bits, groups a user is a part of
or anything that would allow the user access that they normally would not be
able to access.&lt;&#x2F;p&gt;
&lt;p&gt;On each page access check what permissions the user has. While it may mean a
little more heavy lifting server side it provides extra security, and the means
to enforce changes in permissions instantly.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;good-secure-programming-practices&quot;&gt;Good secure programming practices&lt;&#x2F;h2&gt;
&lt;p&gt;Keep secure programming practices in mind at all times, always consider how the
information you are storing&#x2F;processing is accessed&#x2F;viewed&#x2F;administered. More
importantly think about the access controls that are in place, and how one
could expire access to a particular resource without requiring a co-operative
client.&lt;&#x2F;p&gt;
&lt;p&gt;The ordering of how variables are set, and when they are set are very
important.  &lt;code&gt;$_SESSION[&#x27;isadmin&#x27;] = True&lt;&#x2F;code&gt; at the top of a PHP script, and then
removing it by checking to see if the user is actually an administrator later
on in the script is a bad idea.&lt;&#x2F;p&gt;
&lt;p&gt;Always order your code so that if a failure does occur there is no chance that
a critical section of your code is executed by accident, or that information is
stored in a half-verified state. This is especially important for access
control.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Apache mod_fastcgi and PHP with PHP-FPM</title>
        <published>2010-11-14T23:37:27+00:00</published>
        <updated>2010-11-14T23:37:27+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2010/apache-mod-fastcgi-and-php-with-php-fpm/"/>
        <id>https://funcptr.net/2010/apache-mod-fastcgi-and-php-with-php-fpm/</id>
        
        <content type="html" xml:base="https://funcptr.net/2010/apache-mod-fastcgi-and-php-with-php-fpm/">&lt;p&gt;Recently I did a server migration from an older server to a newer server and in
an attempt to help stability I wanted to see if there was a better way to do
PHP FastCGI. In my research I came across running PHP using the FastCGI server
that spins up a PHP on a TCP&#x2F;IP port and allows the web server to connect to
it. However this doesn&#x27;t help with spawning or keeping track of instances or
error recovery.&lt;&#x2F;p&gt;
&lt;p&gt;This is where &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;php-fpm.org&#x2F;&quot;&gt;PHP-FPM&lt;&#x2F;a&gt; comes in handy. It does all of the hard work for us,
it spawns the processes and has a bunch of really awesome features that help
run PHP as various different users as required with different PHP ini files and
memory limits. PHP-FPM&#x27;s defaults, at least from a ports install, are extremely
sane and I don&#x27;t really suggest changing them. After setting up PHP FPM I had
to set up Apache.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;mod_fcgid&lt;&#x2F;code&gt; doesn&#x27;t allow for remote connections, and as such I was unable to use
it for what I needed it for. &lt;code&gt;mod_fastcgi&lt;&#x2F;code&gt; provides the
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20101111193738&#x2F;http:&#x2F;&#x2F;www.fastcgi.com&#x2F;mod_fastcgi&#x2F;docs&#x2F;mod_fastcgi.html#FastCgiExternalServer&quot;&gt;FastCGIExternalServer&lt;&#x2F;a&gt; configuration key, which is exactly what I needed.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&quot;The FastCgiExternalServer directive defines filename as an external
FastCGI application. If filename does not begin with a slash (&lt;code&gt;&#x2F;&lt;&#x2F;code&gt;) then it is
assumed to be relative to the &lt;code&gt;ServerRoot&lt;&#x2F;code&gt;. The filename does not have to exist
in the local filesystem. URIs that Apache resolves to this filename will be
handled by this external FastCGI application.&quot;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;What this documentation does not state is that the path up to the last part of
it has to exist in the local file system. So in my first couple of attempts I
pointed it at &lt;code&gt;&#x2F;usr&#x2F;local&#x2F;www&#x2F;fastcgi&#x2F;php5.fcgi&lt;&#x2F;code&gt; without having an actual fastcgi
directory located in &lt;code&gt;&#x2F;usr&#x2F;local&#x2F;www&#x2F;&lt;&#x2F;code&gt;. There are a lot of examples that require
creating a new &lt;code&gt;FastCGIExternalServer&lt;&#x2F;code&gt; for each and every &lt;code&gt;VirtualHost&lt;&#x2F;code&gt; this is
unacceptable to me, the reason they require it is because they set the
&lt;code&gt;FastCGIExternalServer&lt;&#x2F;code&gt; path to the folder where they are going to be serving
files from.&lt;&#x2F;p&gt;
&lt;p&gt;In the end I found that after creating the &lt;code&gt;&#x2F;usr&#x2F;local&#x2F;www&#x2F;fastcgi&lt;&#x2F;code&gt; directory
(and reading the &lt;code&gt;mod_fastcgi&lt;&#x2F;code&gt; source code) that all it does is make Apache
believe a file exists in a certain directory, much like &lt;code&gt;Alias&lt;&#x2F;code&gt;, except &lt;code&gt;Alias&lt;&#x2F;code&gt;
allows full paths to be aliased, not so here.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;AddType&lt;&#x2F;code&gt; and &lt;code&gt;Action&lt;&#x2F;code&gt; add custom types and what the action should be when
such a type is encountered. In this case the action is to redirect the request
to &lt;code&gt;&#x2F;php5.fcgi&lt;&#x2F;code&gt; which will handle the rest of the request. This does not require
another FastCGI section per &lt;code&gt;VirtualHost&lt;&#x2F;code&gt; as each PHP request will just get
shuttled to the php handler.&lt;&#x2F;p&gt;
&lt;p&gt;Do take note that I have specifically disallowed Apache to serve anything from
the &lt;code&gt;&#x2F;usr&#x2F;local&#x2F;www&#x2F;fastcgi&#x2F;&lt;&#x2F;code&gt; folder, except for a single file &lt;code&gt;php5.fcgi&lt;&#x2F;code&gt;
which is our &lt;code&gt;FastCGIExternalServer&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;IfModule mod_fastcgi.c&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Alias &#x2F;php5.fcgi &#x2F;usr&#x2F;local&#x2F;www&#x2F;fastcgi&#x2F;php5.fcgi&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    FastCGIExternalServer &#x2F;usr&#x2F;local&#x2F;www&#x2F;fastcgi&#x2F;php5.fcgi -flush -host 127.0.0.1:9000&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    AddType application&#x2F;x-httpd-fastphp5 .php&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Action application&#x2F;x-httpd-fastphp5 &#x2F;php5.fcgi&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;Directory &amp;quot;&#x2F;usr&#x2F;local&#x2F;www&#x2F;fastcgi&#x2F;&amp;quot;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        Order deny,allow&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        Deny from all&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &amp;lt;Files &amp;quot;php5.fcgi&amp;quot;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            Order allow,deny&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            Allow from all&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &amp;lt;&#x2F;Files&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;&#x2F;Directory&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;&#x2F;IfModule&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that even-though in my &lt;a href=&quot;&#x2F;2009&#x2F;moving-from-mod-fastcgi-to-mod-fcgid&#x2F;&quot;&gt;last post&lt;&#x2F;a&gt; concerning &lt;code&gt;mod_fastcgi&lt;&#x2F;code&gt; I as moving
away from it, I am now doing the opposite, instead of &lt;a href=&quot;&#x2F;2009&#x2F;moving-from-mod-fastcgi-to-mod-fcgid&#x2F;&quot;&gt;moving from
&lt;code&gt;mod_fastcgi&lt;&#x2F;code&gt; to &lt;code&gt;mod_fcgid&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; I&#x27;m back to &lt;code&gt;mod_fastcgi&lt;&#x2F;code&gt;, only because
&lt;code&gt;mod_fcgid&lt;&#x2F;code&gt; doesn&#x27;t offer the same functionality.&lt;&#x2F;p&gt;
&lt;p&gt;So far this has provided far more stability, along with PHP-FPM doing all of
the process management I can now use a single PHP instance that is running on a
single port for the various web servers I am testing. At the moment I have both
Lighttpd and Apache using the same PHP-FPM instance. It is faster, less
memory is wasted and PHP-FPM is much better at process management than
&lt;code&gt;mod_fastcgi&lt;&#x2F;code&gt; or &lt;code&gt;mod_fcgid&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>FastCGI quick and dirty</title>
        <published>2006-09-13T00:19:31+00:00</published>
        <updated>2006-09-13T00:19:31+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2006/fastcgi-quick-and-dirty/"/>
        <id>https://funcptr.net/2006/fastcgi-quick-and-dirty/</id>
        
        <content type="html" xml:base="https://funcptr.net/2006/fastcgi-quick-and-dirty/">&lt;p&gt;In this quick and dirty guide for FreeBSD I will assume you have already
compiled and installed PHP5 with fcgi mode enabled, which can be checked by
running:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;&#x2F;usr&#x2F;local&#x2F;bin&#x2F;php-cgi&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; --version&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I will also assume you have installed &lt;code&gt;mod_fastcgi&lt;&#x2F;code&gt; from &lt;code&gt;www&#x2F;mod_fastcgi&lt;&#x2F;code&gt;
in the ports.&lt;&#x2F;p&gt;
&lt;p&gt;Create the fast cgi ipc temp dir, make sure you place this somewhere it will
keep existing:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;font-style: italic;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; &#x2F;var&#x2F;tmp&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;mkdir&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; -p&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; &#x2F;var&#x2F;tmp&#x2F;fcgi-ipc&#x2F;dynamic&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;chmod&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; -R&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0777&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; &#x2F;var&#x2F;tmp&#x2F;fcgi-ipc&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s create the directory where we are going to place our PHP wrapper, this
config is for server wide using the same FastCGI PHP config. This is on
purpose, since it will allow server wide PHP usage. If you want you could then
split it up into virtualhosts if one required a standalone php running for
that.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;mkdir&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; &#x2F;usr&#x2F;local&#x2F;www&#x2F;fastcgi-bin&#x2F;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;chmod&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 755&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; &#x2F;usr&#x2F;local&#x2F;www&#x2F;fastcgi-bin&#x2F;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next up we create the very simple wrapper:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;cat&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; EOF&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; &amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; &#x2F;usr&#x2F;local&#x2F;www&#x2F;fastcgi-bin&#x2F;php5.fcgi&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;#!&#x2F;bin&#x2F;sh&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;# To use your own php.ini, comment the next line and uncomment the following one&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;#PHPRC=&amp;quot;&#x2F;usr&#x2F;local&#x2F;etc&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;export PHPRC&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;PHP_FCGI_CHILDREN=8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;export PHP_FCGI_CHILDREN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;exec &#x2F;usr&#x2F;local&#x2F;bin&#x2F;php-cgi&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;EOF&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Set &lt;code&gt;PHP_FCGI_CHILDREN&lt;&#x2F;code&gt; to whatever value you want, that is how many processes
it will use. I suggest the standard which is 8, if you make it more, you use
more ram, you make it less Apache will have to wait if all of the PHP
processes are in use.&lt;&#x2F;p&gt;
&lt;p&gt;Next up, make it executable:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;chmod&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; +x &#x2F;usr&#x2F;local&#x2F;www&#x2F;fastcgi-bin&#x2F;php5.fcgi&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Last but not least, edit &lt;code&gt;httpd.conf&lt;&#x2F;code&gt; and make sure to add a comment in front
of the &lt;code&gt;mod_php5&lt;&#x2F;code&gt; line, and uncomment the &lt;code&gt;mod_fastcgi&lt;&#x2F;code&gt; line. Then add this
to the end of your Apache config:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;IfModule mod_fastcgi.c&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    FastCgiIpcDir &#x2F;var&#x2F;tmp&#x2F;fcgi-ipc&#x2F;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    FastCgiConfig  -pass-header HTTP_AUTHORIZATION&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    AddHandler  fastcgi-script              .fcgi .fcg .fpl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Action      application&#x2F;x-httpd-php5    &#x2F;fastcgi-bin&#x2F;php5.fcgi&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    AddType     application&#x2F;x-httpd-php5    .php .php5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;&#x2F;IfModule&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ScriptAlias &#x2F;fastcgi-bin&#x2F; &amp;quot;&#x2F;usr&#x2F;local&#x2F;www&#x2F;fastcgi-bin&#x2F;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;Location &#x2F;fastcgi-bin&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Options ExecCGI&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    SetHandler fastcgi-script&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Order allow,deny&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Allow from all&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;&#x2F;Location&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s it. Restart apache and watch as the first time you access a PHP script
FastCGI will spawn several of them, and they start serving requests. Load
usage may seem higher, that is because unlike Apache the standalone running
PHP&#x27;s handle the jobs as they come in with whatever one is first, Apache will
try to put as much work in one of its preforked modules. As the PHP processes
come and go through FastCGI the memory usage will go up and down. Mine has
an average of about 300 MB, which is very good compared to the 800 MB Apache
with &lt;code&gt;mod_php&lt;&#x2F;code&gt; would rack up.&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
