Skip to content

Instantly share code, notes, and snippets.

@happypeter
Forked from billie66/nginx.conf
Created September 12, 2012 07:33
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save happypeter/3704968 to your computer and use it in GitHub Desktop.
Save happypeter/3704968 to your computer and use it in GitHub Desktop.
Deploy Rails on Ubuntu server 12.04 with Nginx, Unicorn, Mysql
upstream unicorn {
server unix:/tmp/unicorn.happycasts.sock fail_timeout=0;
}
server {
listen 80 default deferred;
# server_name example.com;
root /home/peter/happycasts/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
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;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}

Packages

  • Ubuntu server 12.04

  • Nginx 1.2.3

  • Unicorn v4.3.1

  • Rails 3.2.8

  • Ruby 1.9.3-p194

Install dependent libraries and tool, if you don't rbenv will give you broken ruby&gem

For all

sudo apt-get install build-essential git-core

For GEM and bundler

sudo apt-get install zlib1g-dev

For rbenv

sudo apt-get install libreadline-gplv2-dev
sudo apt-get install curl 

For test or some gems (like nokogiri ...)

sudo apt-get install libssl-dev libsqlite3-dev libxml2-dev libxslt-dev
sudo apt-get install g++ # GEM:eventmachine
sudo apt-get install mysql-server libmysqlclient-dev# GEM:mysql2

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 mysql

sudo apt-get install mysql-server

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 -d mysql

Change the Gemfile

gem 'rails', '3.2.8'
gem 'therubyracer' 
gem 'mysql2'
gem 'unicorn'

then update all the gems:

bundle update

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.

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 line in the file /etc/nginx/site-enabled/default from:

root /usr/share/nginx/www 

To:

root /usr/share/nginx/html

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 

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.

  1. the default index.html does not work well in prodution mode, so do not borther to config that, just delete it and go on.

  2. To put things in production mode, do

     rails g scaffold items name:string
     rake db:migrate RAILS_ENV=production
     rm public/index.html
     #change route.rb point root to items#index
     rake assets:precompile
     sudo service nginx restart
     ./config/unicorn_init.sh stop
     ./config/unicorn_init.sh start
    

    now everything works!!!

  3. Never forget to do assets:precompile, and make sure you will check all log files when errors happen

root = "/home/deployer/happycasts/"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"
listen "/tmp/unicorn.happycasts.sock"
worker_processes 2
timeout 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment