Skip to content

Instantly share code, notes, and snippets.

@jgryffindor
Created August 25, 2013 20:14
Show Gist options
  • Save jgryffindor/6336029 to your computer and use it in GitHub Desktop.
Save jgryffindor/6336029 to your computer and use it in GitHub Desktop.
# This file should be placed on the directory of ~/blog/config
upstream unicorn {
server unix:/tmp/unicorn.todo.socket fail_timeout=0;
}
server {
listen 80 default;
#server_name example.com;
root /home/username/blog/public;
try_files $uri/index.html $uri @unicorn;
error_page 500 502 503 504 /500.html;
location @unicorn {
proxy_pass http://serverIP:3000;
}
}

Packages

  • VirtualBox 3.2.14

  • Ubuntu server 12.04

  • Nginx 1.2.3

  • Unicorn v4.3.1

  • Mongodb v2.2.0

  • Mongoid 3.0.5

  • Rails 3.2.8

  • Ruby 1.9.3-p194

Install dependent libraries and tool

For all

sudo apt-get install build-essential git-core

For bundler

sudo apt-get install zlib1g-dev

For rbenv

sudo apt-get install libreadline5-dev
sudo apt-get install curl

For test (optional)

sudo apt-get install libssl-dev libsqlite3-dev

Install Nginx

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:nginx/stable
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 00A6F0A3C300EE8C
sudo apt-get update
sudo apt-get install nginx

Install Mongodb

Append the below line to the end of the file /etc/apt/sources.list

deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

Add GPG key

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10

Update package

sudo apt-get update

Install Mongodb

sudo apt-get install mongodb-10gen

Install rbenv, Ruby, Bundler, Rails

Check out rbenv to /home/username/.rbenv.

git clone git://github.com/sstephenson/rbenv.git .rbenv

Add ~/.rbenv/bin to your $PATH for access to the rbenv command-line
.

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc

Add rbenv init to your shell to enable shims and autocompletion.

echo 'eval "$(rbenv init -)"' >> ~/.bashrc

Make your shell know the changes.

source ~/.bashrc

Check out ruby-build to .rbenv.

mkdir -p ~/.rbenv/plugins
cd ~/.rbenv/plugins
git clone git://github.com/sstephenson/ruby-build.git

Install Ruby.

rbenv install 1.9.3-p194

Rebuild the shim binaries once you installed a new Ruby or gem library.

rbenv rehash

Set a global Ruby version for all shells.

rbenv global 1.9.3-p194

Install bundler

gem install bundler --no-rdoc --no-ri; rbenv rehash

Install Rails

gem install rails --no-rdoc --no-ri; rbenv rehash

Create the blog application

On the home directory /home/username/, type:

rails new blog --skip-active-record

In order to use Mongodb.

Change the Gemfile

gem 'rails', '3.2.8'
gem 'therubyracer' 
gem 'mongoid', '~>3.0.3'
gem 'unicorn'

then update all the gems:

bundle install

Create configuration files for nginx and unicorn

Go the ~/blog/config directory:

touch unicorn.rb nginx.conf

We have installed all the stuff needed, now it's time to get them to work together. The progress of managing them is pain, you may meet some errors waiting for you to fix, but all will be done and you could learn lots from this trip.

Check Nginx

Lauch Nginx to check if it was installed correctly:

sudo service nginx start

Then start your browser, type your server IP to the address bar, enter. If you can see the Ngnix default page, Welcome to Nginx!, it proves that your Nginx works well.

Note: you should modify the lines in the file /etc/nginx/site-enabled/default from:

root /usr/share/nginx/www 
server_name localhost

To:

root /usr/share/nginx/html
#server_name localhost    

Because the Nginx default index page is on the directory /usr/share/nginx/html.

At this point, we should setup Nginx to load our blog app, do this:

cd /etc/nginx/site-enabled/
sudo rm default
sudo ln -s ~/blog/config/nginx.conf blog
sudo service nginx reload

Connect to your server again, you will see the rails welcome page, but you can't access the dynamic content.

Check Unicorn

Go to the place which the blog app located on, and create a couple of files in terms of config/unicorn.rb

cd ~/blog
mkdir -p unicorn
cd unicorn
touch err.log out.log 
cd ~/blog/tmp; mkdir -p pids
cd pids; touch unicorn.pid

You must build above files manually, if not, unicorn doesn't create them automatically, and it will not work. After that, start unicorn:

cd ~/blog
bundle exec unicorn -c config/unicorn.rb -E development -D -p 8080

Note: in my case, I need to specify the port number, otherwise it can't work normally.

The method to stop a unicorn process:

kill -9 `cat ~/blog/tmp/pids/unicorn.pid`

Now reload your server page, the rails icon occurs, and the dynamic data can be seen.

Start Mongodb

The Mongodb's server daemon is mongod, so type mongod to start it. But you may the error information prompting you to create a directory to store the data from this process. So:

sudo  mkdir -p /data/db 
chown username:usergroup /data/db

Start Mongodb,

mongod &>/dev/null &

Use Mongoid with Rails

cd ~/blog
rails g mongoid:config
rails g scaffold article title date content 

At the time, you should delete the file ~/blog/public/index.html, and change the line in ~/blog/config/route.rb from root :to => 'welocme#index' to:

root :to => 'posts#index'

Load the page serverIP:8080 to check the result. It's done.

# This file should be placed on the directory of ~/blog/config
working_directory "/home/username/blog"
pid "/home/username/blog/tmp/pids/unicorn.pid"
stderr_path "/home/username/blog/unicorn/err.log"
stdout_path "/home/username/blog/unicorn/out.log"
listen "/tmp/unicorn.todo.socket"
worker_processes 2
timeout 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment