<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  <channel>
    <title>Blog entries tagged fastcgi :: mwop.net</title>
    <description>Blog entries tagged fastcgi :: mwop.net</description>
    <pubDate>Sun, 15 Aug 2010 09:40:29 -0500</pubDate>
    <generator>Laminas_Feed_Writer 2 (https://getlaminas.org)</generator>
    <link>https://mwop.net/blog/tag/fastcgi</link>
    <atom:link rel="self" type="application/rss+xml" href="https://mwop.net/blog/tag/fastcgi/rss.xml"/>
    <item>
      <title>Running mod_php and FastCGI side-by-side</title>
      <pubDate>Sun, 15 Aug 2010 09:40:29 -0500</pubDate>
      <link>https://mwop.net/blog/243-Running-mod_php-and-FastCGI-side-by-side.html</link>
      <guid>https://mwop.net/blog/243-Running-mod_php-and-FastCGI-side-by-side.html</guid>
      <author>contact@mwop.net (Matthew Weier O'Phinney)</author>
      <dc:creator>Matthew Weier O'Phinney</dc:creator>
      <content:encoded><![CDATA[<p>Because we're in full throes of <a href="http://framework.zend.com">Zend Framework</a> 2.0
development, I find myself with a variety of PHP binaries floating around my
system from both the PHP 5.2 and 5.3 release series. We're at a point now where
I'm wanting to test migrating applications from ZF 1.X to 2.0 to se see what
works and what doesn't. But that means I need more than one PHP binary enabled
on my server…</p>
<p>I use <a href="http://www.zend.com/products/server/">Zend Server</a> on my development box;
it's easy to install, and uses my native Ubuntu update manager to get updates.
On Ubuntu, it installs the Debian Apache2 packages, so I get the added bonus of
familiarity with the configuration structure.</p>
<p>I installed Zend Server some time ago, so I'm still on a PHP 5.2 <code>mod_php</code>
binary. I have several PHP 5.3 binaries compiled and installed locally for
running unit tests and sample scripts already — so the question was how to keep
my 5.2 <code>mod_php</code> running while simultaneously allowing the ability to run
selected vhosts in 5.3?</p>
<p>The answer can be summed up in one acronym: FastCGI.</p>


<p>With a little help from <a href="http://ralphschindler.com">Ralph Schindler</a>, I got things setup.</p>
<h2>Enabling FastCGI on Ubuntu's Apache</h2>
<p>Interestingly, FastCGI is not enabled by default, nor is another module you'll
need, <code>mod_actions</code>. You can enable these very easily though:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ <span class="hljs-built_in">cd</span> /etc/apache2/mods-enabled
$ sudo ln -s ../mods-available/fastcgi.load .
$ sudo ln -s ../mods-available/fastcgi.conf .
$ sudo ln -s ../mods-available/actions.load .
$ sudo ln -s ../mods-available/actions.conf .
</code></pre>
<h2>Create a FastCGI-enabled vhost</h2>
<p>Next, you need to add a new vhost that will utilize FastCGI. I copied an
existing vhost I had in my <code>/etc/apache2/sites-enabled</code> tree, modified it to
give it a unique <code>ServerName</code> and <code>DocumentRoot</code>, and added the following lines:</p>
<pre><code class="language-apacheconf hljs apache" data-lang="apacheconf"><span class="hljs-attribute">ScriptAlias</span> /cgi-bin/ /path/to/zfproject/public/cgi-bin/
<span class="hljs-attribute">AddHandler</span> php-fcgi .php
<span class="hljs-attribute">Action</span> php-fcgi /cgi-bin/php-5.3.1
</code></pre>
<p>The name of the PHP script doesn't matter much; I used &quot;php-5.3.1&quot; so that I
could visually recognize what version of PHP I was using with that vhost.</p>
<h2>Create a &quot;cgi-bin&quot; directory and CGI script</h2>
<p>Finally, I needed to actually create the &quot;cgi-bin&quot; directory and CGI script to
execute. This was relatively simple; I navigated to my project's <code>DocumentRoot</code>,
and created a new directory <code>cgi-bin</code> (<code>mkdir cgi-bin</code>).</p>
<p>I then entered that directory and created a new script, based on the name I
provided in my vhost. That script, <code>cgi-bin/php-5.3.1</code> then simply <code>exec</code>'s the
<code>php-cgi</code> binary from my PHP install.</p>
<h3>Note about CGI binaries</h3>
<p>In PHP 5.3 and up, CGI binaries are built by default — and they're already
FastCGI enabled. In PHP 5.2, CGI versions are still built by default, but they
are not FastCGI-enabled unless you explicitly pass the <code>--enable-fastcgi</code>
configure flag. To determine if you did that when compiling, execute the
following:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ php-cgi -i | grep fcgi
</code></pre>
<p>If you get no output, you need to recompile.</p>
<p>My script looks like this:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-built_in">exec</span> /path/to/php/install/bin/php-cgi <span class="hljs-string">"<span class="hljs-variable">$@</span>"</span>
</code></pre>
<p>Because this is a CGI binary, you can pass additional CLI arguments and
environment variables; try experimenting with setting your <code>include_path</code>,
application environment, etc.</p>
<p>Once you're done creating the script, make sure it's executable:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ chmod a+x php-5.3.1
</code></pre>
<h2>Fire it up!</h2>
<p>Once I'd done the above, I restarted my Apache instance
(<code>sudo /etc/init.d/apache2 restart</code>). After ensuring there were no startup
errors, I navigated to my new vhost, and <em>voila!</em> it was running.</p>
<p>For those of you doing your first forays into PHP 5.3, this is an excellent way
to test code without needing a separate server running. It's also a great way to
test whether your application is 5.3-ready — create a 5.3-enabled vhost pointing
to your existing application and see if it runs.</p>


<div class="h-entry">
    <img class="u-photo photo" width="50" src="https://avatars0.githubusercontent.com/u/25943?v=3&u=79dd2ea1d4d8855944715d09ee4c86215027fa80&s=140" alt="matthew">
    <a class="u-url u-uid p-name" href="https://mwop.net/blog/243-Running-mod_php-and-FastCGI-side-by-side.html">Running mod_php and FastCGI side-by-side</a> was originally
    published <time class="dt-published" datetime="2010-08-09T10:45:00-05:00">9 August 2010</time>
    on <a href="https://mwop.net">https://mwop.net</a> by
    <a rel="author" class="p-author" href="https://mwop.net">Matthew Weier O&#039;Phinney</a>.
</div>
]]></content:encoded>
      <slash:comments>0</slash:comments>
    </item>
  </channel>
</rss>
