Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save connor-t/6906902 to your computer and use it in GitHub Desktop.
Save connor-t/6906902 to your computer and use it in GitHub Desktop.

Uber Ruby 1.9.3, Phusion Passenger and Nginx one line installer for Ubuntu 12.04

This script will install the entire web app stack to run Rails apps. This was written for and tested with Ubuntu 12.04 LTS on Amazon EC2.

Once you start up an EC2 instance, log in and run this one command:

sudo apt-get install -y git && git clone git://gist.github.com/6906902.git gist-6906902 && bash ./gist-6906902/install_ruby_passenger_nginx.sh

It will run through an auto install process.

What this will install

  • Ruby 1.9.3 (p286) from source with bundler
  • Nginx (including init.d and logrotate tasks)
  • Phusion Passenger
  • ImageMagick (which we use all the time for things like Paperclip)

Starting/Stopping Nginx

sudo service nginx start
sudo service nginx stop
sudo service nginx restart

Credits

The base of this came from Chris Oliver.

The Nginx start/stop init.d script came from Linode.

The Nginx ubuntu log rotation config came from Mell Zamora

#!/bin/bash
# PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Store the base dir
BASEDIR=$( cd $(dirname $0); pwd)
# Get all dependencies
sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get -y install build-essential zlib1g-dev libssl-dev libreadline-dev libyaml-dev libcurl4-openssl-dev curl git-core python-software-properties libsqlite3-dev
# ImageMagick (used by Paperclip gem)
sudo apt-get -y install zip unzip imagemagick
sudo apt-get -y install fail2ban
sudo adduser deployer
sudo adduser deployer sudo
mkdir /home/deployer
mkdir /home/deployer/.ssh
chmod 700 /home/deployer/.ssh
cat .ssh/authorized_keys > /home/deployer/.ssh/authorized_keys
chmod 400 /home/deployer/.ssh/authorized_keys
chown deployer:deployer /home/deployer -R
su deployer
# Postgresql 9.2
# sudo add-apt-repository ppa:pitti/postgresql
# sudo apt-get update
sudo apt-get -y install postgresql-9.2 libpq-dev
passwd postgres
su -l postgres
nano /etc/postgresql/9.0/main/pg_hba.conf
/etc/init.d/postgresql restart
# Install Ruby
#mkdir ~/src
#cd ~/src
#wget http://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz
#tar -zxf ruby-2.0.0-p247.tar.gz
#cd ruby-2.0.0-p247
#./configure
#make
#sudo make install
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
rvm install 2.0.0
rvm use 2.0.0 --default
echo "gem: --no-ri --no-rdoc" >> ~/.gemrc
sudo gem install bundler
# Install Passenger - which will install Nginx
sudo gem install passenger
sudo passenger-install-nginx-module --auto --prefix=/opt/nginx --auto-download
# Install the control nginx control script
sudo cp $BASEDIR/nginx.initd /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d -f nginx defaults
# Add log rotation to nginx
sudo cp $BASEDIR/nginx.logrotate /etc/logrotate.d/nginx
# Use service to start nginx
sudo service nginx start
### 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:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/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
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /opt/nginx/logs/$NAME.pid \
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$NAME.pid \
--exec $DAEMON
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/opt/nginx/logs/$NAME.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile \
/opt/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /opt/nginx/logs/$NAME.pid \
--exec $DAEMON
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
exit 1
;;
esac
exit 0
/opt/nginx/logs/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
[ ! -f /opt/nginx/logs/nginx.pid ] || kill -USR1 `cat /opt/nginx/logs/nginx.pid`
endscript
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment