<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Brandon W. King&#039;s Blog &#187; mod_rewrite</title>
	<atom:link href="http://www.brandonking.net/blog/tag/mod_rewrite/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brandonking.net/blog</link>
	<description>Blogging about Python, Bioinformatics, Programming, System Administration, Cognitive Science, Philosophy and Life</description>
	<lastBuildDate>Wed, 09 Dec 2009 01:24:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Django App + mod_fcgid + Apache 2 Setup on Mac OS X &amp; Debian/Ubuntu Linux</title>
		<link>http://www.brandonking.net/blog/2008/01/31/django-app-mod_fcgid-apache-2-setup-on-mac-os-x-debianubuntu-linux/</link>
		<comments>http://www.brandonking.net/blog/2008/01/31/django-app-mod_fcgid-apache-2-setup-on-mac-os-x-debianubuntu-linux/#comments</comments>
		<pubDate>Fri, 01 Feb 2008 00:54:00 +0000</pubDate>
		<dc:creator>Brandon W. King</dc:creator>
				<category><![CDATA[Computers/IT]]></category>
		<category><![CDATA[Error Fixes]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Systems Administration]]></category>
		<category><![CDATA[apache2]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[fcgi]]></category>
		<category><![CDATA[flup]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mac os x]]></category>
		<category><![CDATA[mod_fcgid]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[mod_wsgi]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.brandonking.net/?p=34</guid>
		<description><![CDATA[

Update 2008Feb11:
From rather obscure comment from &#8216;apt-cache show python-flup&#8217; on a Debian system, I discovered that flup has been superseded by http://www.modwsgi.org/. It is probably worth checking out mod_wsgi instead of mod_fcgid for Python web applications. Based on the documentation for mod_wsgi integration with Django, I am planning on switching. I will post my results [...]]]></description>
			<content:encoded><![CDATA[<div class="wikipage">
<div id="searchable">
<h2 id="RequiredSoftware">Update 2008Feb11:</h2>
<p>From rather obscure comment from &#8216;apt-cache show python-flup&#8217; on a Debian system, I discovered that flup has been superseded by <a href="http://www.modwsgi.org/">http://www.modwsgi.org/</a>. It is probably worth checking out mod_wsgi instead of mod_fcgid for Python web applications. Based on the documentation for <a href="http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango">mod_wsgi integration with Django</a>, I am planning on switching. I will post my results in a future post.</p>
<h2 id="RequiredSoftware">Required Software<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#RequiredSoftware"><br />
</a></h2>
<ul>
<li>Apache 2 &#8211; <a class="ext-link" href="http://httpd.apache.org/"><span class="icon">http://httpd.apache.org/</span></a></li>
<li>mod_rewrite &#8211; (comes with Apache2)</li>
<li>mod_fcgid &#8211; <a class="ext-link" href="http://fastcgi.coremail.cn/"><span class="icon">http://fastcgi.coremail.cn/</span></a></li>
<li>flup &#8211; <a class="ext-link" href="http://trac.saddi.com/flup"><span class="icon">http://trac.saddi.com/flup</span></a></li>
</ul>
<h2 id="mod_fcgid">mod_fcgid<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#mod_fcgid"><br />
</a></h2>
<h3 id="DownloadSourceandInstall">Download Source and Install<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#DownloadSourceandInstall"><br />
</a></h3>
<ul>
<li><a class="ext-link" href="http://fastcgi.coremail.cn/download.htm"><span class="icon">Download</span></a></li>
</ul>
<p>For reference, here is the mod_fcgid INSTALL.txt&#8230; see my Mac OS X notes below it for changes required to get it working on Mac OS X 10.5 (Leopard):</p>
<pre class="wiki">NOTE: This module is for Apache2 ONLY

UNIXIt's tested on my RedHat8 and Solaris. But it should work on other *NIX platform.NOTE: This module MUST run on share memory supported system

1. If your Apache2 installation isn't in /usr/local/apache2, please edit Makefile and correct it2. cd $mod_fcgid_dir3. make   //in Mac you need Xcode tools, optional install on Install CD4. make install5. add the following line in httpd.conf

LoadModule fcgid_module modules/mod_fcgid.so</pre>
<h4 id="MACOSXNotes">MAC OS X Notes<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#MACOSXNotes"><br />
</a></h4>
<ul>
<li>In Mac OS X Leopard, the Makefile needs to be changed from /usr/local/apache2 to /usr/share/httpd, then steps 3 and 4 above should work.</li>
<li>Step 4 requires: sudo make install</li>
<li>Step 5, you need to add the following to /etc/apache2/httpd.conf: LoadModule fcgid_module libexec/apache2/mod_fcgid.so</li>
<li>IMPORTANT: apache 2 on Mac OS X Leopard is 64-bit and by default, the make file does not build the 64-bit module. To do this, you need to add the following to the Makefile after the EXTRA_CFLAGS option (too far above that will cause it not to work apparently):</li>
</ul>
<pre class="wiki">CFLAGS = -arch ppc -arch ppc64 -arch i386 -arch x86_64</pre>
<p>The above will make a universal binary for all Mac OS X.</p>
<p>If at some point, you get this message either in the error_log or by running &#8216;apachectl configtest&#8217;</p>
<pre class="wiki">httpd: Syntax error on line 117 of /private/etc/apache2/httpd.conf: Cannot load/usr/libexec/apache2/mod_fcgid.so into server:dlopen(/usr/libexec/apache2/mod_fcgid.so, 10): no suitable image found.Did find: /usr/libexec/apache2/mod_fcgid.so: mach-o, but wrong architecture</pre>
<p>Then your mod_fcgid.so module probably was not built with x86_64 architecture&#8230; To check, type</p>
<pre class="wiki">file /usr/libexec/apache2/mod_fcgid.so</pre>
<p>And it should list the architectures that the mod_fcgid.so was built for.</p>
<h3 id="Enablemod_fcgid">Enable mod_fcgid<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#Enablemod_fcgid"><br />
</a></h3>
<h4 id="Linux">Linux<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#Linux"><br />
</a></h4>
<p>At least with Debian/Ubuntu you can enable fcgid by typing (as root):</p>
<pre class="wiki">a2enmod fcgid</pre>
<h4 id="MacOSXLeopard">Mac OS X Leopard (10.5)<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#MacOSXLeopard"><br />
</a></h4>
<p>Add the following to /etc/apache2/httpd.conf:</p>
<pre class="wiki">AddHandler    fcgid-script .fcgiSocketPath    /tmp/fcgid_sockSharememPath  /tmp/fcgid_shmIPCConnectTimeout 20</pre>
<h2 id="mod_rewrite">mod_rewrite<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#mod_rewrite"><br />
</a></h2>
<p>We need to tell apache for what urls it should pass off the request to the fcgi script. This is accomplished with mod_rewrite which is normally turned on by default w/ apache 2 (at least the installations I have seen).</p>
<p>We&#8217;ll start with the full setup required for gaworkflow.frontend, but test it with a &#8220;Hello World&#8221; style dispatch.fcgi script.</p>
<h3 id="gaworkflow.frontendapache2mod_rewritesetup">gaworkflow.frontend apache2 mod_rewrite setup<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#gaworkflow.frontendapache2mod_rewritesetup"><br />
</a></h3>
<h4 id="Linux1">Linux<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#Linux1"><br />
</a></h4>
<p>Add the following to your apache2 /etc/apache2/sites-available/default or other vhost specific file:</p>
<pre class="wiki"># Enable access to the django admin mediaAlias /media /usr/lib/python2.5/site-packages/django/contrib/admin/media

# Turn on the rewrite engineRewriteEngine on

# Enable http:///admin/ and related urlsRewriteRule ^/admin(.*)$ /usr/lib/cgi-bin/dispatch.fcgi/admin$1 [QSA,L]RewriteRule ^/logout(.*)$ /usr/lib/cgi-bin/dispatch.fcgi/logout$1 [QSA,L]RewriteRule ^/login(.*)$ /usr/lib/cgi-bin/dispatch.fcgi/login$1 [QSA,L]</pre>
<h4 id="MacOSXLeopard1">Mac OS X Leopard<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#MacOSXLeopard1"><br />
</a></h4>
<p>Add the following to your apache2 /etc/httpd.conf:</p>
<pre class="wiki"># Enable access to the django admin media# NOTE: the following did not work for me... I had to copy the media directory to#    /Library/WebServer/Documents/media#Alias /media /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/contrib/admin/media/

# Turn on the rewrite engineRewriteEngine on

# Enable http:///admin urlsRewriteRule ^/eland_config(.*)$ /Library/WebServer/CGI-Executables/dispatch.fcgi/eland_config$1 [QSA,L]RewriteRule ^/admin(.*)$ /Library/WebServer/CGI-Executables/dispatch.fcgi/admin$1 [QSA,L]RewriteRule ^/logout(.*)$ /Library/WebServer/CGI-Executables/dispatch.fcgi/logout$1 [QSA,L]RewriteRule ^/login(.*)$ /Library/WebServer/CGI-Executables/dispatch.fcgi/login$1 [QSA,L]</pre>
<h3 id="RestartApache2">Restart Apache 2<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#RestartApache2"><br />
</a></h3>
<h4 id="LinuxDebianUbuntu">Linux (Debian/Ubuntu)<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#LinuxDebianUbuntu"><br />
</a></h4>
<pre class="wiki">sudo /etc/init.d/apache2 restart</pre>
<h4 id="MacOSXLeopard2">Mac OS X Leopard<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#MacOSXLeopard2"><br />
</a></h4>
<pre class="wiki">sudo apachectl restart</pre>
<h2 id="dispatch.fcgi">dispatch.fcgi<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#dispatch.fcgi"><br />
</a></h2>
<h3 id="HelloWorldTest">Hello World Test<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#HelloWorldTest"><br />
</a></h3>
<p>Now to test that we have the mod_fcgid setup properly, create a file called dispatch.fcgi:</p>
<pre class="wiki">#!/usr/bin/python

def myapp(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) return ['Hello World!\n']

if __name__ == '__main__': from flup.server.fcgi import WSGIServer WSGIServer(myapp).run()</pre>
<p>And move it to your cgi-bin directory:</p>
<h4 id="Linuxcgi-bin">Linux cgi-bin<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#Linuxcgi-bin"><br />
</a></h4>
<pre class="wiki">sudo mv dispatch.fcgi /usr/lib/cgi-bin/</pre>
<h4 id="MacOSXLeopardcgi-bin">Mac OS X Leopard cgi-bin<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#MacOSXLeopardcgi-bin"><br />
</a></h4>
<pre class="wiki">sudo mv dispatch.fcgi /Library/WebServer/CGI-Executables/</pre>
<h4 id="LinuxMacOSXFilePermissions">Linux/Mac OS X File Permissions<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#LinuxMacOSXFilePermissions"><br />
</a></h4>
<pre class="wiki">sudo chmod a+x /dispatch.fcgi</pre>
<p>Mac OS X 10.5 users will need to update the Options for  to include +ExecCGI&#8230; by default it is set to &#8220;Options None&#8221;. The updated entry should look like:</p>
<pre class="wiki"> AllowOverride None Options +ExecCGI Order allow,deny Allow from all</pre>
<p><strong>WARNING:</strong> You will get a <strong>message saying &#8220;Forbidden&#8221;</strong> in the web browser if you do not update the directive above.</p>
<h4 id="WebBrowserTest">Web Browser Test<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#WebBrowserTest"><br />
</a></h4>
<p>Point your web browser to <a class="ext-link" href="http://localhost/admin/"><span class="icon">http://localhost/admin/</span></a> and you should see  if it prints &#8220;Hello World!&#8221;. If you see &#8220;Hello World!&#8221; continue to the next section.</p>
<h2 id="InstallingtheDjangodispatch.fcgiscript">Installing the Django App dispatch.fcgi script<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#InstallingtheDjangodispatch.fcgiscript"><br />
</a></h2>
<h3 id="copyofgaworkflowcode">copy of mydjangoapp code<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#copyofgaworkflowcode"><br />
</a></h3>
<p>Make a copy of the mydjangoapp code and database and put it in a location that will be the &#8220;live&#8221; version of the code/database. On Mac OS X, I choose /Library/WebServer/mydjangoapp. Initialize the database like your normally would, but you will need to update the settings.py module so that DATABASE_NAME is an absolute path, otherwise you will get errors in the apache 2 error_log saying that the python code could not connect to the database. Assuming you also choose /Library/WebServer/mydjangoapp, your DATABASE_NAME variable in settings.py should be:</p>
<pre class="wiki">DATABASE_NAME = '/Library/WebServer/mydjangoapp/mydjangoapp.db'</pre>
<p>You will also need to update the settings.py TEMPLATE_DIRS to be an absolute path (relative paths don&#8217;t work for some reason) of &#8220;/Library/WebServer/gaworkflow/templates&#8221;&#8230; so the update version should look like:</p>
<pre class="wiki">TEMPLATE_DIRS = ( "/Library/WebServer/mydjangoapp/templates",)</pre>
<p>Also, you will need to make a link to the admin templates in our /Library/WebServer/mydjangoapp/templates directory by typing:</p>
<p><strong>Mac OS X 10.5:</strong></p>
<pre class="wiki">ln -s /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/contrib/admin/templates/admin /Library/WebServer/mydjangoapp/templates/adminln -s /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/contrib/admin/templates/admin_doc /Library/WebServer/mydjangoapp/templates/admin_docln -s /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/contrib/admin/templates/widget /Library/WebServer/mydjangoapp/templates/widgetln -s /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/contrib/admin/templates/registration /Library/WebServer/mydjangoapp/templates/registration</pre>
<p><span style="font-weight: bold;">Warning: The following directions will tell you how to get past the db read/access errors but may not be the best choice for security&#8230; you consider the possible security issues before following the following instructions.</span></p>
<p>To get the dispatch.fcgi to work properly, I needed to change ownership of /Library/WebServer/mydjangoapp to be owned by the apache2 user, which is _www on Mac OS X 10.5 and www-data on Debian/Ubuntu systems. The the apache user also needs access to the mydjangoapp.db as well. I ran the following commands on Mac OS X 10.5 when placing the files in /Library/WebServer/gaworkflow:</p>
<pre class="wiki">sudo chown _www:_www /Library/WebServer/mydjangoappsudo chown _www:_www /Library/WebServer/mydjangoapp/mydjangoapp.dbsudo chmod o-rwx /Library/WebServer/mydjangoapp/mydjangoapp.dbsudo chmod ug+rwx /Library/WebServer/mydjangoapp/mydjangoapp.db</pre>
<h3 id="newdispatch.fcgi">new dispatch.fcgi<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#newdispatch.fcgi"> ¶</a></h3>
<p>Replace the hello world dispatch.fcgi with the following script:</p>
<pre class="wiki">#!/usr/bin/python                                                            import syssys.path += ['/Library/WebServer/mydjangoapp']from flup.server.fcgi import WSGIServerfrom django.core.handlers.wsgi import WSGIHandlerimport osos.environ['DJANGO_SETTINGS_MODULE'] = 'mydjangoapp.settings'WSGIServer(WSGIHandler()).run()</pre>
<p>You will need to update these two rows:</p>
<ul>
<li>sys.path += ['/Library/WebServer/mydjangoapp']</li>
<li>os.environ['DJANGO_SETTINGS_MODULE'] = &#8216;mydjangoapp.settings&#8217;</li>
</ul>
<p>Where &#8216;/Library/WebServer/gaworkflow should be replaced by PYTHONPATH that would allow your Django package to be imported&#8230; one directory level below the directory containing <span class="underline">init</span>.py.</p>
<p><strong>NOTE:</strong> Make sure you dispatch.fcgi is executable:</p>
<pre class="wiki">sudo chmod a+x dispatch.fcgi</pre>
<p>If everything worked out properly, you should have a working installation of gaworkflow.frontend using mod_fcgid.</p>
<h2 id="TroubleShooting">Trouble Shooting<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#TroubleShooting"><br />
</a></h2>
<ul>
<li>Check apache error logs.</li>
<li>run: sudo apachectl configtest</li>
<li>Check that dispatch.fcgi has #!/usr/bin/python and not #!/usr/bin/env python as that will screw up the environment variables at least w/ Apache 2 that ships on Mac OS 10.5.</li>
<li>run &#8216;python dispatch.fcgi&#8217; to see if you get any standard Python errors.</li>
</ul>
<h3 id="Apache2-error_log--warnmod_fcgid:stderr:OperationalError:unabletoopendatabasefile">Apache2 &#8211; error_log &#8212; [warn] mod_fcgid: stderr: OperationalError: unable to open database file<a class="anchor" title="Link to this section" href="https://woldlab.caltech.edu/cgi-bin/gaworkflow/wiki/FcTrackerApache2#Apache2-error_log--warnmod_fcgid:stderr:OperationalError:unabletoopendatabasefile"><br />
</a></h3>
<p>This means the database is not accessible by the user that is running apache. On Mac OS X 10.5, the user is _www. On Debian/Ubuntu Linux the user is www-data. See the &#8220;copy of gaworkflow code&#8221; section above for directions on changing file permissions for the database.</p></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.brandonking.net/blog/2008/01/31/django-app-mod_fcgid-apache-2-setup-on-mac-os-x-debianubuntu-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
