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:

https://gist.github.com/4220718

  • 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):

https://gist.github.com/4220698

Passenger and Nginx

First let's install Passenger and Nqinx:

https://gist.github.com/4220722

Setup script to control Nginx:

https://gist.github.com/4220728

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

https://gist.github.com/4220752

  • 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.

MySQL

https://gist.github.com/4220778

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
comments powered by Disqus