Skip to content

Instantly share code, notes, and snippets.

@knoopx
Created June 21, 2012 12:07
Show Gist options
  • Save knoopx/2965410 to your computer and use it in GitHub Desktop.
Save knoopx/2965410 to your computer and use it in GitHub Desktop.
Setting Up A New Server

Setting up a Rails application on a new server

This guide assumes the following points:

  • You are using a debian-based operating system
  • You would like to deploy your application using RVM, Capistrano and Bundler
  • Your application source code is hosted on a remote GIT repository

Update the operating system

First of all, make sure your system is up-to-date:

apt-get update
apt-get dist-upgrade

Setup LAMP platform

This is pretty straight-forward on Debian. Just use tasksel and a wizard will guide you through all necessary steps and install all necessary packages.

tasksel install lamp

Install other common packages

Are you going to send emails and create thumbnails for images, right?

apt-get install sendmail imagemagick

If you are going to send lots of emails you will need a more reliable email delivering software than sendmail. However, properly setting up an email server is hard, cumbersome and error-prone. You would better use a 3rd party service like http://sendgrid.com/

Install CA Certificates

Before installing anything else we will need CA Certificates, elsewhere curl or wget will complain about untrusted domains.

apt-get install ca-certificates

Install RVM

RVM installation is simple, just execute this as root:

bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

And you will end up with a system-wide installation of RVM, ready for every user on the system. After reloading SSH session rvm command should work.

Install ruby through rvm

Before installing ruby you need to make sure a few dependencies are present. Otherwise ruby will be compiled without zlib, readline or OpenSSL support.

apt-get install zlib1g-dev libssl-dev libcurl4-openssl-dev libreadline5-dev libmysqlclient15-dev
rvm package install openssl
rvm package install readline

Then to install ruby 1.9.2 just execute:

rvm install 1.9.2 --with-openssl-dir=$rvm_path/usr --with-readline-dir=$rvm_path/usr

And RVM will download, configure and compile everything for you.

Install bundler

RVM, Capistrano and Bundler are perfect companions. To use all them we just need to set a default ruby installation

rvm use 1.9.2 --default

And install bundler

gem install bundler --no-ri --no-rdoc

Setup deploy user for Capistrano

We are going to create a new system user called deploy

adduser --system --home /home/deploy --shell /bin/bash --ingroup nogroup deploy

and add it to sudoers, rvm and www-data groups

adduser deploy sudo
echo "deploy ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

adduser deploy www-data
adduser deploy rvm

Also we are going to generate a private/public RSA keypair in order to be able to checkout the application source code from the GIT repository.

su deploy
ssh-keygen

Finally add the public key (/home/deploy/.ssh/id_rsa.pub) to your GIT hosting solution and try to checkout your application to verify everything is working fine and in a password-less fashion.

git clone git://your.repository/application.git

Install passenger

apt-get install apache2-prefork-dev libapr1-dev libaprutil1-dev
#libopenssl-ruby
gem install passenger --no-ri --no-rdoc
passenger-install-apache2-module

Deploy your application

From you local checkout:

cap deploy:setup
cap deploy

Pending

LoadModule passenger_module /usr/local/rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.7/ext/apache2/mod_passenger.so

echo "LoadModule passenger_module /usr/local/rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.7/ext/apache2/mod_passenger.so" > /etc/apache2/mods-available/passenger.load
	
PassengerRoot /usr/local/rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.7
PassengerRuby /usr/local/rvm/wrappers/ruby-1.9.2-p180/ruby

a2enmod passenger
a2enmod expires
a2enmod rewrite
/etc/init.d/apache2 restart
rm /var/www/index.html

GRANT ALL ON database.* TO 'user'@'localhost';
FLUSH PRIVILEGES;

Rescuing the system

Most hosting companies provide you a rescue system in order to boot your server in case of boot failure. In most of the cases you will need to mount your OS filesystem and chroot into it:

mount /dev/sda1 /mnt
mount -o bind /proc /mnt/proc/
mount -o bind /dev /mnt/dev/
mount -o bind /sys /mnt/sys
chroot /mnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment