# server.sh
# Author: Marius Voila
# Edited by: marc
# Created: 06/25/2010
# Updated: 19/03/2011
# See my blog post on this script: http://mariusv.com/rackspace-cloud-new-server-script
# This script installs everything needed to run a Ruby on Rails application on
# Nginx (with Passenger) and PostgreSQL on CentOS 5.4 and 5.5. init.d scripts
# are installed for Ngnix and PostgreSQL as well. Git and Bundler are also
# installed as many applications use Bundler for gem dependencies and git is
# often used in Gemfiles. ImageMagick is also installed. Port 80 and 443 are
# also opened in IPTables.
# The install takes about 20 minutes on average.
# All of the versions are the latest as of 19/03/2011.
# To easily add a new virtual host, add a file with `.conf` as its extension
# to `/usr/local/nginx/conf/virtual_hosts/`.
# Example virtual host: http://gist.github.com/478237#file_example.conf
# To run this script, simply run the following command as root:
# $ wget https://gist.github.com/raw/877545/server.sh;chmod +x server.sh;./server.sh
# You will need to press "y" and Enter twice when yum prompts you to install
# updates and necessary packages at the beginning. You can leave it and let it
# do its thing after that.

# General
set -e
# Note: changing prefix doesn't quite work due to my lack of shell scripting
# knowledge, so don't change it :)
export PREFIX=/usr/local
cd ~

# Install necessary dependencies all at once
yum update
yum install gcc gcc-c++ patch make zlib-devel pcre-devel.x86_64 openssl-devel.x86_64 readline-devel libpng-devel libjpeg-devel libtiff-devel libwmf-devel lcms-devel freetype-devel ghostscript-devel curl-devel libxslt-devel

# set required package versions

# Git
wget http://kernel.org/pub/software/scm/git/git-$GIT_VERSION.tar.gz -O- | tar xz
./configure --prefix=$PREFIX
make install
cd ..

# --------------------------------------------
# setup .gemrc for use with RVM (Ruby Version Manager)
echo -e '---
:verbose: true 
:bulk_threshold: 1000 
- http://rubygems.org
- http://gems.github.com
gem: --no-ri --no-rdoc
:benchmark: false
:update_sources: true 
:backtrace: false' > ~/.gemrc

# install RVM
bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
echo -e '
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"  # This loads RVM into a shell session.' >> ~/.bash_profile
source ~/.bash_profile

# update RVM
rvm get head
rvm reload
## --------------------------------------------

# Install required gems
rvm install $RUBY_VERSION # install Ruby
rvm --default use $RUBY_VERSION@global
gem install awesome_print map_by_method wirble builder bundler pg cheat
gem install -v2.1.2 builder
rvm --default --create use $RUBY_VERSION@rails3 # install Rails 3, and set this ruby+gemset as the RVM default
gem install rails

# Nginx with SSL and Passenger
gem install passenger
rvmsudo passenger-install-nginx-module # Install nginx with Passenger support. Press 1 when prompted

wget -d https://gist.github.com/raw/478229/nginx
mv nginx /etc/init.d/nginx
chmod +x /etc/init.d/nginx
chkconfig --add nginx
groupadd nginx
useradd nginx -g nginx
rm -f $PREFIX/nginx/conf/nginx.conf
wget -d https://gist.github.com/raw/877575/nginx.conf
mv nginx.conf $PREFIX/nginx/conf/nginx.conf
mkdir $PREFIX/nginx/conf/virtual_hosts

# PostgreSQL
wget http://wwwmaster.postgresql.org/redir/198/h/source/v$POSTGRES_VERSION/postgresql-$POSTGRES_VERSION.tar.gz -O- | tar xz
cd postgresql-$POSTGRES_VERSION
./configure --prefix=$PREFIX/pgsql --with-openssl
gmake install
cd contrib
gmake all
gmake install
cd ..
cp contrib/start-scripts/linux /etc/init.d/postgresql
chmod +x /etc/init.d/postgresql
chkconfig --add postgresql
cd ..
adduser postgres
echo 'PATH=$PATH:/usr/local/pgsql/bin' > /etc/profile.d/postgresql.sh
echo 'export PATH;' >> /etc/profile.d/postgresql.sh
chmod +x /etc/profile.d/postgresql.sh
mkdir -p /var/log/pgsql
touch /var/log/pgsql/pgsql.log
chown -R postgres:postgres /var/log/pgsql/
mkdir -p $PREFIX/pgsql/data
chown -R postgres:postgres $PREFIX/pgsql/data
sudo -u postgres $PREFIX/pgsql/bin/initdb -U postgres -E=UTF8 $PREFIX/pgsql/data

# ImageMagick
wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick-$IMAGEMAGICK_VERSION.tar.gz -O- | tar xz
wget http://sourceforge.net/projects/gs-fonts/files/gs-fonts/8.11%20%28base%2035%2C%20GPL%29/ghostscript-fonts-std-8.11.tar.gz/download -O- | tar xz
mv -f fonts $PREFIX/share/ghostscript
./configure --prefix=$PREFIX --disable-static --with-modules --without-perl --without-magick-plus-plus --with-quantum-depth=8 --with-gs-font-dir=$PREFIX/share/ghostscript/fonts
make install
cd ..

# Clean up

# Open ports
/sbin/iptables -I INPUT -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
/sbin/iptables -I OUTPUT -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
/sbin/iptables -I OUTPUT -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
service iptables save

# Start everything up
service postgresql start
service nginx start