1. OMFG = RVM + Rails 3 (w/ Bundler) + Mongo + MongoMapper + nginx + passenger + capistrano

    Spent the last 24 hours setting up MongoDB, MongoMapper and nginx in a production environment.  Much of that time was spent wrestling with these technologies, and some of that time was spent wrestling with Thunder Snow (NYC Snowpocalypse + Lightening & Thunder — it’s scary cool).

    Documentation generally sucks and is all over the place, here’s what worked for me:

    1) Get your servers running (1 app, 1 db for me) and do the basic lockdown.  I setup Rackspace Cloud servers and followed their guides for basic setup


    When setting up iptables on your DB box, be sure to run the following command to open up the MongoDB port (this should be done before the DROP is added):


    iptables -A INPUT -p tcp --dport 27017 -j ACCEPT

    2) Use RVM

    Install ruby (1.8.7 or whatever is easy) and use that to get RVM, I think this tutorial basically worked:  http://ruby.about.com/od/rubyversionmanager/ss/installrvmlinux.htm

    **RVM & SUDO** Sudo starts a new subshell, so you lose your RVM environment.  So you need to run rvmsudo (provided by rvm) if you need to sudo some ruby command.  This gotcha took 2 hours of my life.

    If you’re using Capistrano, add the following to the top of Capfile to enter your desired rvm environment when executing on your server:


    # Add RVM's lib directory to the load path.
    $:.unshift(File.expand_path('./lib', ENV['rvm_path']))
    # Load RVM's capistrano plugin.
    require "rvm/capistrano"
    # Set it to the ruby + gemset of your app, e.g:
    set :rvm_ruby_string, '1.9.2'
    set :rvm_type, :user

    Another thing about capistrano while we’re here: It doesn’t have “bundle install” built in.  I suggest the following tutorial:  http://rand9.com/blog/bundler_0_9_1_with_capistrano

    3) Setup nginx

    If you’re going to use passenger, don’t install nginx; passenger will do it for you.  (I had to uninstall nginx b/c it didn’t play nice w/ the passenger install).  Follow these instructions from passenger: http://www.modrails.com/install.html

    I created a user on my app box and have capistrano deploy into that users ~/app/ directory.  So instead of fucking around with permissions, i just told nginx to use that same user and group (very top of nginx config file).

    I added the server to nginx as passenger recommended and changed the default server (that was already in the conf file) to listen on port 8080, so it wouldn’t fuck with mine.

    4) setup mongodb

    http://www.mongodb.org/display/DOCS/Rails+3+-+Getting+Started  <== pretty good but be sure to see my comment (disqus Dan Spinosa) regarding the Initialization section

    Authentication is a good idea in production.  To setup the users in mongo, mongo’s tutorial is good: http://www.mongodb.org/display/DOCS/Security+and+Authentication

    Once you’ve done that, enable authentication in your mongo configuration; just uncomment the `auth = true` setting.

    Finally, tell your application to connect w/ a username and password.  I modified the config/initializers/mongo.rb so it starts like this:


    MongoMapper.connection = Mongo::Connection.new(SERVER, PORT)
    MongoMapper.database = "name-#{Rails.env}"
    if APP_CONFIG[:mongo_username] && APP_CONFIG[:mongo_password]
      MongoMapper.database.authenticate(APP_CONFIG[:mongo_username], APP_CONFIG[:mongo_password])

    tags:  development 

  2. blog comments powered by Disqus