Skip to content

Instantly share code, notes, and snippets.

@soffes
Created February 25, 2010 18:33
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save soffes/314865 to your computer and use it in GitHub Desktop.
Save soffes/314865 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# new_server.sh
# Author: Sam Soffes
# Created: 02/25/2010
# Updated: 11/04/2010
#
# See my blog post on this script: http://samsoff.es/posts/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 07/13/2010. The lastest patch level
# for Ruby 1.8.7 is used. This is due to some applications having
# incompatibilities with Ruby 1.9.1.
#
# 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/314883#file_example.conf
#
# To run this script, simply run the following command as root:
# $ wget http://gist.github.com/raw/314865/new_server.sh;chmod +x new_server.sh;./new_server.sh
#
# 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 -y update
yum -y install gcc gcc-c++ 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
# Git
wget http://kernel.org/pub/software/scm/git/git-1.7.3.2.tar.gz -O- | tar xz
cd git-1.7.3.2
./configure --prefix=$PREFIX
make
make install
cd ..
rm -rf git-1.7.3.2
# Ruby
wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p302.tar.gz -O- | tar xz
cd ruby-1.8.7-p302
./configure --prefix=$PREFIX
make
make install
cd ..
rm -rf ruby-1.8.7-p302
# RubyGems
wget http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz -O- | tar xz
cd rubygems-1.3.7
ruby setup.rb
cd ..
rm -rf rubygems-1.3.7
# Nginx with SSL and Passenger
gem install passenger --version=3.0.0 --no-ri --no-rdoc
wget http://nginx.org/download/nginx-0.8.53.tar.gz -O- | tar xz
cd nginx-0.8.53
./configure --with-http_ssl_module --add-module=/usr/local/lib/ruby/gems/1.8/gems/passenger-3.0.0/ext/nginx
make
make install
cd ..
rm -rf nginx-0.8.53
# Configure Nginx
wget -d http://gist.github.com/raw/143136/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 http://gist.github.com/raw/314883/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/v9.0.1/postgresql-9.0.1.tar.gz -O- | tar xz
cd postgresql-9.0.1
./configure --prefix=$PREFIX/pgsql --with-openssl
gmake
gmake install
cd contrib
gmake all
gmake install
# Configure PostgreSQL
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
cd ..
rm -rf postgresql-9.0.1
# ImageMagick
wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick-6.6.5-5.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
cd ImageMagick-6.6.5-5
./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
make install
cd ..
rm -rf ImageMagick-6.6.5-5 fonts
# Bundler
gem install bundler --no-ri --no-rdoc
# 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
#!/bin/sh
#
# z_after.sh
# Author: Sam Soffes
# Created: 02/25/2010
# Updated: 11/04/2010
#
# This script is more specific to my needs. I run it after new_server.sh.
# To get the pg gem to install, I generally have to restart my shell so
# the path to pg_config works.
#
useradd deploy -g nginx
mkdir -p /var/www
chown -R deploy /var/www
chgrp -R nginx /var/www
chmod -R 775 /var/www
gem install pg --no-ri --no-rdoc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment