Skip to content

Instantly share code, notes, and snippets.

@dimoreira
Last active December 18, 2015 23:49
Show Gist options
  • Save dimoreira/5864628 to your computer and use it in GitHub Desktop.
Save dimoreira/5864628 to your computer and use it in GitHub Desktop.
Production setup with RVM + Nginx + Unicorn in Ubuntu

Setup the RVM + Nginx + Unicorn in Ubuntu


Init by installing necessary linux headers

$ sudo apt-get update
$ sudo apt-get install build-essential vim git-core curl
$ sudo apt-get install libcurl4-openssl-dev libx11-dev libffi-dev tcl-dev tk-dev

Next install RVM

$ \curl -L https://get.rvm.io | bash -s

And add it to your .profile:

$ vim .profile
...

# Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

Run RVM Requirements:

$ rvm requirements

And install all the necessary sources that RVM needs.

Compile and Install Ruby

$ rvm install 2.0.0 # or another ruby version (1.9.2, 1.9.3)

Install Rails gem

$ gem install rails # current stable is 4.0.0

# for another versions run
$ gem install rails --version=3.2.13

Install Nginx (updated and stable)

First install dependecies for ppa repository:

$ sudo apt-get install python-software-properties

Then add the repository for apt-get, update the packages and install:

$ sudo add-apt-repository ppa:nginx/stable
$ sudo apt-get update 
$ sudo apt-get install nginx

Remove the default site configuration from /etc/nginx/sites-available/default and /etc/nginx/sites-enabled/default:

$ sudo rm -f /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default

Create a rails project with configuration files

For test purposes, create a rails project and add configuration files for nginx and unicorn:

$ rails new blog

Add unicorn to the Gemfile and bundle install it:

# Gemfile
gem 'unicorn'
$ bundle install

After the bundle completes gem install, create 2 configuration files:

# config/nginx.conf
upstream unicorn {
  server unix:/path/to/your/application/tmp/unicorn.blog.sock fail_timeout=0;
}

server {
	listen 80 default;
	root /path/to/your/application/public;
	
	keepalive_timeout 10;
	
	try_files $uri/index.html $uri @unicorn;
	location @unicorn {
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header Host $http_host;
		proxy_redirect off;
		proxy_pass http://unicorn;
	}
}
# config/unicorn.rb
root = "/path/to/your/app/"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn_error.log"
stdout_path "#{root}/log/unicorn_out.log"

listen "#{root}tmp/sockets/unicorn.sock"
worker_processes 4 
timeout 30

Symlink the config/nginx.conf to the sites-enabled folder on nginx configuration path:

$ sudo ln -s /path/to/your/application/config/nginx.conf /etc/nginx/sites-enabled/blog

WebServer restart

Now that configuration is set, restart Nginx:

$ sudo service nginx restart
 * Restarting nginx nginx

Now open your application url on browser to see it working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment