Isaac Sloan - Setting up an Ubuntu 13.04 Server for Rails

Setting up an Ubuntu 13.04 Server for Rails

Step 1: Install Ubuntu

Step 2: Add a deploy user

sudo adduser deploy

Step 3: Update and Install dependencies

Make sure that each line completes before running the next. Don't just copy the lines in all at once or it will use the beginning letters of one line as an answer to questions the first may ask.

sudo apt-get -y 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 vim libmagickwand-dev imagemagick libxml2 libxml2-dev libxslt1-dev

I usually install sshguard to help protect my server from being hacked.

sudo apt-get install sshguard

Step 4: Install ruby 2.0.0

Option 1: RVM
curl -L | sudo bash -s stable
sudo vim /etc/group
# Add your current user and deploy to the rvm group like so: rvm:x:101:isaac,deploy
# Logout and log back in.
rvm list known # finds the newest version of ruby.
rvm install ruby-2.1 #whatever the latests version of ruby is.
rvm ruby-2.1 --default
Option 2: Compile Ruby
tar -xvzf ruby-2.1.1.tar.gz
cd ruby-2.1.1/
sudo make install
sudo gem install bundler

Step 5: Install Node.js

sudo apt-add-repository ppa:chris-lea/node.js
sudo apt-get -y update
sudo apt-get -y install nodejs

Step 6: Install your database

sudo apt-get install mysql-server mysql-client libmysqlclient-dev

Step 7: Install your Web Server (Nginx and Passenger)

Setup a swapfile (Recommended)


swapon -s

If you don't see any swapfiles you should create one like so:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024k
sudo mkswap /swapfile
sudo swapon /swapfile
sudo bash -c "echo '/swapfile       none    swap    sw      0       0 ' >> /etc/fstab"
sudo chown root:root /swapfile 
sudo chmod 0600 /swapfile
Install Passenger
gem install passenger
sudo passenger-install-nginx-module # if you installed ruby with rvm use "rvmsudo"

The following lines will setup you nginx configs with sites-enabled and sites-available directories. This way you can keep you config a lot cleaner as you won't ever have to edit the main conf file.

sudo sed -i '1iuser deploy rvm;' /opt/nginx/conf/nginx.conf #use replace staff with rvm as group if installed.
sudo sed -i '36iinclude /opt/nginx/conf/sites-enabled/*;' /opt/nginx/conf/nginx.conf
sudo sed -i '36iserver_names_hash_bucket_size   64;' /opt/nginx/conf/nginx.conf
sudo mkdir /opt/nginx/conf/sites-enabled
sudo mkdir /opt/nginx/conf/sites-available

To setup the following commands for nginx:

sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx restart


wget -O
sudo mv /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults

Step 8: Host your rails app

Clone your rails app to a directory owned by deploy. Then create a file in /opt/nginx/conf/sites-available/ called

sudo touch /opt/nginx/conf/sites-available/

Edit /opt/nginx/conf/sites-available/ with your favorite editor and add:

server {
  listen 80;
  rewrite     ^$request_uri? permanent;

server {
  client_max_body_size 40M;
  listen 80;
  root /path/to/your/railsapp/public;   # <--- be sure to point to 'public'!
  passenger_enabled on;

Then enabled your site by symlinking your conf file to sites-enabled and restarting nginx.

sudo ln -s /opt/nginx/conf/sites-available/ /opt/nginx/conf/sites-enabled/
sudo /etc/init.d/nginx restart

All done. Enjoy!

UPDATE: Works with Ubuntu 13.04

May 25, 2013
comments powered by Disqus