Skip to content

Instantly share code, notes, and snippets.

@bjjb
Created November 21, 2013 05:00
Show Gist options
  • Select an option

  • Save bjjb/7576318 to your computer and use it in GitHub Desktop.

Select an option

Save bjjb/7576318 to your computer and use it in GitHub Desktop.
A bash script for quickly setting up NginX, PostreSQL, ruby, thin, and an app user on a Debian-like system
#!/bin/bash
# Provisioning script for an Ubuntu server to run a PostgreSQL server, NginX,
# and a Rack application behind a thin server, which can be deployed with
# Capistrano.
#
# This script needs to be run as root
#
# When it's run, you will still need to add your deploy users to the group, so
# that they can actually act on behalf of the named application user. And,
# obviously, they will need an account into which they can SSH, so you'll need
# their public key.
NAME=boobs
DATABASE=YES
WEB=YES
APP=YES
RUBY_VERSION=2.0
apt-get update -y
if [ "$WEB" = "YES" ] ; then
apt-get -y install nginx
ln -fs /var/www/$NAME/current/config/nginx.conf /etc/nginx/sites-available/$NAME
ln -fs /etc/nginx/sites-available/$NAME /etc/nginx/sites-enabled/$NAME
fi
if [ "$DATABASE" = "YES" ] ; then
apt-get -y install postgresql postgresql-contrib
sudo -u postgres createuser -dSR $NAME
fi
if [ "$APP" = "YES" ] ; then
apt-get -y install git build-essential
cd /tmp && rm -rf ruby-install
git clone git://github.com/postmodern/ruby-install && cd ruby-install && make install
ruby-install --no-reinstall ruby $RUBY_VERSION
cd /tmp && rm -rf chruby
git clone git://github.com/postmodern/chruby && cd chruby && make install
d=/usr/local/share/chruby
f=/etc/profile.d/chruby.sh
echo "[ \"$BASH\" ] && [ -f $d/chruby.sh ] && . $d/chruby.sh" > $f
echo "[ \"$BASH\" ] && [ -f $d/auto.sh ] && . $d/auto.sh" >> $f
. $f
chruby $RUBY_VERSION
gem install thin --conservative
thin install -f
update-rc.d -f thin defaults
ln -sf /var/www/$NAME/current/config/thin.yml /etc/thin/$NAME
useradd -rmU -s /bin/bash -b /var/www -c "$NAME app service runner" $NAME
chmod 2775 /var/www/$NAME
mkdir -p -m 700 /var/www/$NAME/.ssh
f=/etc/sudoers.d/$NAME
echo "Cmnd_Alias START = /etc/init.d/thin start -C /etc/thin/$NAME" > $f
echo "Cmnd_Alias STOP = /etc/init.d/thin stop -C /etc/thin/$NAME" >> $f
echo "Cmnd_Alias RESTART = /etc/init.d/thin restart -C /etc/thin/$NAME" >> $f
echo "%$NAME ALL=(ALL) NOPASSWD: START,STOP,RESTART" >> $f
chmod 0440 $f
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment