Ubuntu 12.10 Server Setup for Ruby on Rails, Passenger, Nginx

So we all know what a pain in the ass it is to setup a new server for Ruby on Rails. This is why I love to use Heroku for hosting. There are times that this is not an option so we have to use a hosted linux server. For this walk thru I used Rackspace with Ubuntu 12.04 for hosting. This is about as fully detailed as possible.

Server Setup

  • Create new server on Rackspace
  • Login to new server
    • ssh root@IPaddress (you can also use the domain name if you have the DNS setup already)
    • accept the RSA key
    • use the password provided at server setup
    • change the password that was provided at setup:
      • $ passwd
  • Create a new user for deployment
    • $ sudo adduser “username”
      • accept the defaults
    • add new user to staff and sudo groups:
      • $ usermod -a -G staff “username”
      • $ usermod -a -G sudo “username”
    • switch user
      • $ su “username”
  • Update and Install dependencies:

  • To make our live easier, lets go ahead and add our ssh keys to the new server so we do not have to sign in every time (this is done from your machine and not on the server):

$ cat ~/.ssh/id_rsa.pub | ssh root@IPaddress “cat >> ~/.ssh/authorized_keys”

Install Ruby

Install your Ruby (obviously you use the version that you want. At this point many people use a Ruby version management like RVM or RBENV. As these servers are normally setup for just on application we will use the system version of Ruby):

Passenger and Nginx

First let’s install Passenger and Nqinx:

Setup script to control Nginx:

You should be able to control Nginx. To start and stop server:

  • Go to your IPaddress (or domain name) and you should see “Welcome to nginx”
  • After installing nginx, you should see the following message in the terminal:

Screen Shot 2012-11-22 at 5.42.34 PM

  • Setup the nginx.conf for our server
    • edit the /opt/nginx/conf/nginx.conf
    • add the IPaddress to the server name (You may use the domain name if you have one)
    • add the path to your apps public directory to the root
    • add the user to the nginx.conf
      • user “username” staff;
    • if necessary, set the rails environment using rails.env

 Screen Shot 2012-11-23 at 1.16.58 PM

Database Setup

At this point there are a lot of different ways to go. The database needs to be setup. There are multiple options for databases such as MySQL, PostgreSQL, and Mongodb.


Deploying Rails

There are also several options for deploying your Rails application. The option that I am going to use is a nice little gem called git-deploy.

After following the directions for git-deploy:

  • ssh into the server
  • cd into you application
  • check your Rails environment$ echo $RAILS_ENV
    • if it needs to be set:
      • add “export RAILS_ENV=staging” to the the ~/.bashrc
      • $ source ~/.bashrc
  • run bundle
  • rake db:create
  • rake db:schema:load (if needed)
  • rake db:migrate
  • touch tmp/restart.txt

4 comments on “Ubuntu 12.10 Server Setup for Ruby on Rails, Passenger, Nginx

  1. So I haven’t done this in most of a decade, but I have done this before with various LAMP servers.

    What I don’t see in this that A) I used to see mentioned all the time, and B) never see really discussed in terms of cloud servers Amazon to Rackspace to even things like Google app engine, heroku, engine yard, is what your security model is.

    What are you doing on the firewall?
    How are you securing nginx?
    What does Ruby/Rails need to secure that application?

    How often, how do you, get updates to ubuntu, rails, ruby, nginx, etc.

  2. This is an incredibly useful post. I was setting up a VirtualBox/Ubuntu 12.04 image for local development and this saved me a lot of time. Thanks for writing this up.

  3. […] Ubuntu 12.10 Server Setup for Ruby on Rails, Passenger, Nginx. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: