Setting up a production server with ruby on rails (Deployment included) Part 1

When first creating a ruby on rails application I thought about going with a service like heroku. Yes heroku is great since they manage your server but the prices and addons can get very expensive. If you're a developer who is not scared to dive deep into the command line then look no further. In this tutorial I am going to share with you all how to setup your very own production server with rails 3.

What well be using

  • Ubuntu 10.04
  • RVM
  • git
  • Ruby 1.9.3
  • Rails 3.2.4
  • Mysql / Postgresql

What you'll need

<a title=" <a title="

Setup server

Now since you are aware of the tools that we'll be using, lets begin setting up our server from scratch. When you are all signed into your hosting account setup a new ubuntu 10.04 lts(Lucid Lynx) server. If you are using rackspace then I would start with the 256mb of ram(you can scale if needed). After you have picked your server and have setup a root password we will begin logging into the server via ssh and the terminal.

Open your terminal and type root@ your ip address for your server. A prompt should ask if you would like to save key. Yes is the answer.


Now that your logged into the server lets create a new user so that you are not logged in as root for everything. It will ask for you to enter a password for that user. Enter the password then press enter to skip full name etc.

adduser yourusername

Now that our user account is setup we need to allow our user to install programs but not as root. Open the sudoers file.

sudo nano /etc/sudoers

Scroll down to bottom and add your username. Press CMD + X & y to save.

# User privilege specification
yourusername ALL=(ALL) ALL

Now that we have our user created(though we're still logged in as root) we can stop people from trying to brute-force our root password and take over out account. nano /etc/ssh/sshd_config

Look for the line that has "PermitRootLogin" and change it from yes to no. Now lets restart the server. /etc/init.d/ssh restart

Then logout of your server as root logout

Now that we are all logged out, lets login using our new user credentials. Connect to the server using ssh

Installing rvm

After successfully logging in as the new user let;s update the software on the server.

sudo apt-get update
sudo apt-get upgrade

Now we'll install git so that we can install rvm, ruby, and other dependencies that we may need. sudo apt-get install git-core

Next we'll install rvm

gpg --keyserver hkp:// --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -s

Then run source so we will not have to exit terminal. source ~/.rvm/scripts/rvm

Type code below, if working correctly it will return "RVM is a function" type rvm | head -n1

Now that rvm is setup let's install ruby dependencies


###### CONFIG

if [ "$UID" -ne 0 ]
 echo "Superuser rights required"
 exit 2

 echo -e "# 
    sudo apt-get install build-essential bison openssl libreadline6 \
                     libreadline6-dev curl git-core zlib1g zlib1g-dev \
                     libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev \
                     sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev \

Installing ruby & rails

Now we're ready to install ruby. This will take a while

rvm install 2.2

Now set ruby 2.2 as default

rvm --default use 2.2
gem install rails -v4.2.4

Before moving on make sure ruby and rails are installed in that you're using the correct version and that they are both installed.

ruby --version
rails --version

Adding a firewall

Now lets install a firewall so we're not easily venerable to outsiders. For this tutorial I will use Uncomplicated Firewall – UFW, which is a firewall for our ubuntu server. sudo apt-get install ufw

Now lets setup the rules for our firewall

sudo ufw default deny
sudo ufw allow ssh
sudo ufw allow http
sudo ufw enable

Now lets check our rules to see if port 22, and 80 are open to the internet sudo ufw status

In Part 2 of the next tutorial we will setup mysql, apache, and get our web server up and running.

nginx, capistrano, cloud, deployment, ruby on rails, ubuntu, linux,