Press enter to see results or esc to cancel.

Basic Setup Rails Server on EC2 Ubuntu 12.04 with Passenger, Unicorn and Nginx

Setup Rails Server on EC2

Setup Rails Server on EC2

install the package needed for installation

$ sudo apt-get install build-essential git-core

$ sudo apt-get install curl git nodejs

$ curl -L https://get.rvm.io | 
  bash -s stable --ruby --autolibs=enable --auto-dotfiles

add line

source ~/.profile

in

~/.bash_profile

then start using rvm

$ source /home/ubuntu/.rvm/scripts/rvm

installing ruby 1.9.3

$ rvm install 1.9.3

$ rvm use ruby-1.9.3-p448 --default

create gemset

rvm gemset create rails-3.2.11

rvm use [email protected] --default

install rails

$ gem install rails -v 3.2.11

Finish installed rails, now the Passenger

$ gem install passenger
$ rvmsudo passenger-install-nginx-module

to start nginx, you can type this

$ sudo /opt/nginx/sbin/nginx

to stop

$ sudo /opt/nginx/sbin/nginx -s stop

now edit /opt/nginx/conf/nginx.conf

server {
    listen 80;
    server_name domain.com;
    charset utf-8;
    root /www/firstapp/public;
    passenger_enabled on;
    rails_spawn_method smart;
    rails_env production;
}

configuring nginx

sudo vim /etc/init.d/nginx

copy paste this


#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/opt/nginx/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/nginx/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
    . /etc/default/nginx
fi

set -e

. /lib/lsb/init-functions

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --quiet --pidfile /opt/nginx/logs/$NAME.pid 
        --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$NAME.pid 
        --exec $DAEMON || true
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --quiet --pidfile 
        /opt/nginx/logs/$NAME.pid --exec $DAEMON || true
    sleep 1
    start-stop-daemon --start --quiet --pidfile 
        /opt/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile /opt/nginx/logs/$NAME.pid 
          --exec $DAEMON || true
      echo "$NAME."
      ;;
  status)
      status_of_proc -p /opt/nginx/logs/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
      ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
    exit 1
    ;;
esac

exit 0

after that run this to make the file executeable

sudo chmod +x /etc/init.d/nginx

make this default when we restart the system

sudo /usr/sbin/update-rc.d -f nginx defaults

some built in function of passenger

$ rvmsudo passenger-status
$ rvmsudo passenger-memory-stats

using Unicorn

put this in Gemfile

gem 'unicorn'

$ bundle install

then you can run by run this command

$ unicorn_rails

put some script for unicorn configuration

$ touch config/unicorn.rb

and insert this script

worker_processes 2
timeout 30

at /opt/nginx/conf/nginx.conf change the server conf to this

upstream unicorn {
  server unix:/tmp/unicorn.todo.sock fail_timeout=0; 
}

server {
 listen 80 default;
 server_name localhost;
 root /home/ubuntu/www/firstapp/public; 
 try_files $uri/index.html $uri @unicorn;
 location @unicorn {
   proxy_pass https://localhost:8080;
 }
 error_page 500 502 503 504 /500.html;
}

dont forget to delete/comment the passenger thingy there if you install using passenger package

– evnpr

source:

https://techoctave.com/c7/posts/16-how-to-host-a-rails-app-with-phusion-passenger-for-nginx https://railscasts.com/episodes/293-nginx-unicorn?view=asciicast

Comments

Leave a Comment

Show Buttons
Hide Buttons