<?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>Nick Carroll &#187; lighttpd</title>
	<atom:link href="http://ca.rroll.net/tag/lighttpd/feed/" rel="self" type="application/rss+xml" />
	<link>http://ca.rroll.net</link>
	<description>Metabolising caffeine into code</description>
	<lastBuildDate>Thu, 01 Sep 2011 04:18:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Deploying your Django app on Joyent Shared Accelerators</title>
		<link>http://ca.rroll.net/2009/07/12/deploying-your-django-app-on-joyent-shared-accelerators/</link>
		<comments>http://ca.rroll.net/2009/07/12/deploying-your-django-app-on-joyent-shared-accelerators/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 06:56:49 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[Joyent]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[ThoughtWorks]]></category>

		<guid isPermaLink="false">http://ca.rroll.net/?p=208</guid>
		<description><![CDATA[This guide is basically a rehash of this posting in Joyent&#8217;s support forums. I am reproducing it below to include my experiences as I found some discrepancies with the posting in the Joyent forums. Joyent Shared Accelerators don&#8217;t allow you to deploy your Django app using mod_python, so you have to create a proxy path [...]]]></description>
			<content:encoded><![CDATA[<p>This guide is basically a rehash of this <a href="http://discuss.joyent.com/viewtopic.php?pid=193224#p193224">posting</a> in Joyent&#8217;s support forums.  I am reproducing it below to include my experiences as I found some discrepancies with the posting in the Joyent forums.</p>
<p>Joyent Shared Accelerators don&#8217;t allow you to deploy your Django app using mod_python, so you have to create a proxy path that diverts traffic to lighttpd and FastCGI to serve your Django app.</p>
<p>For this guide you will need to replace ${USER} with your username on Joyent&#8217;s server, the hostname of your server as ${HOST}, and your DNS domain as ${DOMAIN}.</p>
<p><strong>Set up Apache as a proxy server for lighttpd</strong></p>
<p>You no longer need to submit a support ticket to request a port number for lighttpd.  You can just go to Virtualmin for your server (https://virtualmin.joyent.us/${HOST}/) > Other Tools > Check ports to view a list of available port numbers that have been reserved for you.  Pick one and note it down.  We will refer to this port number as ${PORT}.</p>
<p>Set up a directory structure for lighttpd:</p>
<pre name="code" class="java">
mkdir -p ~/etc/init.d
mkdir -p ~/etc/lighttpd/vhosts.d
touch ~/logs/lighttpd.error.log ~/logs/lighttpd.access.log
</pre>
<p>Using a text editor, create ~/etc/lighttpd/lighttpd.conf:</p>
<pre name="code" class="java">
#-- Lighttpd modules

server.modules = ( "mod_rewrite",
                                "mod_redirect",
                                "mod_access",
                                "mod_cgi",
                                "mod_fastcgi",
                                "mod_compress",
                                "mod_accesslog",
                                "mod_alias" )

#-- Fundamental process configs
server.port = ${PORT}
server.username = "${USER}"
server.groupname = server.username
var.base = "/users/home/" + server.username
server.pid-file = base + "/var/run/lighttpd.pid"

#-- Logging
server.errorlog = base + "/logs/lighttpd.error.log"
accesslog.filename = base + "/logs/lighttpd.access.log"

#-- Default
server.document-root = base + "/web/public"
server.indexfiles = ( "index.php", "index.html",  "index.htm", "default.htm" )

#-- Security
url.access-deny = ( "~", ".inc", ".ht" )

#-- Mimetypes
include_shell "cat " + base + "/etc/lighttpd_mimetypes.conf"

#-- VHOSTS
</pre>
<p>Create ~/etc/lighttpd/mimetypes.conf:</p>
<pre name="code" class="java">
mimetype.assign             = (
".pdf"          =>      "application/pdf",
".sig"          =>      "application/pgp-signature",
".spl"          =>      "application/futuresplash",
".class"        =>      "application/octet-stream",
".ps"           =>      "application/postscript",
".torrent"      =>      "application/x-bittorrent",
".dvi"          =>      "application/x-dvi",
".gz"           =>      "application/x-gzip",
".pac"          =>      "application/x-ns-proxy-autoconfig",
".swf"          =>      "application/x-shockwave-flash",
".tar.gz"       =>      "application/x-tgz",
".tgz"          =>      "application/x-tgz",
".tar"          =>      "application/x-tar",
".zip"          =>      "application/zip",
".mp3"          =>      "audio/mpeg",
".m3u"          =>      "audio/x-mpegurl",
".wma"          =>      "audio/x-ms-wma",
".wax"          =>      "audio/x-ms-wax",
".ogg"          =>      "audio/x-wav",
".wav"          =>      "audio/x-wav",
".gif"          =>      "image/gif",
".jpg"          =>      "image/jpeg",
".jpeg"         =>      "image/jpeg",
".png"          =>      "image/png",
".xbm"          =>      "image/x-xbitmap",
".xpm"          =>      "image/x-xpixmap",
".xwd"          =>      "image/x-xwindowdump",
".css"          =>      "text/css",
".html"         =>      "text/html",
".htm"          =>      "text/html",
".js"           =>      "text/javascript",
".asc"          =>      "text/plain",
".c"            =>      "text/plain",
".conf"         =>      "text/plain",
".text"         =>      "text/plain",
".txt"          =>      "text/plain",
".dtd"          =>      "text/xml",
".xml"          =>      "text/xml",
".mpeg"         =>      "video/mpeg",
".mpg"          =>      "video/mpeg",
".mov"          =>      "video/quicktime",
".qt"           =>      "video/quicktime",
".avi"          =>      "video/x-msvideo",
".asf"          =>      "video/x-ms-asf",
".asx"          =>      "video/x-ms-asf",
".wmv"          =>      "video/x-ms-wmv",
".bz2"          =>      "application/x-bzip",
".tbz"          =>      "application/x-bzip-compressed-tar",
".tar.bz2"      =>      "application/x-bzip-compressed-tar"
)
</pre>
<p>Finally, create an init script at ~/etc/init.d/lighttpd:</p>
<pre name="code" class="java">
#!/bin/sh

HOME=/users/home/${USER}
LIGHTTPD_CONF=$HOME/etc/lighttpd/lighttpd.conf
PIDFILE=$HOME/var/run/lighttpd.pid

case "$1" in

    start)
    # Starts the lighttpd daemon
    echo "Starting lighttpd"
    PATH=$PATH:/usr/local/bin /usr/local/sbin/lighttpd -f $LIGHTTPD_CONF

;;
    stop)
    # stops the daemon bt cat'ing the pidfile
    echo "Stopping lighttpd"
    kill `/bin/cat $PIDFILE`

;;
    restart)
    ## Stop the service regardless of whether it was
    ## running or not, start it again.
    echo "Restarting lighttpd"
    $0 stop
    $0 start

;;
    reload)
    # reloads the config file by sending HUP
    echo "Reloading config"
    kill -HUP `/bin/cat $PIDFILE`

;;
    *)
    echo "Usage: lighttpd (start|stop|restart|reload)"
    exit 1
