<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title type="text">Blog entries tagged fastcgi :: mwop.net</title>
  <updated>2010-08-15T09:40:29-05:00</updated>
  <generator uri="https://getlaminas.org" version="2">Laminas_Feed_Writer</generator>
  <link rel="alternate" type="text/html" href="https://mwop.net/blog/tag/fastcgi"/>
  <link rel="self" type="application/atom+xml" href="https://mwop.net/blog/tag/fastcgi/atom.xml"/>
  <id>https://mwop.net/blog/tag/fastcgi</id>
  <entry xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <title type="html"><![CDATA[Running mod_php and FastCGI side-by-side]]></title>
    <published>2010-08-09T10:45:00-05:00</published>
    <updated>2010-08-15T09:40:29-05:00</updated>
    <link rel="alternate" type="text/html" href="https://mwop.net/blog/243-Running-mod_php-and-FastCGI-side-by-side.html"/>
    <id>https://mwop.net/blog/243-Running-mod_php-and-FastCGI-side-by-side.html</id>
    <author>
      <name>Matthew Weier O'Phinney</name>
      <email>contact@mwop.net</email>
      <uri>https://mwop.net</uri>
    </author>
    <content xmlns:xhtml="http://www.w3.org/1999/xhtml" type="xhtml">
      <xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml"><xhtml:p>Because we're in full throes of <xhtml:a href="http://framework.zend.com">Zend Framework</xhtml: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…</xhtml:p>
<xhtml:p>I use <xhtml:a href="http://www.zend.com/products/server/">Zend
Server</xhtml: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.</xhtml:p>
<xhtml:p>I installed Zend Server some time ago, so I'm still on a PHP 5.2
<xhtml:code>mod_php</xhtml: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
<xhtml:code>mod_php</xhtml:code> running while simultaneously allowing the
ability to run selected vhosts in 5.3?</xhtml:p>
<xhtml:p>The answer can be summed up in one acronym: FastCGI.</xhtml:p>
<xhtml:p>With a little help from <xhtml:a href="http://ralphschindler.com">Ralph Schindler</xhtml:a>, I got things
setup.</xhtml:p>
<xhtml:h2>Enabling FastCGI on Ubuntu's Apache</xhtml:h2>
<xhtml:p>Interestingly, FastCGI is not enabled by default, nor is another
module you'll need, <xhtml:code>mod_actions</xhtml:code>. You can enable these
very easily though:</xhtml:p>
<xhtml:pre><xhtml:code class="language-bash hljs bash" data-lang="bash">$ <xhtml:span class="hljs-built_in">cd</xhtml: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 .
</xhtml:code></xhtml:pre>
<xhtml:h2>Create a FastCGI-enabled vhost</xhtml:h2>
<xhtml:p>Next, you need to add a new vhost that will utilize FastCGI. I
copied an existing vhost I had in my
<xhtml:code>/etc/apache2/sites-enabled</xhtml:code> tree, modified it to give
it a unique <xhtml:code>ServerName</xhtml:code> and <xhtml:code>DocumentRoot</xhtml:code>,
and added the following lines:</xhtml:p>
<xhtml:pre><xhtml:code class="language-apacheconf hljs apache" data-lang="apacheconf"><xhtml:span class="hljs-attribute">ScriptAlias</xhtml:span> /cgi-bin/ /path/to/zfproject/public/cgi-bin/
<xhtml:span class="hljs-attribute">AddHandler</xhtml:span> php-fcgi .php
<xhtml:span class="hljs-attribute">Action</xhtml:span> php-fcgi /cgi-bin/php-5.3.1
</xhtml:code></xhtml:pre>
<xhtml:p>The name of the PHP script doesn't matter much; I used
"php-5.3.1" so that I could visually recognize what version of PHP
I was using with that vhost.</xhtml:p>
<xhtml:h2>Create a "cgi-bin" directory and CGI script</xhtml:h2>
<xhtml:p>Finally, I needed to actually create the "cgi-bin" directory and
CGI script to execute. This was relatively simple; I navigated to
my project's <xhtml:code>DocumentRoot</xhtml:code>, and created a new directory
<xhtml:code>cgi-bin</xhtml:code> (<xhtml:code>mkdir cgi-bin</xhtml:code>).</xhtml:p>
<xhtml:p>I then entered that directory and created a new script, based on
the name I provided in my vhost. That script,
<xhtml:code>cgi-bin/php-5.3.1</xhtml:code> then simply <xhtml:code>exec</xhtml:code>'s the
<xhtml:code>php-cgi</xhtml:code> binary from my PHP install.</xhtml:p>
<xhtml:h3>Note about CGI binaries</xhtml:h3>
<xhtml: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 <xhtml:code>--enable-fastcgi</xhtml:code> configure flag.
To determine if you did that when compiling, execute the
following:</xhtml:p>
<xhtml:pre><xhtml:code class="language-bash hljs bash" data-lang="bash">$ php-cgi -i | grep fcgi
</xhtml:code></xhtml:pre>
<xhtml:p>If you get no output, you need to recompile.</xhtml:p>
<xhtml:p>My script looks like this:</xhtml:p>
<xhtml:pre><xhtml:code class="language-bash hljs bash" data-lang="bash"><xhtml:span class="hljs-meta">#!/bin/bash</xhtml:span>
<xhtml:span class="hljs-built_in">exec</xhtml:span> /path/to/php/install/bin/php-cgi <xhtml:span class="hljs-string">"<xhtml:span class="hljs-variable">$@</xhtml:span>"</xhtml:span>
</xhtml:code></xhtml:pre>
<xhtml:p>Because this is a CGI binary, you can pass additional CLI
arguments and environment variables; try experimenting with setting
your <xhtml:code>include_path</xhtml:code>, application environment, etc.</xhtml:p>
<xhtml:p>Once you're done creating the script, make sure it's
executable:</xhtml:p>
<xhtml:pre><xhtml:code class="language-bash hljs bash" data-lang="bash">$ chmod a+x php-5.3.1
</xhtml:code></xhtml:pre>
<xhtml:h2>Fire it up!</xhtml:h2>
<xhtml:p>Once I'd done the above, I restarted my Apache instance
(<xhtml:code>sudo /etc/init.d/apache2 restart</xhtml:code>). After ensuring
there were no startup errors, I navigated to my new vhost, and
<xhtml:em>voila!</xhtml:em> it was running.</xhtml:p>
<xhtml: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.</xhtml:p>
<xhtml:div class="h-entry"><xhtml:img class="u-photo photo" width="50" src="https://avatars0.githubusercontent.com/u/25943?v=3&amp;u=79dd2ea1d4d8855944715d09ee4c86215027fa80&amp;s=140" alt="matthew"/> <xhtml: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</xhtml:a> was originally
published <xhtml:time class="dt-published" datetime="2010-08-09T10:45:00-05:00">9 August 2010</xhtml:time> on <xhtml:a href="https://mwop.net">https://mwop.net</xhtml:a> by <xhtml:a rel="author" class="p-author" href="https://mwop.net">Matthew Weier
O'Phinney</xhtml:a>.</xhtml:div>
</xhtml:div>
    </content>
  </entry>
</feed>
