So I got Sinatra running on my shared host, Webfaction. As I’ve said in past posts, I like working with Sinatra because it lets me write Ruby webapps without all the code required by larger libraries like Ruby on Rails. With Sinatra, I write more code and so it augments my Ruby learning and lets [...]
So I got Sinatra running on my shared host, Webfaction. As I’ve said in past posts, I like working with Sinatra because it lets me write Ruby webapps without all the code required by larger libraries like Ruby on Rails. With Sinatra, I write more code and so it augments my Ruby learning and lets me experiment. Often I try to rewrite the PHP apps from school using Sinatra.
Getting Sinatra working on Webfaction required a fair number of steps and took a few hours, mostly for investigation and learning. It required a good deal of reading about Nginx, a front-end webserver that is an alternative to Apache, and Thin, a backend application server that is overtaking Mongrel in usage and popularity. Here’s a how-to and some links I found helpful in case you’re looking to get Sinatra running on Webfaction.
Steps
Custom Ruby install
Create two custom apps in the Webfaction Control Panel
Build Nginx from source
Start Nginx
Create a website in Webfaction Control Panel
Gem Install Thin
Your Sinatra app
Example main sinatra app file
Example rackup file for config.ru
Make a Thin config file
Example app.yml file
Start Thin
Chillax, its almost quittin’ time… you’re done! Read some more stuff if you want
Custom Ruby Install
First I did a custom ruby install, building from source. At the moment I can’t remember why I did this. But… I did, for some reason. Something in all the articles I read on Webfaction convinced me this was a good idea. And now I’m running ruby 1.9.1p376.
Create two ‘custom apps listening on port …’ apps
Both Nginx and Thin need “Custom application listening on port…” created for them in the Webfaction control panel. Name one of the custom apps “Nginx” and make the other custom app the same name as your Sinatra application. Keep track of the port numbers for these custom apps as you will need the port numbers for the Nginx and Thin config files. After you’ve created the “Custom app listening on port … ” apps, named nginx and YOUR_SINATRA_APP_NAME respectively, you will have a directory for each of them in ~/webapps/. *Note: If you want several instances of Thin, you need to create however many “Custom app listening on port … ” apps in the Webfaction control panel.
Build Nginx from source, tell it about Thin
I followed examples in the link below for installing Nginx on Webfaction. Do your Nginx install and config in the ~/webapps/nginx directory. In your nginx.conf file, you need to tell Nginx what port your Thin app is running on. Here’s a portion of my nginx.conf file, note that this is not the entire file. You just need something like this http { } block.
upstream sinatra {
server 127.0.0.1:12345;
# Don't use 12345, but use your sinatra app port number
}
server {
listen 67891;
# don't use 67891, but use your NGINX port number
server_name YOUR_DOMAIN.COM;
root /home/USER_DIR/webapps/SINATRA_APP_DIR/public;
location / {
#root html;
#index index.html index.htm;
if (-f $request_filename) {
break;
}
if (!-f $request_filename) {
proxy_pass http://sinatra;
break;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Start Nginx
When you’re ready, start Nginx from within your ~/webapps/nginx by running the command
./sbin/nginx
Create website, assign a domain to be used, and assign it the Nginx custom app
In the Webfaction Control Panel -> Sites, you have to associate a domain (dev.example.com) with an application (nginx, in this case) so you can see your Sinatra app. Choose an existing domain of yours. Assign it the Nginx app.
Install Thin, tell it about your Sinatra app
For Thin, you just need to run
gem install thin
Put your Sinatra code in your ~/webapps/APP_NAME/
At the bare minimum, you need:
E.g. main app file
# app.rb require 'rubygems' require 'sinatra' get '/' do "You're online!" end # Note: Sinatra does not require you have line breaks after/before your strings # my syntax highlighter is acting up!
Example rackup file for config.ru
# config.ru # this file goes in APP_ROOT/config/config.ru # require whatever your main app file is named # for app.rb, require 'app' require 'app' run Sinatra::Application
Make a Thin config file
Make a directory called ./config inside your application directory. You’ll need to generate a Thin config file and tell Thin what port it should run on, what environment it should run in e.g. production. My Thin config command is below. Note, except for the -C flag, items in CAPS are where you need to use information about your own set up:
thin config -s 1 -a 127.0.0.1 -p PORT_NUMBER_FROM_CUSTOM_APP -e production -C SINATRA_APP_FILE_NAME.yml
That command will generate a .yml file that you need to pass as a flag to Thin when you enter the start command. **I’m actually unable to start Thin if I’m using the auto-generated config file. So i just type out my own, which I will show you below.
***Note if you want to use multiple Thin instances, (you would need to have created multiple custom apps) then you need to change the -s 1 bit in the config command above to reflect that.
E.g. app.yml file
--- # APP_ROOT_DIR == whatever your custom app was named chdir: /home/YOUR_NAME/webapps/APP_ROOT_DIR environment: production address: 127.0.0.1 port: CUSTOM PORT NUMBER from your CUSTOM APP timeout: 30 log: /home/YOUR_NAME/webapps/APP_ROOT_DIR/log/thin.log pid: /home/YOUR_NAME/webapps/APP_ROOT_DIR/tmp/pids/thin.pid max_conns: 1024 max_persistent_conns: 512 require: [] rackup: /home/YOUR_NAME/webapps/APP_ROOT_DIR/config/config.ru daemonize: true wait: 30 servers: 1
Start Thin
thin start -C ./config/file-name.yml
Links:
Custom ruby install on webfaction – https://help.webfaction.com/89
General Nginx and Thin set up – http://wiki.rubyonrails.org/deployment/nginx-thin
Nginx on Webfaction set up – https://help.webfaction.com/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=180
Sample Nginx config file, sample Thin config file. Sheds some light on what is required to run Sinatra apps on webfaction – http://forum.webfaction.com/viewtopic.php?pid=12252
Some thin commands – http://www.softiesonrails.com/2008/4/27/using-thin-instead-of-mongrel
Rackup file example, and more – http://www.isnorcreative.com/blog/29
Content © professional dilettante
Proudly powered by WordPress
Theme designed by Artisan Themes
25 queries.
0.612 seconds.
Thanks, I found this post very helpful!