Category Archives: Linux

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)

postman image uploader

Postman – A simple image uploading application for Ubuntu/Linux Mint

Postman is a nice little application that can upload your images to various services, currently it supports Google Plus, Picasa, Flickr, Ubuntu One.

It’s an easy to use application, with an awesome looking graphical interface and minimal set of features, but it does support drag and drop feature for image upload. You can also set basic attributes for the images such as title and description.

postman image uploader

Installing Postman in Ubuntu 12.04/12.10 (or Linux Mint 13/14)

If you prefer Ubuntu Software Center, then you can install it from there or just install using the command –

sudo apt-get install postman-image-uploader

Or you can install it from the PPA

sudo apt-add-repository ppa:schumifer/postman
sudo apt-get update
sudo apt-get install postman-image-uploader
enable-vpn

setup VPN (Virtual Private Network) connection in Ubuntu 12.04/12.10

VPN (Virtual Private Network) connections are great for those who want higher degree of security on the Internet. All of your web traffic will be encrypted. So even if you’re browsing a non-ssl website – your information (e.g login details) is always safe with VPN.

So even if you’re just a web surfer or a heavy torrent downloader you can try VPN to save yourself from the common spy (your ISPs and goverment) and it will also allow you to bypass geographical restrictions (it means if you use a US based VPN then you can browse CBS or Hulu videos from anywhere in the world).

Which VPN provider to choose ?

There are lot of VPN providers, I recommend Private Internet Access (I’m using it – and it’s working great) – it’s good and costs only few bucks a month. If you’re using other VPN provider then you may have to adjust few things according to their recommendation.

Setting up VPN on Ubuntu / Linux Mint

step 1. Go to Network settings and create a new interface for your network – select VPN.

network-settings

Then a new dialog will appear to select the interface type – select VPN .

New Interface - VPN

step 2. Then select the VPN type – choose PPTP (Point to Point Tunneling Protocol).

vpn-connection-type

step 3. Now – add your VPN account details (gateway, there are various gateway you can choose from here is the list of gateways).

vpn-provider - details

step 4. After adding some basic details for your VPN provider, go to advanced settings and enable MPPE.

vpn-enable-ppe

 

step 5. Now – enable VPN and enjoy the higher degree of security :)

That’s all, your VPN setup is done and you can switch between vpn or direct access – just in single click.

Note: using vpn may slow down your browsing experience, it’s not recommended when your connection speed is too slow.

qt-logo

Qt 5 is released!

qt-logo

Qt Framework v 5 is released! Qt is a popular cross-platform application development framework (free and open source, available under GNU LGPL). If you have used KDE desktop environment then you’ve already used a lot of applications built upon it (as KDE desktop heavily relies on Qt).

The new version of Qt (v 5.0) has lot of exciting features (it’s a major released, after a long time – the v 4.0 was released, around 7 years ago).

What’s new in Qt 5.0 ?

  • amazing graphics capabilities and performance
  • Qt quick
  • Modularized Qt Libraries
  • Improved multimedia stuffs
  • Qt platform abstraction
  • JSON support
  • Webkit and HTML 5
  • Read more on Qt 5 features!

Download Qt 5

Linux LOGO

Linux Kernel 3.7 released!

Linux Kernel v3.7 is released (official announcement). The most exciting thing in this release is probably the support for ARM 64 bit architectures.

Linux LOGO

What’s New in Linux (Kernel) 3.7 ?

  • ARM Multiplatform support
  • ARM 64 bit support
  • signed kernel modules
  • updated btrfs
  • adds vxlan (virtual extensible LAN tunneling protocol)
  • SMB v2 protocol support (experimental)
  • supports TCP fast Open (on server side)
  • parallel NFS support

Read about the latest changes/features in more details.