Friday, September 20, 2013

Ruby on Rails + Slim & Nginx + Unicorn

The first, you must install Ruby and Rails for your computer.
Deploy website on Nginx & Unicorn

Install Nginx
$ sudo apt-get install nginx
Setup virtual host for Nginx. Open file /etc/nginx/sites-available/ and add below lines:
server {
        listen 80;
        root /document_root;
        index index.php;
        #access_log  /var/nginx/access.log

Install Unicorn
$ gem install unicorn
Setup Unicorn to process request for Ruby Application on Nginx. Continue add more below lines into setup files of nginx
upstream unicorn {
  server unix:/tmp/unicorn.hello.sock fail_timeout=0;

server {
  listen 80 default deferred;
  root /home/taind/svn/demo/hello;

  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;
Add a boot loader file file Unicorn in document_root/config/unicorn.rb
root = "/home/taind/svn/demo/hello"
working_directory root
pid "#{root}/tmp/pids/"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

listen "/tmp/unicorn.hello.sock"
worker_processes 2
timeout 30

# Force the bundler gemfile environment variable to
# reference the capistrano "current" symlink
before_exec do |_|
  ENV["BUNDLE_GEMFILE"] = File.join(root, 'Gemfile')

After finish all above steps, you just start Nginx and Unicorn
Start Nginx
$ service nginx start
Start Unicorn
unicorn_rails -c document_root/config/unicorn.rb -D

Setup Slim template
How to start?
Install Slim as a gem:
$ gem install slim

Include Slim in your Gemfile with gem 'slim' or require it with require 'slim'. That's it! Now, just use the .slim extension and you're good to go.

No comments :

Post a Comment