Skip to content

Instantly share code, notes, and snippets.

@anderssvendal
Created October 29, 2012 10:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anderssvendal/3972779 to your computer and use it in GitHub Desktop.
Save anderssvendal/3972779 to your computer and use it in GitHub Desktop.
ubuntu + unicorn + sinatra

Based on:

Install packages

Make add-apt-repository work:

$ sudo apt-get install python-software-properties

Add nginx repository:

$ sudo add-apt-repository ppa:nginx/stable
$ sudo apt-get update

Install packages:

$ sudo apt-get -y install nginx git-core build-essential
$ sudo apt-get -y install zlib1g-dev
$ sudo apt-get -y install libssl-dev libsqlite3-dev
$ sudo apt-get -y install curl

Create app user

$ sudo adduser --shell /bin/bash app

Install rbenv, ruby-build and ruby for user

Switch user:

$ su - app

Install rbenv:

$ git clone git://github.com/sstephenson/rbenv.git .rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile

Install ruby-build

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

Install ruby

$ rbenv install 1.9.3-p194
$ rbenv global 1.9.3-p194
$ rbenv rehash

Checkout & setup app

As app user $ git clone ~/app

Install bundle/gems

Create folders for app

$ mkdir -p ~/app/tmp/{sockets,pids} $ mkdir ~/app/log

Setup unicorn & nginx

Create unicorn.rb in ~/app if needed.

Create /etc/init.d/unicorn[-APP_NAME] as sudo $ sudo chmod +x /etc/init.d/unicorn[-APP_NAME]

Edit nginx config

Start services

$ sudo service unicorn start
$ sudo service nginx start
$ sudo update-rc.d unicorn defaults
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the unicorn web server
# Description: starts unicorn
### END INIT INFO
USER=app
PATH=/home/$USER/.rbenv/bin:/home/$USER/.rbenv/shims:$PATH
DAEMON=unicorn
DAEMON_OPTS="-c /home/$USER/app/unicorn.rb -E production -D"
NAME=unicorn
DESC="Unicorn app for $USER"
PID=/home/$USER/app/tmp/pids/unicorn.pid
case "$1" in
start)
CD_TO_APP_DIR="cd /home/$USER/app"
START_DAEMON_PROCESS="$DAEMON $DAEMON_OPTS"
echo -n "Starting $DESC: "
if [ `whoami` = root ]; then
su - $USER -c "$CD_TO_APP_DIR > /dev/null 2>&1 && $START_DAEMON_PROCESS"
else
$CD_TO_APP_DIR > /dev/null 2>&1 && $START_DAEMON_PROCESS
fi
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
kill -QUIT `cat $PID`
echo "$NAME."
;;
restart)
echo -n "Restarting $DESC: "
kill -USR2 `cat $PID`
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
kill -HUP `cat $PID`
echo "$NAME."
;;
*)
echo "Usage: $NAME {start|stop|restart|reload}" >&2
exit 1
;;
esac
exit 0
upstream workers {
server unix:/home/app/app/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name _;
root /home/app/app/public;
location / {
access_log off;
include proxy_params;
proxy_redirect off;
if (-f $request_filename) {
access_log off;
expires max;
break;
}
if (!-f $request_filename) {
proxy_pass http://workers;
break;
}
}
}
@dir = '/home/app/app/'
worker_processes 8
working_directory @dir
listen "#{@dir}tmp/sockets/unicorn.sock", backlog: 64
pid "#{@dir}tmp/pids/unicorn.pid"
stderr_path "#{@dir}log/unicorn.stderr.log"
stdout_path "#{@dir}log/unicorn.stdout.log"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment