Skip to content

Instantly share code, notes, and snippets.

@ZackMattor
Created December 23, 2013 06:40
Show Gist options
  • Save ZackMattor/8092608 to your computer and use it in GitHub Desktop.
Save ZackMattor/8092608 to your computer and use it in GitHub Desktop.
Unicorn, nginx, init.d
worker_processes 1;
user nobody nogroup; # for systems with a "nogroup"
pid /tmp/nginx.pid;
error_log /tmp/nginx.error.log;
events {
worker_connections 1024; # increase if you have lots of clients
accept_mutex off; # "on" if nginx worker_processes > 1
}
http {
include mime.types;
server_names_hash_bucket_size 64;
types_hash_max_size 2048;
default_type application/octet-stream;
access_log /tmp/nginx.access.log combined;
sendfile on;
tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
tcp_nodelay off; # on may be better for some Comet/long-poll stuff
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/html text/xml text/css
text/comma-separated-values
text/javascript application/x-javascript
application/atom+xml;
include /etc/nginx/sites-enabled/*;
}
upstream app_server {
server unix:/home/zmattor/apps/Faces-of-Dave/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name facesofdave.org;
#client_max_body_size 4G;
keepalive_timeout 5;
root /home/zmattor/apps/Faces-of-Dave/public;
try_files $uri/index.html $uri.html $uri @app;
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
#this sets expire headers to static assets
location ~* \.(js|css|png|jpg|gif)$ {
if ($query_string ~ "^[0-9]+$") {
expires max;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/ubuntu/websites/appname_staging/current/public;
}
}
#! /bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $all
# Required-Stop: $network $local_fs $syslog
# Default-Start: 2 3 4 5
# Description: starts unicorn
### END INIT INFO
# Change these to match your app:
USER="zmattor"
APP_NAME="Faces-of-Dave"
APP_PATH="/home/zmattor/apps"
ENV="production"
RBENV_RUBY_VERSION="2.0.0p247"
# env
APP_ROOT="$APP_PATH/$APP_NAME"
RBENV_ROOT="/home/$USER/.rbenv"
PATH="$RBENV_ROOT/bin:$RBENV_ROOT/shims:$PATH"
SET_PATH="cd $APP_ROOT "
DAEMON="/home/zmattor/.rbenv/shims/unicorn"
DAEMON_OPTS="-c $APP_ROOT/config/unicorn.rb -E $ENV -D"
CMD="$SET_PATH; $DAEMON $DAEMON_OPTS"
NAME=unicorn
DESC="Unicorn app for $APP_NAME"
PID="$APP_ROOT/tmp/pids/unicorn.pid"
OLD_PID="$PID.oldbin"
cd $APP_ROOT || exit 1
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $OLD_PID && kill -$1 `cat $OLD_PID`
}
case ${1-help} in
start)
echo $user
sig 0 && echo >&2 "Already running" && exit 0
su - $USER -c "$CMD"
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
su - $USER -c "$CMD"
;;
upgrade)
sig USR2 && exit 0
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
su - $USER -c "$CMD"
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
exit 1
;;
esac
exit 0
# set path to app that will be used to configure unicorn,
# note the trailing slash in this example
@dir = "/home/zmattor/apps/Faces-of-Dave/"
worker_processes 2
working_directory @dir
timeout 30
# Specify path to socket unicorn listens to,
# we will use this in our nginx.conf later
listen "#{@dir}tmp/sockets/unicorn.sock", :backlog => 64
# Set process id path
pid "#{@dir}tmp/pids/unicorn.pid"
# Set log file paths
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