Search code examples
ruby-on-railsweb-applicationsstaging

Best Practices for a Web App Staging Server (on a budget)


I'd like to set up a staging server for a Rails app. I use git & github, Cap, and have a VPS with Apache/Passenger. I'm curious as to the best practices for a staging setup, as far as both the configuration of the staging server as well as the processes for interacting with it. I do know it should be as identical to the production server as possible, but restricting public access to it will limit that, so tips on securing it only for my use would also be great.

Another specific question would be whether I could just create a virtual host on the VPS, so that the staging server could reside alongside the production one. I have a feeling there may be reasons to avoid this, though.


Solution

  • Cheap and Easy answer:

    1) Point staging.domainname.com at your VPS.

    2) Add in a virtual host for staging, pointing to the staging copy of the app.

    3) Add in a staging environment setting. (Did you know you could define new environments in Rails? Fun stuff!) I think this is as simple as copying production.rb to staging.rb and tweaking as necessary, plus updating database.yml.

    4) In ActionController, add in code similar to the following

       if (ENV["RAILS_ENV"] == "staging")
         before_filter :verifies_admin
       end
    

    Where verifies_admin can be anything you want. I suggest using HTTP basic authentication -- cheap and easy.

    def verifies_admin
      authenticate_or_request_with_http_basic do |username, password|
        username == "foo" && password == "bar"
      end
    end
    

    Note that this may bork your connection to that payment site if they are making inbound requests to you, although that is simple enough to fix (just turn off the before_filter for the appropriate controllers and/or actions.)

    Better answer:

    1) Buy a second VPS configured from the same image as your regular VPS, and/or configured from the same install-from-the-bare-metal script (I like Capistrano & Deprec for this).

    2) Point staging.domainname.com at it.

    3) Otherwise its the same as the other option.

    Things to think about:

    1) Should I have a staging database as well? Probably, especially if you're going to be testing schema changes.

    2) Should I have some facility for moving data between the staging and production systems?

    3) Can catastrophic failure of my staging application take down the main application? Best hope the answer is no.