Rails 5 deployment with nginx-unicorn-postgress-rbenv
In this blog i’m sharing the steps I observed to deploy the rails with nginx-unicorn. Initially I faced some issues with RVM so tried with rbenv. You can try with RVM as well.
The steps here is simple and collected it from blogs to keep it for my future reference. Same i’m publish here thinking it will kelp others.
rbenv
If rbenv is installed skip this step.
sudo apt-get update
sudo apt-get install rbenv ruby-build
Postgress
postgresql-with-your-ruby-on-rails-application-on-ubuntu-14–04
Standard steps
$ bundle install$ rake assets:precompile RAILS_ENV=production$ rake db:migrate RAILS_ENV=production
unicorn
$ vi Gemfile
add gem 'unicorn'
$ bundle
Create a file inside config/unicorn.rb and copy past the code below
# set path to application
app_dir = File.expand_path(“../..”, __FILE__)
shared_dir = “#{app_dir}/shared”
working_directory app_dir# Set unicorn options
worker_processes 2
preload_app true
timeout 30# Set up socket location
listen “#{shared_dir}/sockets/unicorn.sock”, :backlog => 64# Logging
stderr_path “#{shared_dir}/log/unicorn.stderr.log”
stdout_path “#{shared_dir}/log/unicorn.stdout.log”# Set master PID location
pid “#{shared_dir}/pids/unicorn.pid”
As per the above lines create the directories
$ mkdir -p shared/pids shared/sockets shared/log
create Unicorn Init Script with app name
$ sudo vi /etc/init.d/unicorn_appname
field with BOLD need to be changed
#!/bin/sh### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the unicorn app server
# Description: starts unicorn using start-stop-daemon
### END INIT INFOset -eUSAGE="Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"# app settings
USER="deploy"
APP_NAME="appname"
APP_ROOT="/home/$USER/$APP_NAME"
ENV="production"# environment settings
PATH="/home/$USER/.rbenv/shims:/home/$USER/.rbenv/bin:$PATH"
CMD="cd $APP_ROOT && bundle exec unicorn -c config/unicorn.rb -E $ENV -D"
PID="$APP_ROOT/shared/pids/unicorn.pid"
OLD_PID="$PID.oldbin"# make sure the app exists
cd $APP_ROOT || exit 1sig () {
test -s "$PID" && kill -$1 `cat $PID`
}oldsig () {
test -s $OLD_PID && kill -$1 `cat $OLD_PID`
}case $1 in
start)
sig 0 && echo >&2 "Already running" && exit 0
echo "Starting $APP_NAME"
su - $USER -c "$CMD"
;;
stop)
echo "Stopping $APP_NAME"
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
echo "Force stopping $APP_NAME"
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload|upgrade)
sig USR2 && echo "reloaded $APP_NAME" && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
$CMD
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 $USAGE
exit 1
;;
esac
- sudo chmod 755 /etc/init.d/unicorn_appname
- sudo update-rc.d unicorn_appname defaults
$ sudo service unicorn_appname start $ sudo service unicorn_appname stop
Nginx
Install Nginx using apt-get:
$ sudo apt-get install nginx
Now open the default server block with a text editor:
$ sudo vi /etc/nginx/sites-available/default
Change to respective server name example : spritle.com
and root to /home/deploy/appname/public;
upstream app {
# Path to Unicorn SOCK file, as defined previously
server unix:/home/deploy/appname/shared/sockets/unicorn.sock fail_timeout=0;
}server {
listen 80;
server_name localhost;root /home/deploy/appname/public;try_files $uri/index.html $uri @app;location @app {
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Once above file is saved
$ sudo service nginx restart$ sudo service nginx stop$ sudo service nginx start
Log
nginx
$ sudo tail -f /var/log/nginx/error.log
Unicorn
$ tail -f shared/log/unicorn.stderr.log$ tail -f shared/log/unicorn.stdout.log
Yes Done!