Deploying Sinatra via Thin and Lighttpd
I’ve previously written about deploying Sinatra to Dreamhost, both with FastCGI, and also Passenger (mod_rails). This howto will cover how to deploy Sinatra to a load-balanced reverse-proxy setup using Lighttpd and Thin.
1) Install Lighttpd and Thin
# Figure out lighttpd yourself, it should be handled by your # linux distro's package manager # For thin: gem install thin
2) Create your rackup file - the “require ‘app’” line should require the actual sinatra app you have written.
require 'sinatra' Sinatra::Application.default_options.merge!( :run => false, :env => :production ) require 'app' run Sinatra.application
3) Setup a config.yml - change the /path/to/my/app path to reflect reality.
--- environment: production chdir: /path/to/my/app address: 127.0.0.1 user: root group: root port: 4567 pid: /path/to/my/app/thin.pid rackup: /path/to/my/app/config.ru log: /path/to/my/app/thin.log max_conns: 1024 timeout: 30 max_persistent_conns: 512 daemonize: true
4) Setup lighttpd.conf - change mydomain to reflect reality. Also make sure the first port here matches up with the port setting in config.yml.
$HTTP["host"] =~ "(www\.)?mydomain\.com" { proxy.balance = "fair" proxy.server = ("/" => ( ( "host" => "127.0.0.1", "port" => 4567 ), ( "host" => "127.0.0.1", "port" => 4568 ) ) ) }
5) Start thin and your application . I have a rake script so I can just call “rake start” rather than typing this in. It’s just a few lines to do that.
thin -s 2 -C config.yml -R config.ru start
Go to mydomain.com/ and see the result! Everything should be setup now, check it out at the domain you setup in your lighttpd.conf file.
Variations - nginx - I haven’t looked into the config file syntax, but there isn’t any reason that this exact same approach wouldn’t work with nginx. Thin and the rackup file stay the same, while the nginx layer has to be configured to reverse proxy to the thin install.
Variations - More Thin instances - To add more thin instances, change the -s 2 parameter on the thin start command to be how ever many servers you want. Then be sure lighttpd proxies to all of them by adding more lines to the proxy statements. Then restart lighttpd and you’re done.
June 29th, 2008 at 8:01 am
Here ist the config for an nginx server - your approach works very well with nginx:
upstream my_app {
server localhost:4567;
server localhost:4568;
}
server {
listen 9000;
server_name http://www.mydomain.com;
location / {
proxy_pass http://my_app;
}
}
Thanks for sharing your work!
Ralf
June 14th, 2012 at 2:23 pm
How does the config file work for deployment? Will we always have to know what the absolute path is to the project or can we simply use “../” or “/” etc..
May 13th, 2013 at 6:07 am
This is certainly interesting, You are incredibly professional writer. We’ve registered your current feed and show off to in the seek out added of your respective excellent submit. Additionally, I’ve got provided your internet site within my social networking sites