Category Archives: Web Development

Web Development Tools, Tips and resources for beginner developers!

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).

Skeleton – A lightweight CSS framework for your next side project

Skeleton is a lightweight CSS framework that provides a nice boilerplate to kickstart the development process. I like the minimal design approach. Plus, I can easily add more CSS on the top if required. The grid and some basic styles are more than enough to get started.

Simple Responsive grid

Simple, easy and human friendly grid-naming conventions. Just add a div element with class ‘row’ (with child columns) inside a container and you’re good to go (12 column grid – kind of similar to bootstrap and foundation).

Basic styles for Standard elements

Skeleton comes with basic styles for standard HTML elements such as Headings (h1/h2/h3 etc), basic form elements (input, buttons etc), tables and some utility classes (for left/right alignment, clearing floats etc). Here is how a simple ‘sign in’ form with Skeleton.

Lightweight, fast and easy to override

The Skeleton along with Normalize CSS is less than 10KB when minified. So, it’s a great choice for smaller web projects, side projects, mobile websites, landing pages for your mobile apps etc.

You can download the latest version from the official website : (Current version : 2.0.4). Sass and LESS versions are also available to help you add Skeleton in your preferred development setup.

Also check out my last weekend project – Mobile Number Tracker (India) – I created using Skeleton.

Start Unicorn on reboot in a Rails application

If you need to run some cron jobs on server, check out the ruby gem whenever. It lets you write cron jobs in ruby way. You can do lots of useful thing using whenever, create database dumps/backups, run scripts/commands on reboot etc.

Using whenever to start unicorn on reboot

1. Add whenever to your Gemfile and run bundle command.

gem 'whenever', :require => false

2.Writing a simple cron job
Move to the project directory and run

wheneverize .

Now, open the file config -> schedule.rb and remove the default contents. Add these lines instead. (you need to replace unicorn_app with whatever you’ve defined in your nginx config)

If you use capistrano for deployment, you should add this to config/deploy.rb.

require "whenever/capistrano"

Next time, you push the code to the server, it will create the required cron jobs on the server. Make sure, it’s working as expected and let me know (via comments) if you’ve any issue.

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)