;;
esac
</pre>
<p>Don&#8217;t forget to make the init script executable:</p>
<pre name="code" class="java">
chmod 755 ~/etc/init.d/lighttpd
</pre>
<p><strong>Proxy Apache to lighttpd</strong></p>
<p>Open up a web browser, and log into https://virtualmin.joyent.us/${HOST}/</p>
<p>Select the virtual server to configure.  Then go to Server Configuration > Proxy Paths > Add a new proxy path.  Enter the following values and click Create.</p>
<pre name="code" class="java">
Local URL path: /
Destination URLs: http://${DOMAIN}:${PORT}
</pre>
<p><strong>Configure Django environment</strong></p>
<p>Add the path to your Django app to the PYTHONPATH.  Add the following to your .profile and .bashrc files.</p>
<pre name="code" class="java">
export PYTHONPATH=/users/home/${USER}/src/django_projects
</pre>
<p><strong>Deploying your Django app</strong></p>
<p>Check out your django app to /users/home/${USER}/src/django_projects.  I will refer to this django app as ${APPNAME}.</p>
<pre name="code" class="java">
cd ~/src/django_projects
svn co svn+ssh://subversion_repos/site/${APPNAME}/trunk ${APPNAME}
</pre>
<p><strong>Create a MySQL database</strong></p>
<p>Normally I would use PostgreSQL cos it rocks, but unfortunately Joyent only provides database restrictions for specific users on MySQL.  So we&#8217;ll create a mysql user and grant it privileges to access a mysql database.</p>
<p>Open up a web browser and log into https://virtualmin.joyent.us/${HOST}/</p>
<p>Select ${DOMAIN} from the dropdown list > click &#8220;Edit Databases&#8221; > Click &#8220;Create a new database&#8221;.</p>
<p>I entered &#8220;production&#8221; into the Database name field so that my database will be called ${USER}_${DOMAIN}_production.  Then click &#8220;Create&#8221;.</p>
<p><strong>Create a database user</strong></p>
<p>In Virtualmin, Select ${DOMAIN} from the dropdown list.<br />
Click &#8220;Edit Mail and FTP Users&#8221; > &#8220;Add a user to this server&#8221;.<br />
Under Virtual domain user mailbox details, enter &#8220;django&#8221; into the Email address field.  This will create a mysql user called django-${DOMAIN}, and the auto-generated password will also be used for the mysql password in your django settings.py.</p>
<p>Expand &#8220;Quota and home directory settings&#8221;. Limit the user&#8217;s home directory quota to 1MB.<br />
Expand &#8220;Other user permissions&#8221;.  Allow the user access to the &#8220;${USER}_${DOMAIN}_production&#8221; database we just created. Click create.</p>
<p><strong>Configure project settings</strong></p>
<p>In settings.py modify your database settings to the following:</p>
<pre name="code" class="java">
FORCE_SCRIPT_NAME=''

import os.path
ROOT_DIR = os.path.abspath(os.path.dirname(file))

DATABASE_ENGINE = 'mysql'
DATABASE_NAME = '${USER}_${DOMAIN}_production'
DATABASE_USER = 'django-${DOMAIN}'
DATABASE_PASSWORD = 'password"
DATABASE_HOST = ''
DATABASE_PORT = ''

MEDIA_ROOT = os.path.join(ROOT_DIR, 'media')
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/media/admin/'

TEMPLATE_DIRS = (
    os.path.join(ROOT_DIR, 'templates'),
)

INSTALLED_APPS = (
    'django.contrib.sites',
    'django.contrib.admin',
    'django.contrib.flatpages',
)
</pre>
<p>Since the settings file has our MySQL password inside, don&#8217;t let others read it:</p>
<pre name="code" class="java">
chmod 600 ~/src/django_projects/${APPNAME}/settings.py
</pre>
<p>Then create the database tables in the usual fashion:</p>
<pre name="code" class="java">
./manage.py syncdb
</pre>
<p><strong>Create project init script</strong></p>
<p>Create ~/src/djangoprojects/${APPNAME}/etc/init.sh:</p>
<pre name="code" class="java">
#!/bin/sh

HOME="/users/home/${USER}" # Edit to your own username
PYTHONPATH=$HOME/src/django_projects
export PYTHONPATH

PROJECT_NAME="${APPNAME}"
PROJECT_DIR="$HOME/src/django_projects/$PROJECT_NAME"
PID_FILE="$HOME/var/run/$PROJECT_NAME.pid"
SOCKET_FILE="$HOME/tmp/$PROJECT_NAME.socket"
MANAGE_FILE="$PROJECT_DIR/manage.py"
METHOD="prefork"

case "$1" in

    start)
    # Starts the Django process
    echo "Starting Django project $PROJECT_NAME"
    python $MANAGE_FILE runfcgi maxchildren=2 maxspare=2 minspare=1 method=$METHOD socket=$SOCKET_FILE pidfile=$PID_FILE

;;
    stop)
    # stops the daemon by cat'ing the pidfile
    echo "Stopping Django project $PROJECT_NAME"
    kill `/bin/cat $PID_FILE`

;;
    restart)
    ## Stop the service regardless of whether it was
    ## running or not, start it again.
    echo "Restarting Django project $PROJECT_NAME"
    $0 stop
    $0 start

;;
    *)
    echo "Usage: init.sh (start|stop|restart)"
    exit 1

;;
esac
</pre>
<p>Make the init script executable:</p>
<pre name="code" class="java">
chmod 755 ~/src/djangoprojects/${APPNAME}/etc/init.sh
</pre>
<p><strong>Offload static media to lighttpd</strong></p>
<p>We don&#8217;t want Django to be serving static content, so any path that refers to static content will be served by the web server directly from ~/web/public.</p>
<p>Create a softlink from django&#8217;s admin media to ~/web/public/media/admin.</p>
<pre name="code" class="java">
mkdir ~/web/public/media
ln -s /usr/local/lib/python2.5/site-packages/django/contrib/admin/media/ ~/web/public/media/admin
</pre>
<p>Create a softlink from your project&#8217;s media directory to ~/web/public/media/public.</p>
<pre name="code" class="java">
mkdir -p ~/src/django_projects/project/media/public
ln -s /users/home/${USER}/src/django_projects/${APPNAME}/media/public ~/web/public/media/public
</pre>
<p><strong>Configure lighttpd</strong></p>
<p>Edit ~/etc/lighttpd/vhosts.d/${APPNAME}.conf.</p>
<pre name="code" class="java">
$HTTP["host"] =~ "(www.)?${DOMAIN}" {
    server.document-root = base + "/web/public"
    fastcgi.server = (
        "/${APPNAME}.fcgi" => (
            "main" => (
                "socket" => base + "/tmp/${APPNAME}.socket",
                "bin-environment" =>
                            ( "TZ" => "America/Chicago" ),
                "check-local" => "disable",
            )
        ),
    )

    url.rewrite-once = (
        "^(/media/admin.*)$" => "$1",
        "^(/media/public.*)$" => "$1",
        "^/favicon.ico$" => "/media/public/img/favicon.ico",
        "^(/.*)$" => "/${APPNAME}.fcgi$1",
    )
}
</pre>
<p>Then include vhosts.d/${APPNAME}.conf in your lighttpd.conf:</p>
<pre name="code" class="java">
echo 'include "vhosts.d/${APPNAME}.conf"' >> ~/etc/lighttpd/lighttpd.conf
</pre>
<p><strong>Schedule service start</strong></p>
<p>Create a Joyent bootup action in Virtualmin.  In Virtualmin, Select ${DOMAIN} from the dropdown list > go to Services > Booup Actions > Add a new bootup action.  Enter the following values in the input fields and click &#8220;Create&#8221;.</p>
<pre name="code" class="java">
Action name: init-${APPNAME}-django-site
Description: Init ${APPNAME} Django Site
Commands to run at startup: /users/home/${USER}/src/django_projects/${APPDNAME}/etc/init.sh start
</pre>
<p>Go back to the Bootup Actions, click &#8220;Add lighttpd&#8221;.  Enter the following values in the input fields and click &#8220;Create&#8221;.</p>
<pre name="code" class="java">
Action name: lighttpd-${APPNAME}-django-site
Description: Lighttpd ${APPNAME} Django Site
Commands to run at startup: /usr/local/sbin/lighttpd -f /users/home/${USER}/etc/lighttpd/lighttpd.conf
</pre>
<p>Open up your browser and go to your newly deployed Django app!</p>
<p>If you don&#8217;t see your site then you will have to do some debugging.  I didn&#8217;t get it first time as you&#8217;ll note that my instructions above are slightly different from the original post <a href="http://discuss.joyent.com/viewtopic.php?pid=193224#p193224">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ca.rroll.net/2009/07/12/deploying-your-django-app-on-joyent-shared-accelerators/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

