Skip to content

Instantly share code, notes, and snippets.

@scottlowe
Created September 1, 2010 22:33
Show Gist options
  • Save scottlowe/561493 to your computer and use it in GitHub Desktop.
Save scottlowe/561493 to your computer and use it in GitHub Desktop.
Part of a recipe for fronting Glassfish with Nginx
# ~/.bashrc: executed by bash(1) for non-login shells.
# If not running interactively, don't do anything
#[ -z "$PS1" ] && return
if [[ -n "$PS1" ]] ; then
# don't put duplicate lines in the history. See bash(1) for more options
#export HISTCONTROL=ignoredups
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" -a -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# A color and a non-color prompt:
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w \$ '
# Comment in the above and uncomment this below for a color prompt
#PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \$ '
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ "$TERM" != "dumb" ]; then
export LS_OPTIONS='--color=auto'
eval `dircolors -b`
fi
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
alias l='ls $LS_OPTIONS -lA'
# Some more alias to avoid making mistakes:
#alias rm='rm -i'
#alias cp='cp -i'
#alias mv='mv -i'
umask 022
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# don't put duplicate lines in the history. See bash(1) for more options
#export HISTCONTROL=ignoredups
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" -a -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color)
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
;;
*)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
;;
esac
# Comment in the above and uncomment this below for a color prompt
#PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'
;;
*)
;;
esac
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
#if [ -f ~/.bash_aliases ]; then
# . ~/.bash_aliases
#fi
# enable color support of ls and also add handy aliases
if [ "$TERM" != "dumb" ]; then
eval "`dircolors -b`"
alias ls='ls --color=auto'
#alias dir='ls --color=auto --format=vertical'
#alias vdir='ls --color=auto --format=long'
fi
# some more ls aliases
#alias ll='ls -l'
#alias la='ls -A'
#alias l='ls -CF'
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
#if [ -f /etc/bash_completion ]; then
# . /etc/bash_completion
#fi
fi
if groups | grep -q rvm ; then
source "/usr/local/lib/rvm"
fi
# Ensure RVM gems are loaded
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
# Load RVM's capistrano plugin.
require "rvm/capistrano"
# Set it to the ruby + gemset of your app, e.g 'jruby-1.5.2':
set :rvm_ruby_string, 'default'
set :use_sudo, false
set :scm, :git
#tell git to clone only the latest revision and not the whole repository
set :git_shallow_clone, 1
set :keep_releases, 3
set :application, "my_website"
set :domain, "example-app.com"
set :user, "my_website"
set :password, "my_password"
set :deploy_to, "/home/my_website/sites/#{application}"
set :runner, "my_website"
set :repository, "git@github.com:my_account_name/my_website.git"
set :branch, "master"
set :deploy_via, :remote_cache
# Bundler
require 'bundler/capistrano'
set :bundle_flags, "--deployment"
set :bundle_cmd, 'ruby -S bundle'
# Options necessary to make Ubuntu’s SSH happy
ssh_options[:paranoid] = false
default_run_options[:pty] = true
role :app, "www.my_website_hostname.com" # Your Application server, Glassfish
role :web, "www.my_website_hostname.com" # Your HTTP server, Nginx
role :db, "www.my_website_hostname.com", :primary => true # This is where Rails migrations will run
namespace :deploy do
desc "Start Glassfish Gem from a shutdown state"
task :cold do
start
end
desc "Stop a server running GlassFish gem"
task :stop do
run "/etc/init.d/gfish-my_website stop"
end
desc "Starts a server running GlassFish gem"
task :start do
run "/etc/init.d/gfish-my_website start"
end
desc "Restarts a server running GlassFish gem"
task :restart do
run "/etc/init.d/gfish-my_website restart"
end
end
platforms :jruby do
gem 'jruby-openssl'
gem 'activerecord-jdbc-adapter', '>= 1.0.0.beta2'
gem 'activerecord-jdbcpostgresql-adapter', '>= 1.0.0.beta2'
end
platforms :ruby do
gem 'pg'
end
#! /bin/sh
### BEGIN INIT INFO
# Provides: glassfish-mywebsite
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the mywebsite app server
# Description: starts mywebsite app server
### END INIT INFO
set -u
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# The following variables can be edited for your app:
NAME=glassfish-mywebsite
APP_ROOT=/home/my_user/sites/mywebsite/current
PIDFILE="$APP_ROOT/tmp/pids/glassfish.pid"
DAEMON="ruby -S glassfish"
DAEMON_ARGS="-P $PIDFILE $APP_ROOT"
RUN_AS_USER="mywebsite"
. /lib/lsb/init-functions
cd $APP_ROOT || exit 1
export RAILS_ENV=production
sig () {
test -s "$PIDFILE" && kill -$1 `cat $PIDFILE`
}
# Clean up any threads that won't die
hard_kill_cleanup () {
test -s "$PIDFILE" && kill -9 -$1 `cat $PIDFILE`
}
start () {
log_daemon_msg "Starting $NAME: "
sig 0 && echo >&2 "$NAME is already running" && exit 0
if [ "x$USER" != "x$RUN_AS_USER" ]; then
su - $RUN_AS_USER -c "$DAEMON $DAEMON_ARGS"
else
$DAEMON $DAEMON_ARGS
fi
log_daemon_msg "$NAME has started"
}
stop () {
log_daemon_msg "Stopping $NAME: "
sig INT && exit 0
echo >&2 "$NAME is stopped"
}
reload () {
log_daemon_msg "Stopping $NAME: "
sig INT
sleep 8
hard_kill_cleanup
sleep 2
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
reload
;;
*)
echo "Usage: $NAME {start|stop|restart}" >&2
exit 1
;;
esac
exit 0
server {
listen 80 default;
server_name www.mywebsite.com;
root /home/my_user/sites/mywebsite.com/current/public;
location ~* \.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires max;
}
location / {
access_log off;
proxy_pass http://localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
@scottlowe
Copy link
Author

This file 'mywebsite.com' is an Nginx conf file to go in /etc/nginx/sites-available/mywebsite.com on Ubuntu. This will serve static assets from the public directory with maximum expiry headers so that they are not requested again. Nginx will pass other requests on to your application server listening at http://localhost:3000

The file 'gfish-mywebsite' should be located at /etc/init.d/mywebsite.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment