Category Archives: Tutorials

Some Free Tutorials related to Programming,Software,Computer,Web etc..

Adding SSL to a Rails Application

Adding SSL to a new or existing Rails application isn’t really that difficult. If the website exists for a while and you want to move to https, then you must properly redirect (301) visitors to the new url. If it’s a new website, then it’s probably a good idea to use SSL from the start. (it’s must if you collect any kind of sensitive data from the users)

Assumptions : It’s a Rails 4 app, running with nginx, passenger and Ubuntu 14.04 Server. (preferably on VPS, or somehow you should be able to update nginx configs and so). For SSL certificate, I would recommend Comodo PositiveSSL Certificate from NameCheap @ $9 a year, unless you’ve a good reason to spend more on that.

SSL Setup

First, generate a key and then CSR for buying a SSL certificate. Enter the required info as required. Watch out for Common Name / FQDN field, it must match with the domain (in this case :

openssl genrsa -out 2048
openssl req -new -key -out

Then copy the content of above csr file to your clipboard (use xclip, a command line utility) and paste that into SSL order form.

xclip -sel clip < path_to_your_csr_directory/

Next, you’ll receive a confirmation email. After confirming that, they will email you the certificate. (usually within few hours)

Once you receive the ssl certificate (usually in *.zip format), extract the zip file (containing certificates) and concatenate them in right order to get a single certificate file.

cat www_example_com.crt COMODORSADomainValidationSecureServerCA.crt COMODORSAAddTrustCA.crt AddTrustExternalCARoot.crt > ssl-bundle.crt

Now, you need to upload these two files – ssl-bundle.crt and (the private key, generated earlier) to the server. (use scp. e.g scp target_file user@server_ip:file_name)

Preparing Rails for SSL

Enable SSL in production mode, by updating the config/environments/production.rb file.

config.force_ssl = true

And you also need to make sure all the external resources (e.g fonts, images, css, js etc) are loaded securely over https only.

Nginx setup

Login to VPS/Server and create/update your nginx config for ssl.

sudo nano /etc/nginx/sites-available/

A sample nginx config for Rails Application.

Now, enable that nginx config and reload the server.

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
sudo service nginx reload

Deploying static websites to VPS using git

git is a distributed open source version control system. If you’re not already familiar with git then read some free tutorials listed here and come back after you have some basic understanding to follow through the tutorial. Whether it’s a static website or a fun project, it’s usually a good idea to use a version control system such as git. Additionally, git can also help you in deployment and make your development workflow much easier and simpler.

git deployment

Step #1. Server Setup

If you’ve not already selected a VPS then get one there on Digital Ocean or Linode. Make sure you select Ubuntu (preferably : 14.04 LTS) for the server OS. Otherwise, you may need to adjust few commands depending on the Linux distribution you’re using.

Step #2. Configure git on the server

SSH into the remote server and configure git for deployment.

ssh example_user@IP_ADDRESS
mkdir example.git
cd example.git
git init --bare

Next, create a git hook – a shell script that will be executed on git push (to update code on server).

nano ~/example.git/hooks/post-receive

Here is a sample post-receive script you can use : (use Ctrl+X to save and exit)

And make it executable.

chmod +x ~/example.git/hooks/post-receive

Step #3. Install and configure git on dev machine

First, install git, move into your project directory and commit it under git.

sudo apt-get -y install git-core
git init
git add .
git commit -am "MVP is ready for the wild"

Add remote repository for deployment server.

git remote add production ssh://example_user@IP_ADDRESS/~/example.git

And now, you’re ready to deploy. Just type (to push recent changes to live server) :

git push production master

Step #4. Configure nginx

ssh into the server and create a nginx config for the domain (

sudo nano /etc/nginx/sites-available/example.conf

Here is a sample config you could use :

sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/
sudo service nginx reload

Now, update the DNS settings at the registrar and create two records – one for naked domain (e.g and other for www-version of the website (e.g – both pointing to the server IP. And, wait for the DNS to be propagated (few minutes).

Deploying Sinatra Applications to VPS with Passenger/Nginx [Ubuntu 14.04]

Sinatra is a lightweight web development framework (a.k.a micro framework) written in Ruby. It’s a MVC framework (just like Rails) but more suitable for simple and small web projects or APIs (otherwise, you’d probably end up writing too much code, which could be done more easily in Rails, in case of complex web applications).

If you’re new to web development and you’ve just learned ruby then then you should start with sinatra. It’s much easier to learn and you can start creating something (ideally something useful) within hours. Create a simple web app and deploy to Heroku or your VPS using passenger. This article is all about deploying your Sinatra app to VPS.

Why passenger ?

For simple lightweight web applications (sinatra apps), I prefer passenger. You can easily run multiple applications on a single server without any extra configuration (good for small side projects). Later, you can move to unicorn (probably faster response cycle) or puma (lightweight, optimized for concurrency) or something else, if you really need to.

Getting a VPS

Before going through the steps, I assume you’ve already selected a VPS company, if not I would recommend Digital Ocean. (That’s a referral link and you’ll receive $10 credit – worth two months of free hosting) They’ve great plans starting at just $5 per month, well suitable for fun/side projects. For server operating system, select Ubuntu 14.04 LTS, otherwise, you may need to adjust few commands a little, depending on the Linux distribution. For memory and other requirements, 512 MB plan may be enough for a starting out, as you can always upgrade later if required.

Step 1. Server setup

Login into the server (with default root user) :


Update existing packages and install nano (a lightweight text editor)

apt-get update && apt-get upgrade
apt-get -y install python-software-properties nano

Creating a user

adduser username --ingroup sudo

Now, upload your ssh key (public key) from development machine. (local)

ssh-copy-id username@IP_ADDRESS

Now ssh into the server, and make sure ssh keys are setup as as expected. Additionally, you can turn off password authentication for additional security, type sudo nano /etc/ssh/sshd_config and disable password authentication by changing the value from yes to no. (PasswordAuthentication no)

Note : If you haven’t created any ssh key before (on your local computer), then run ssh-keygen to generate one.

Step 2. Install Ruby

install curl, git and other dependencies

sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev

installing rbenv

git clone git:// .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

Install ruby 2 and set it as the default

git clone git:// ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL
rbenv install 2.1.5
rbenv global 2.1.5

Now check if it’s installed correctly

ruby -v

Update gemrc and Install Bundler

echo "gem: --no-ri --no-rdoc" > ~/.gemrc
gem install bundler
rbenv rehash

Step 3. Install nginx and passenger

Adding the passenger repository (official)

sudo apt-key adv --keyserver --recv-keys 561F9B9CAC40B2F7
sudo apt-get install apt-transport-https ca-certificates
sudo sh -c "echo 'deb trusty main' >> /etc/apt/sources.list.d/passenger.list"
sudo chown root: /etc/apt/sources.list.d/passenger.list
sudo chmod 600 /etc/apt/sources.list.d/passenger.list
sudo apt-get update

Install nginx (with extras) and passenger

sudo apt-get install nginx-extras passenger

Step 4. Deployment setup

Setting up nginx and passenger is fairly easy. Just make sure the ruby path is setup correctly in your nginx config. Type  which ruby to get the ruby version and make sure that’s correctly specified at /etc/nginx/nginx.conf. (just lookout for the lines that says passenger_ruby)

/etc/nginx/nginx.conf file (make sure it looks like this)

passenger_ruby /home/rkjha/.rbenv/shims/ruby;

Server config for nginx/passenger

sudo nano /etc/nginx/sites-available/

Here is a sample config you can use. Replace and username accordingly. (it also creates a 301 redirect for www version of your domain, you can change that if you want).

Now create a symlink for that config and reload the server to apply the new config.

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
sudo service nginx reload

Setting up git/bitbucket Add your project to git (if you’ve not done that already), create an account at And create a repository (private unless you want the source code of your app to be available open/freely) there. You may need to upload the public key (server), go to repo’s settings and Add Deployment key there.

git init
git add --all
git commit -am "first release"
git remote add origin  
git push origin master

If you’ve not used git before then check out some free tutorials listed here and comeback later. Deploy using custom rake task You can use some deployment tools like Capistrano (probably overkill for a Sinatra app) or mina (a lightweight deployment tool). But here, I’ll keep things simple and just use a simple rake task to deploy the code.

Step 5. Deploy

To deploy the app simply type : (you may need to add bundle exec before rake command)

rake deploy:setup
rake deploy

You may also need to run bundle install on server, as the rake task is only fetching the latest files from bitbucket repo. Or you can add few lines there (Rakefile) to do that for you. I’ve tried to keep things as simple as possible or I’ll add that later.

If the deployment is successful you can add a DNS entry for your domain. (Make sure you also add an entry for www, so, the will be redirected to, without creating any confusion or duplicate issue in Search Engines)

Note : If you’ve any problem or I missed something, let me know (via comments).


Deploying Ruby on Rails Application to OpenShift PaaS

OpenShift is a Platform as a Service (PaaS) from RedHat. It’s great for deploying web applications as you can setup/scale/manage apps quickly without any hassle, just like Heroku, but the OpenShift platform is available as a free and open source software, so you’re not locked in (you can install OpenShift on your server and create your own private PaaS if you want).

Previously, I’ve already written on various deployment options available for a rails application, also on deploying rails app to vps. In this article, I’ll guide you in deploying your Rails application to OpenShift.

Step 1. OpenShift Setup

First of all, Create a free Account. You get 3 small gears (resource container/unit : one small gear is equivalent to 512 MB RAM and 1GB storage) for free. If you need more, you can upgrade to premium plans.

Install rhc tool

gem install rhc

Note : If you use rbenv for managing ruby then you also need to run `rbenv rehash`.


create token and upload public keys

rhc setup

And follow the instructions. Once the setup is completed, you can easily create/manage your apps using this client utility (rhc).

Just type :


to see available options. If you want to see all the cartridges (application environment e.g ruby, php, python etc) available, just type :

rhc cartridges

Create a Ruby on Rails App [openshift]
Run this command from the parent directory of your project (~/parent_directory/project) or it will create a directory inside your app.

Note : joblee is the name of the Rails app we’re going to deploy. So, replace it with the name of your app. And I assume you’re using Postgresql for database.

rhc app create ruby-1.9 -a joblee

Add database cartridge

rhc cartridge add postgresql-9.2 -a joblee

Add ‘pg’ to your Gemfile. And run bundle install. Although, you’ll receive database details in the end (above command) but it’s a better to use openshift environment variables in database.yml. So, update the database.yml accordingly. Something like this one : production config for database.yml (openshift)

Step 2. Preparing the application for deployment

Once the openshift is setup, move to your project directory and setup git push deployment for your app. rhc show app rails

cd joblee
rhc show-app joblee

And get the value of Git URL from the above command. Now, add the remote url for deployment. (replace GIT_REMOTE_URL with the value you got above)

git remote add openshift GIT_REMOTE_URL

Now, merge the remote repo with your development repo.

git pull openshift master

Then, you need to manually fix conflict in Keep the default content of file.

 git add .
 git commit -am "fixed merge and now getting ready for deployment"

Step 3. Deploy

git push openshift master

That’s all :-). Now, your app should be live at :

Note : If anything goes wrong, you can always ssh into your app server and fix things there. To ssh into your server, type :

rhc ssh joblee

Managing apps
ssh into the app server and type


to see lots of options available. You can also get rails console, just by typing :

cd app-root/repo
bundle exec rails console RAILS_ENV=production

Postgresql Database Setup

cd app-root/repo
RAILS_ENV=production rake db:setup

Assets compilation problem
I had to run few other commands on server due to some gems/assets related problems. But you can add it to openshift deployment hooks like this example rails app, so you won’t have to manually execute it every time you deploy the app.

RAILS_ENV=production bundle
RAILS_ENV=production bundle exec rake assets:precompile

Adding Custom Domain
First, add openshift app url ( as a cname record for www and the IP Address (using the free redirection service) for root record. So, it will redirect root domain to www version.

Now, register that domain with rhc server.

rhc alias-add joblee

If you don’t want to use www domain, then you should type this instead : (and you also need to add cname record for root domain, at your domain registrar)

rhc alias-add joblee

Note : I’ve setup naked domain so, www version of the domain is getting redirected(301) to the root domain. Not all DNS providers supports this (I’m using namecheap and I can specify openshift app url as a CNAME record for the root record) and sometimes it can cause some weird behaviours, especially if you’re using email with the domain. (read more on how it can break MX records)


want to Learn git ? Start Here!

What is Git ?

git is a distributed version control system, developed by Linux Torvalds during the development/management of Linux Kernel. In other words, it’s a software, a tool, that simply keeps track of the different version of the files, If you mess up, you can easily go back to some previous (correct) version of the file. (to any commits)

The purpose of the version control system is to maintain the consistency between the changes made by different(or may just one) people on a large complex project, it allows them to contribute simultaneously in an easy way. Being distributed means – everyone has the full copy of everything – and everyone can work independently (even without any internet connection, but in case if they want to share the changes with other remote developer, the connection will be required as usual).


Why use a version control system such as git ?

There are many advantages such as –

  • it keeps tracks of different versions of the files in your project
  • it’s very fast
  • it also works as a backup for all your files/changes
  • it’s easy to manage the project if there are more than one people working on it
  • a number of people are already using it, so if you want to collaborate on popular open source project or in an organization then probably you might need it
  • it’s distributed and you got full copy of project files, so you can work offline
  • there are cool project hosting apps such as github that makes git even more fun

How to Learn Git ?

First of all, learn some basics from any sources, and start using it in your project. You can’t just learn git by watching videos or reading manuals – you must use it. It’s a very practical thing, even if you learn all the basics of git – you will eventually forget it unless you apply it somewhere.

Recommended tutorials to begin with git :

Note : All the above tutorials are free but if you don’t mind paying for great tutorials then you must checkout “git real” course on Code School.

update 1: added codeschool link!
update 2: added link to some new tutorials!
update 3: added a link to visual explanation for git