Shovel—Rails Deployment with Lighttpd

NOTE: See also my Dreamhost Recipe

DISCLAIMER: THIS SCRIPT CREATES DIRECTORIES, MAKES FILES, AND LAUNCHES PROCESSES ON YOUR TEXTDRIVE ACCOUNT. USE AT YOUR OWN RISK!

IT IS ALSO EXPERIMENTAL AND MAY HAVE BUGS!

Shovel is a customized Capistrano deployment file to setup config files on TextDrive in order to make it easier to start running Rails with lighttpd. It could also be modified to work with other shared hosts who run lighttpd.

Download Shovel lighttpd + Capistrano Task

It automatically does the tasks recommended on 10-15 pages of instructions spread across the following blogs:

Capistrano was previously called Switchtower.

What It Does

Shovel uses a basic Capistrano task to ssh into your TextDrive account and do the following things:

  • Makes a ‘lighttpd’ directory with a customized lighttpd.conf that works right out of the box.
  • Makes a customized lighttpdctrl script that can be used to start, stop, and restart your lighttpd server easily. (From code by Jarkko Laine)
  • Makes a ‘sites’ directory that will hold installations of all your deployed websites.
  • Performs Capistrano’s setup task to make the directories it needs. Also does an initial deployment of your site.
  • Makes ‘var’ folder for pid files and some logs.
  • Sets a crontab to automatically restart lighttpd for you if the server is rebooted.
  • Fixes a few basic Capistrano tasks to work better with TextDrive.

And, it runs in under a minute, automatically!

Assumptions

  • You’ve filed a ticket for your own lighttpd port. See Painless Lighttpd for the details.
  • You’re running subversion and you name your projects with the domain name that the project is for. So my subversion project for topfunky.net is in a repo called ‘topfunky.net’. You can manually edit deploy.rb to change this.
  • You’ve done at least one subversion checkout somewhere in on your remote server. This will cache the password there so things work smoothly.

Usage

There are three steps, or a fourth if you are on Mac OS X. You will also have to re-route your Apache config to lighttpd using the TextDrive webmin.

Step 0: Fix Ruby on Mac OS X or Ubuntu

The Ruby built-in to Mac OS X is broken in regards to Capistrano. (Reportedly, it is also broken on Ubuntu and you must use the pure Ruby version of SSH).

Fortunately, you can run my ru-ra-lim.sh script to automatically rebuild Ruby, Rails, lighttpd, and MySQL on your local box.

Or, install Darwinports and reinstall Ruby with

sudo port install ruby
sudo port install rb-rubygems
sudo gem install rails --include-dependencies
sudo gem install capistrano --include-dependencies
mkdir ~/backup_ruby
sudo mv /usr/bin/ruby ~/backup_ruby/
sudo mv /usr/bin/ri ~/backup_ruby/
sudo mv /usr/bin/irb ~/backup_ruby/
ln -s /opt/local/bin/ruby /usr/bin/ruby

You may also have to re-install other compiled gems, like RMagick.

Step 1: Install Capistrano

sudo gem install capistrano --include-dependencies
rehash
cap --apply-to path_to_my_rails_app

Step 2: Setup Shovel’s deploy.rb

The script requires you to setup your base domain first. For example, I signed up to TextDrive with topfunky.net, so I need to start the whole process with topfunky.net before I setup deployment for all my other domains.

NOTE: You can change this, but the script assumes it will be done first, so you’ll have to modify the lighttpd.conf if you start with another site first.

Ok…to prep a Rails project for Capistrano, do:

cd my_rails_app/config
rm deploy.rb
wget http://topfunky.net/svn/shovel/deploy.rb

Now edit the customized deploy.rb. While the file itself is 300+ lines long, modifying it is easy.

Change the following four lines at the top of the script:

set :application, 'my_domain_name.com' # The one you are deploying.
set :user, "funky_buffalo" # Your TextDrive username
set :txd_primary_domain, 'my_domain_name.com' # The domain you ssh to for your server.
set :lighty_port, 00000000 # The port given to you by TextDrive for running lighttpd

Step 2.5: Do a Subversion Checkout on Your Server

Capistrano needs to use the cached password for Subversion. Do a ‘svn co—username my_user_name http://my_domain.com/svn/my_project.com’ anywhere on your server (a tmp directory or wherever).

Step 3: Run setup_lighty

Now it’s time for the magic.

rake remote:exec ACTION=setup_lighty

You will be prompted for your ssh password.

If something goes wrong during the process, you can re-run it again, or run ACTION=teardown_lighty to delete the folders that were made (the cron job will have to be deleted manually).

To test it, dial up http://your_domain.com:your_port in a web browser. If you don’t see it, you might have to ssh into your server and manually start lighttpd with

./lighttpd/lighttpdctrl start

You will also have to use the TextDrive webmin to re-route Apache to your new lighttpd port (Painless lighttpd).

Further Deployment

Now, you can push your changes to the live server by checking them into Subversion and running:

rake deploy

Isn’t that great?

Advanced Features

Here are some more things you can do with this script.

daedalus

UPDATE: Don’t install daedalus

Adding subdomains or other domains on the same host.

You can run multiple websites or subdomains from the same lighttpd config file.

  1. Copy deploy.rb to a new rails project, and change :application to refer to your extra domain or subdomain.
  2. Edit lighttpd/lighttpd.conf and copy/paste the ‘host’ section.
  3. Replace all instances of your original domain with the new one.
  4. From your new Rails project, run ‘rake remote:setup’ and then ‘rake deploy’. A new folder will be made under ‘sites’ that represents your additional domain.

Modifying the Recipe

Capistrano is fantastically easy to work with, but will die easily, as it should. It’s made for deploying rock solid applications to production, or to fail if anything goes wrong.

More on that in the next few weeks.

Troubleshooting

/opt/local/bin/ruby

If you re-install Ruby using DarwinPorts, your new Rails projects will reference the new location in the hash-bang:

#!/opt/local/bin/ruby

You’ll have to edit this to run dispatch.fcgi (and others in ./script) on a remote server.

Or, start your new project with

rails my_project.com --ruby /usr/local/bin/ruby

Download

Download Shovel lighttpd + Capistrano Task

Contribute

Comment about Shovel on my Blog

Subversion repo at

svn co http://topfunky.net/svn/shovel shovel

Author

Geoffrey Grosenbach, boss@topfunky.com

References & Other Articles on lighttpd

Nuby on Rails

Geoffrey Grosenbach / Ruby / Code / Graphics / Design / Rails / Merb / Javascript / CSS

Manufactured with

Subscribe

Subscribe (RSS)