Nginx Init Script

Posted in Justin Shacklette on October 31st, 2008 by admin

This blog is running Wordpress on Nginx on a 256 slice from Slicehost.  I’ve diligently followed all of the awesome articles by Slicehost’s tutorial master, Pickled Onion.  And I even googled some other great stuff.  But when it came down to PHP, spawn-fcgi, and particularly the Ubuntu way of daemon initialization, I had to blaze a new trail.

Here is the spawn-fcgi-php script that I came up with:

#! /bin/sh
 
### BEGIN INIT INFO
# Provides:          spawn-fcgi-php
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts FastCGI for PHP
# Description:       starts FastCGI for PHP using start-stop-daemon
### END INIT INFO
 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=spawn-fcgi-php
PID=/var/run/spawn-fcgi-php.pid
DAEMON=/usr/local/bin/spawn-fcgi
DAEMON_OPTS="-f /usr/bin/php-cgi -a 127.0.0.1 -p 55155 -u www-data -g www-data -P $PID"
 
test -x $DAEMON || exit 0
 
set -e
 
case "$1" in
  start)
        echo "Starting $NAME: "
        start-stop-daemon --start --pidfile $PID --exec $DAEMON -- $DAEMON_OPTS
        echo "done."
        ;;
  stop)
        echo "Stopping $NAME: "
        start-stop-daemon --stop  --pidfile $PID --retry 5
        rm -f $PID
        echo "done."
        ;;
  restart)
        echo "Stopping $NAME: "
        start-stop-daemon --stop  --pidfile $PID --retry 5
        rm -f $PID
        echo "done..."
        sleep 1
        echo "Starting $NAME: "
        start-stop-daemon --start --pidfile $PID --exec $DAEMON -- $DAEMON_OPTS
        echo "done."
        ;;
  *)
        echo "Usage: /etc/init.d/$NAME {start|stop|restart}" >&2
        exit 1
        ;;
esac
 
exit 0

A pretty straight forward script the does just two things: start and stop. The restart is just a convenience option, composed of a stop plus a start with a sleep 1 in between.

start

The script starts spawn-fcgi from lightty with a couple of options, most importantly a port and a pid-file. By default, spawn-fcgi will start up 5 child instances of php-cgi. So a quick ps aux | grep cgi will show 6 processes. From the limited (aka non-existent) performance data of my blog, this seems like a fine number.

stop

The script stops all those instances by sending a SIGTERM (-15) to the parent process recorded in the pid-file. Stopping with the --retry 5 option, will first send the default SIGTERM signal and check for 5 seconds if the process is still running, then send a KILL (-9) signal and wait another 5 seconds before erroring out.

Digging Deeper: When stopping, pass only one matching option to the start-stop-daemon. In most cases, it’s easiest to just pass the pid-file via the --pidfile $PID option (you might find some scripts online that pass --exec $DAEMON as a second matching option, but this didn’t work for me).

Just copy this script into /etc/init.d/, given it 755 permissions, and add it to the init sequence with sudo update-rc.d spawn-fcgi-php defaults. After a quick reboot, you should see 6 php-cgi processes running.

First Post

Posted in Justin Shacklette on October 23rd, 2008 by admin

Welcome to the humble beginnings of my blog.  When I start something new, I always try to start with a goal.  And I also believe strongly in having one, and only one, primary goal.  Sure you can have secondary goals, and yes, things certainly change over time.  So…

The primary goal for this blog is my writing. I desperately want to be a better, faster writer.

Once the choice of a primary goal is made it acts like The Force–and as Obi-Wan says “The Force is what gives a Jedi his power. It’s an energy field created by all living things. It surrounds us, and penetrates us. It binds the galaxy together.”  Everything in this blog, especially code-filled posts about Flex animation or Nginx configuration files, will always be about my writing (that’s the surround and penetrate part that binds this blog together).  I want to find a voice that is terse, but informative.  I want to go deep into the details, but I don’t want to bore.  And it’s gotta be funny.  Along the way, I expect there’ll be some swearing, cursing, and lots of frustration, but there always is when code, work, and life collide.

Why?  It’s probably something from my science upbringings, but I always like to question decisions, including mine.  First, I don’t think I’ve ever seen a job posting without “Excellent written and verbal communication skills” in it somewhere.  Second, and more importantly, communication is one of the keys to success in today’s global society.  And that’s that, not much to question.

Please enjoy, and I do so love feedback.

DrunkOnSoftware.com :: On The Air

Posted in Drunk On Software, Jon Rose on October 21st, 2008 by jonr

James Ward and I launched our new video podcast tonight, DrunkOnSoftware.com.  The concept is a bit silly, and of course tongue in cheek as we aren’t drunks, but one of the main things we have in common is the ability to not take ourselves too seriously.  We do love talking about software and the occasional drink.  So, we figure why not make that our niche… everybody needs a niche.  We are planning on talking about all things software, but will probably focus more on Adobe Flex than other things.

In the first episode, we talk about the Adobe Flash Player 10.  James shares about the main updates of interest to developers.

Please contact us if you are in the Denver area and would like to appear on the Videocast.  We would love to have you!

Flex & MVC

Posted in Jon Rose on October 15th, 2008 by jonr

You know you had a good idea when you see someone else implement it, and they do it better than you may have yourself.  Brian Lesser posted an article this week on O’Reilly titled, “MVC As Anti-Pattern.”  I have been meaning to write a similar article for a while.  Internally at Gorilla Logic, Stu Stern and I have had a long running dialog questioning the wisdom of bringing in full blown MVC frameworks into Flex (especially model 2 like implementations with a front controller).

Brian Lesser does a good job of arguing against implementing the traditional MVC pattern within Flex applications.  Lesser outlines the language/framework features in Flex that are far different than the language MVC grew out of many years back (i.e. Smalltalk), including discussing data binding and event listeners in Flex.  In addition to his main points, I am still a little awestruck at the willingness of developers to drag in a framework that quadruples or more the amount of code without adding much clear value.

Obviously, architecture is important and Lesser does spend some time discussing what the layers look like within Rich Client applications.  I am still digesting this section, but my initial reaction is that his ideas fit well with how I view application architecture in context of RIA’s (hard to go wrong when you borrow from Eric Evans).  It really is an exciting time as the move to rich client runtimes not only allows us to build better applications for end users, but forces us to think differently about the architecture.

Adobe Releases Flash 10

Posted in Jon Rose on October 14th, 2008 by jonr

Flash Player 10 is out tonight.  There are some pretty nice looking features, but it looks like we will have to wait for Flex 4 to really take advantage of them.  Read my write up on the release on InfoQ.com here: http://www.infoq.com/news/2008/10/flash10.

More FlexMonkey for Flex Testing

Posted in FlexMonkey, Jon Rose on October 13th, 2008 by jonr

Interviews are still my favorite part of writing for InfoQ.com.  This week I interviewed Stu Stern on FlexMonkey.