Skip to content

Instantly share code, notes, and snippets.

@unixmonkey
Created May 19, 2010 22:41
Show Gist options
  • Save unixmonkey/406944 to your computer and use it in GitHub Desktop.
Save unixmonkey/406944 to your computer and use it in GitHub Desktop.
build_ubuntu_rails_server.sh
#!/bin/bash
# This bash script is intended to be a solid starting point
# for a Ruby on Rails application server on Ubuntu Linux.
# Edit these settings if you wish
HOSTNAME="railsmachine"
DEPLOYUSER="deploy"
RUBYDIR="/opt/ruby"
RAILSAPPNAME="railsapp"
DEPLOYDIR="/home/$DEPLOYUSER/www/"
if [ "$(whoami)" != "root" ]; then
echo "You need to be root to run this!"
echo "Try: 'sudo -i', then './install.sh'"
exit 2
fi
function section_title {
echo ""
echo "-------------------------------------"
echo $1
echo "-------------------------------------"
}
section_title "Updating Ubuntu installation"
aptitude update
aptitude safe-upgrade -y
aptitude full-upgrade -y
section_title "Adding user for server to run as"
adduser $DEPLOYUSER --quiet
section_title "Installing OpenSSH"
aptitude install openssh openssh-server -y
section_title "Install build tools"
aptitude install build-essential -y
section_title "Installing Git"
aptitude install git git-core -y
section_title "Installing MySQL"
aptitude install mysql-server mysql-client libmysqlclient15-dev libmysql-ruby1.8 -y
section_title "Installing SQLite"
aptitude install sqlite -y
section_title "Installing Ruby Enterprise Edition, Rubygems, Rake & Rails"
echo "Installing prereqisites ssl & readline..."
aptitude install libssl-dev libreadline5-dev -y
RUBYENTERPRISE="ruby-enterprise-1.8.7-2010.01"
if [ -e $RUBYENTERPRISE.tar.gz ]; then # only download if it isn't already here
wget "http://rubyforge.org/frs/download.php/68719/$RUBYENTERPRISE.tar.gz"
fi
tar zxvf $RUBYENTERPRISE.tar.gz
if [ -d ./$RUBYENTERPRISE ]; then
echo "Directory Found..."
./$RUBYENTERPRISE/installer --auto $RUBYDIR
else
echo "Ruby Enterprise Directory not found! Exiting..."
exit
fi
echo "Putting $RUBYDIR in $PATH"
echo 'export PATH=/$RUBYDIR/bin:$PATH' >> /etc/profile
source /etc/profile # reload bash env with new path
source ~/.profile # reload root's normal .profile
section_title "Updating Rubygems"
gem update
gem update --system
section_title "Highly recommended gems"
gem install sqlite3-ruby
gem install mysql -- --with-mysql-include=/usr/include/mysql --with-mysql-lib=/usr/lib/mysql
section_title "Installing Apache"
aptitude install apache2 apache2.2-common apache2-mpm-prefork apache2-prefork-dev apache-utils ssl-cert -y
aptitude install libapr1-dev libaprutil1-dev libexpat1 -y
echo "ServerName $HOSTNAME" >> /etc/apache2/apache2.conf
apache2ctl graceful
section_title "Installing Phusion Passenger"
gem install passenger
passenger-install-apache2-module --auto
touch /etc/httpd/conf.d/passenger.conf
PASSENGERVERSION=`passenger-config --version`
echo "LoadModule passenger_module $RUBYDIR/lib/ruby/gems/1.8/gems/passenger-$PASSENGERVERSION/ext/apache2/mod_passenger.so
PassengerRoot $RUBYDIR/lib/ruby/gems/1.8/gems/passenger-$PASSENGERVERSION
PassengerRuby $RUBYDIR/bin/ruby
PassengerLogLevel 3
PassengerMaxPoolSize 6
PassengerPoolIdleTime 400
PassengerDefaultUser root
PassengerUseGlobalQueue On" >> /etc/apache2/conf.d/passenger.conf
section_title "Installing Email (SMTP) Server"
aptitude install postfix -y
section_title "Locking Down & Firewalling Install"
echo "*filter
# Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT -i ! lo -d 127.0.0.0/8 -j REJECT
# Accepts all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allows all outbound traffic
# You can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT
# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT
# Allows SSH connections
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
# Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
# log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
# Reject all other inbound - default deny unless explicitly allowed policy
-A INPUT -j REJECT
-A FORWARD -j REJECT
COMMIT
" > /etc/iptables.up.rules
echo "pre-up iptables-restore < /etc/iptables.up.rules" >> /etc/network/interfaces
section_title "Creating Rails project (delete & replace with your own)"
mkdir -p $DEPLOYDIR
if [ -d $DEPLOYDIR ]; then
pushd $DEPLOYDIR
rails $RAILSAPPNAME
popd
chown -R $DEPLOYUSER $DEPLOYDIR
chgrp -R $DEPLOYUSER $DEPLOYDIR
else
echo "Cannot create project! Exiting..."
exit
fi
section_title "Enabling Rails Site with Apache"
echo "<VirtualHost *>
DocumentRoot /home/$DEPLOYUSER/www/$RAILSAPPNAME/public
</VirtualHost>" > /etc/apache2/sites-available/$RAILSAPPNAME
a2dissite default # disable default apache site
a2ensite $RAILSAPPNAME # enables site w/ apache
apache2ctl graceful
section_title "ALL DONE!"
section title "INSTALLED VERSIONS"
echo "- Ruby version -"
ruby -v
echo "- RubyGems Version -"
gem -v
echo "- Rails Version -"
rails -v
echo "- Passenger Version -"
passenger-config --version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment