Tag Archives: guides

deploying-rails-app to vps

How to Deploy Rails Application to VPS

It seems like your cool rails application is ready to go wild, That’s great! sooner the better. Before you made up your mind for deploying rails application to a VPS – you should know that it’s not a very easy (and only) process. You could have deployed using some other way e.g Heroku, probably much faster.

Manually deploying your rails application to a VPS can be very frustrating for beginners but if you’re willing to learn all those stuffs (GNU/Linux, terminal commands, server administration stuffs and more) or may be you want full control over the things – then probably you made the best choice.

deploying-rails-app to vps

“step by step” guide for deploying a Rails application to VPS

1. get a VPS and install Ubuntu 12.04 LTS

If you don’t already own a VPS then buy one. I recommend Linode (I’ve been using it for my blog as well as for other rails applications, I absolutely love their service). They’ve plans starting at $20/month for 512MB 1024MB 2048 RAM. (which should be sufficient for testing and launch, you can upgrade easily so start with small one, unless you’re sure how much memory you need).

Checkout Linode VPS plans (starting @ $20/month)

Another Great Option – Digital Ocean (starting @$5/mo, 20 GB SSD, 1TB Data, 512 MB RAM, free backups and more!) You can use promo code DEPLOY2DO to get $10 credit (worth two months of free hosting), so you can give it a try without any risk.

I’ve written more about it recently – Digital Ocean Hosting Review.

After getting a VPS ready (usually within few minutes), choose an operating system – I recommend Ubuntu (unless you’ve some specific choice, due to some previous experience with a GNU/Linux distribution such as ArchLinux, Debian etc) – select latest LTS release, ie Ubuntu 12.04 LTS (precise pangolin).

Update! Now, Ubuntu 14.04 LTS is out and available. So, you should choose that. You may need to change few commands below. Although, if you go with 12.04, you can always upgrade.

2. Setting up the Server

When, the vps is ready (within few minutes, after you deploy and boot the server), SSH into the server and use your root account to login (you would get the root password from vps provider’s panel, while deploying the OS, in the above step #1).

 ssh root@vps_ip_address

Now, first update the repository cache and the packages.

apt-get -y update
apt-get -y upgrade

Creating a user

Using root account is not a good idea (of course for security reasons, that’s why there is something called sudo, that would allow you to execute commands with root privilages). So lets create a user, e.g mrhuman, add him to sudo group (so that he can execute commands with root power).

adduser mrhuman --ingroup sudo

(it will ask you to enter some details, as well as the password for the user)

Using password authentication is not secure and it’s also not very efficient. Logout from the current session or switch to the newly created user with su user_name command. In next step, we will be setting up the ssh-authentication.

setup ssh authentication

If you’ve not already generated public/private keys (most likely you already have a pair of public/private keys, e.g you might have generated it for github or bitbucket), then generate one and upload your public key to the vps server.

Type the command(on your local computer)

ssh-keygen
scp ~/.ssh/id_rsa.pub mrhuman@vps_ip_address:

Login to the remote server with newly created user(mrhuman, in this case) and type

mkdir .ssh
mv id_rsa.pub .ssh/authorized_keys

3. Install nginx server

In this guide I assume you’re using nginx server, Apache is another great option. Before installing nginx, first install the utility – python-software-properties, for installing packages via PPA, sometimes the packages available in repository are very outdated.

sudo apt-get -y install python-software-properties

Then install nginx from the PPA.

apt-add-repository -y ppa:nginx/stable
apt-get -y update
apt-get -y install nginx

After installing nginx you can type

sudo service nginx start

to start the nginx and visit the IP address your server, you should see the default page there.

4. Install ruby and other dependencies/gems

For managing ruby versions you should choose RVM or rbenv. In this guide I’m going with rbenv.

Before installing rbenv, lets install some common dependencies such as curl, git version control system, javascript runtime such as nodejs etc.

Installing nodejs

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

Installing curl and git

sudo apt-get -y install curl git-core

Now, install rbenv using an installer

curl https://raw.github.com/fesplugas/rbenv-installer/master/bin/rbenv-installer | bash

Add rbenv to your path variable

After installing rbenv, Just add the following lines to the top of your ~/.bashrc file.

export RBENV_ROOT=\"\${HOME}/.rbenv\"
if [ -d \"\${RBENV_ROOT}\" ]; then
  export PATH=\"\${RBENV_ROOT}/bin:\${PATH}\"
  eval \"\$(rbenv init -)\"
fi

Now, reload the shell by tying the command –

source ~/.bashrc

Install dependencies with rbenv-installer script

rbenv bootstrap-ubuntu-12-04

Installing Ruby

rbenv install 1.9.3-p327
rbenv rehash
rbenv global 1.9.3-p327

Installing Bundler & Rake

gem install bundler --no-ri --no-rdoc
gem install rake --no-ri --no-rdoc
rbenv rehash

Install MySQL and Create database/user

I assume you’re using MySQL in production (or whatver like Postgresql, MongoDB etc, you need to install the required packages/dependenices and create database/user).

Installing MySQL

sudo apt-get -y install mysql-server libmysql++-dev

Now, create database and users (enter the root password, as entred in above step)

mysql -u root -p
create database YOUR_DB_NAME;
grant all on YOUR_DB_NAME.* to DB_USER@localhost identified by 'your_password_here';
exit

5. Setting up Capistrano for Deployment

Now, it’s time to set up capistrano for automating the deployment process. You would also need to have the source code hosted on a private repository (unless you want the source code to be available public) such as on github or bitbucket and add the remote repository to list of identified hosts (to avoid some error during deployment). Simply, ssh into the remote host (remote repository) e.g ssh github.com or ssh bitbucket.org(if you’re using bitbucket) from the remote server.

add following gems into your Gemfile.

gem 'mysql2'
gem 'unicorn'
gem 'capistrano'

and install those gems, by running bundle install.

Now, type the command(of course, in root of your application directory)

capify .

It will create few files that you need to change. First update your Capfile (for the assets section), it should look like this:

Then, update database.yml file (!warning, you shouldn’t put this kind of file in git repository).

Next, add unicorn.rb and unicorn_init.sh (make it executable by running chmod +x unicorn_init.sh) file to config directory.

Next unicorn_init.sh,

Then update config/deploy.rb file.

Don’t forget to change the variable names in above files (e.g replace YOUR_APP_NAME by your application name, git repository URL by your_repository address and more!)

6. Deploy!

That’s all, now it’s time to deploy!

I assume you’re already using git, so commit the latest changes and push it to master branch.

git add .
git commit -am "add deployment configs"
git push master

First setup the deployment configurations using the command –

cap deploy:setup

Then deploy it,

cap deploy

If things look OK (in fact, it may take a while and you may get some errors, just read the messages and act accordingly, rather than freaking out) then open the browser and visit the IP address (or domain, now you can set up DNS for the VPS)

Run the database migrations and start.

cap deploy:migrate
cap deploy:start

Now, Your Rails app should be running fine, any problem? Drop a comment here, we’ll look into that.

Reference(s) and Recommended Link(s)

ubuntu-11-10-live-usb

How to install Ubuntu 11.10 : From USB Drive or CD

Ubuntu 11.10 (code named as Oneiric Ocelot) beta 1 has released and final (stable version of 11.10) released a week ago. If you’re a absolute beginner – i.e you’ve very little (negligible) or no experience with Linux distributions then this post might help you in getting started with Ubuntu as it contains step by step instruction for installing Ubuntu 11.10 on your Desktop/Laptop/Netbook.

The instructions given in this post assume that you want to install ubuntu as a dual boot with Windows 7 (or XP/Vista or whatever you’ve already installed), which is recommended for absolute beginners as if any problem occurs with Ubuntu (or you are not feeling comfortable with Ubuntu, although that’s not probably going to happen) then still you would be able to access Windows, but if you want something else like – removing windows and install Ubuntu or erase whole disk and install Ubuntu on a new computer – then most of the steps would be same – few things will change that I’ve pointed out (Jump to steps).

Ubuntu 11.10 beta 1 has released with a lot of surprises/changes, new features and some improvements in the unity desktop. One of the biggest change is that Ubuntu 11.10 uses Gnome 3, unity also uses Gnome 3 so it means you can install gnome shell on ubuntu 11.10 – in few steps without breaking the unity i.e it will support Gnome 3 without any problem. (although there may be some bugs right now as it’s beta release).

Installing Ubuntu 11.10 – step by step guide

Ubuntu 11.10 can be installed in many ways – one of the easiest method is using wubi installer, but it doesn’t provide much control options so we would go with advanced installation..and one more thing – it’s not as difficult as it appears to be, just follow the instructions – if you’ve any doubt then drop a comment.

Preparing for installation : First of All – backup your important data

Before going to start the installation procedure – you are strongly recommended to backup your data (using a backup disk or online backup program), although you aren’t going to loose any if you’ve multiple partition on your drive and want to go for custom installation procedure, but you’re supposed to have a backup of all your critical data before starting any experiments.

Step 1. Download Ubuntu 11.10 ISO file

First, Download Ubuntu 11.10 ISO , select the archive file (ISO) depending on your computer architecture – such as Intelx86 or AMD64. If you are not sure then go for first one. When the download is complete move on to next step.

Step 2. Create a bootable media (USB/CD)

Then create a bootable USB stick/drive or a CD/DVD from the ISO file you’ve just downloaded. If you want to create a bootable CD/DVD – then it’s pretty easy- you just need to burn the ISO image to the cd.

On Windows

If you are using windows now then use the free program called – universal USB installer. To make your pendrive bootable – use  Universal-USB-Installer (Download and run it – then locate the ISO file, choose your USB drive as a target and your will be done in a minute). In Windows 7 you can burn ISO files directly in few simple steps – Insert cd in to the tray, right click on the ISO file and select burn this ISO.. and finally you will get a bootable cd.

On Ubuntu

There is a program called – ‘Start Up Disk creator’ is installed by default on Ubuntu. So just run the program – browse the ISO and select your pendrive/cd as the target startup disk. That’s All.

In the and of step 2 you will get a bootable media – which is also called as Live CD/USB Installer (this is also very useful for removing viruses from Windows or recovering data from corrupted OS, as you don’t need to enter any password (exception : BIOS password) when your boot your computer using this live cd/usb), Now we will boot the computer from this bootable disk and finally we will install it permanently into the hard drive.

step 3. Insert the USB disk (or CD) and restart

Now restart your computer (and enter your supervisor BIOS passsword, if you have set any password – you system may not boot from CD if you enter user BIOS password). Your computer should boot automatically from the bootable media, and the Ubuntu will be loaded in RAM (if any option comes then select ‘try Ubuntu without installing if you want to take a look before installing it on your hard drive’).

ubuntu-11-10-live-usb

Then click on the install Ubuntu 11.10 icon on the desktop to begin.

installing-ubuntu-welcome

Step 4. Select Installation Type

Now select the check boxes such as ‘Download Updates’ and ‘install third party software’, but you must be connected with Internet (it’s recommended – if wireless network doesn’t seem to work use wired connection). Although there is no hurry – you can always install them later, so it’s optional.

preparing for installation

then click on continue – then a new window will appear – where you need to select installation type.

select installation type

You may get different options depending on your computer configuration. The above snapshot has been taken while installing Ubuntu 11.10 on a computer with Ubuntu 10.04 and Windows 7 pre-installed as dual boot (Now it’s time for triple boot, it’s cool! Hmm). Most of the option seems self explanatory –

  • Install Ubuntu alongside with them : it will install Ubuntu 11.10 alongside with existing operating systems such as Windows 7.
  • Erase Entire Disk and Install Ubuntu : it’s going to erase your whole hard drive and everything will be deleted (your files as well as other operating systems), useful only if your hard-drive doesn’t have any important files or you just bought a new computer and want to keep only one OS – i.e Ubuntu.
  • Something Else : Create, Allocate and choose the partition to which you want to install Ubuntu, using advanced partition manager. At first look it may seems little difficult but it’s better as it give you more options/control.

We will go with the third option – select Something else and continue.

Step 5. Create a new Ext4 Partition and Install Ubuntu 11.10

In the end of step 4, you will get the partition manager window for creating/deleting partitions.

Partition Manager

Then select the free space available for creating partition (if you don’t have any free space then try deleting any empty NTFS drive or the partition whose files has been backed up completely).

Now select the partition which says – ‘free space’ and click on ‘New Partition Table’.

creating ext4 partition

Now, there are so many field in new partition window –

  • New Partition Size : The size of the partition (in megabytes) where you are going to install ubuntu 11.10.Recommended minimum size is 15 GB, if you want to use it for daily purpose, although 5GB (or may be less) is critically required. I’ve left it default as I want to use all that selected free space partition (around 51GB).
  • Location for the New Partition : beginning (if you want it to appear on the top in grub menu (when the system starts) – default OS ) or end (if you want the Ubuntu 11.10 to appear at the end in OS list at GRUB). Don’t worry you can change the default OS later. Let’s choose Beginning.
  • File System (use as) – select Ext4 journaling file system , the latest and recommended files system for Ubuntu 11.10.
  • Mount Point – select / (forward slash), it’s used to mount additional file systems, forward slash means – mounting is done with reference to root.

Then click OK to apply.

select-drive-to-install

Next, click on Install Now button to begin installation procedure.

Step 6. Enter some basic info and Create a user

While installation procedure is going on – you will be prompted to enter some basic information such as

Your Physical Location – it will be used to set your local time and other settings. Select it on world Map.

your-location

Keyboard Layout – Leave it default if you aren’t sure or select the one you want to use.

keyboard-layout

Then Enter some basic details about the user –

  • Your Name : of course your Name (Mr(s). X)
  • Computer’s Name : x’s computer
  • username : Your username – e.g mrx
  • password : Your password (strong – something like this (think twice it’s very easy to remember and it’s very strong!) – U11#kkd_MS!)
  • Require my password to login : better leave this option selected if you want authorized access (unless you aren’t using BIOS password) to your computer, else you can also choose the other option – login automatically.

create-user

Step 7. Restart your computer

Now the installation procedure will complete within few minutes.

installing-ubuntu 11.10

Now when you see a window saying installation is complete – restart your computer and remove the installation media and select Ubuntu 11.10 at the boot menu, and login to ubuntu.

ubuntu 11.10 Desktop screenshot
Ubuntu 11.10 Desktop

That’s All..Now it’s time for installing some basic applications – 10 things to do after installing Ubuntu 11.10, for performing some common tasks.

Update1 : Added a video explaining how to install Ubuntu 11.10, although steps aren’t exactly same as described above but it might help you.