Tag Archives: deploy

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 : example.com).

openssl genrsa -out example.com.key 2048
openssl req -new -key example.com.key -out example.com.csr

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/example.com.csr

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 example.com.key (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/example.com

A sample nginx config for Rails Application.

Now, enable that nginx config and reload the server.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
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
cd example.com
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 (example.com).

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/example.com
sudo service nginx reload

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

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

rhc_setup

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 :

rhc

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 config.ru. Keep the default content of config.ru 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 : http://your_app_name-domain.rhcloud.com.

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

ctl_app

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 (app-domain.rhcloud.com) as a cname record for www and the IP Address 174.129.25.170 (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 www.joblee.in

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 joblee.in

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)

openshift

Heroku Alternatives : For Deploying Rails Applications

Heroku is a cool PaaS (Platform as a Service) for deploying Ruby on Rails applications. It has even a free plan to help you get started (although, it has issue though – often the dyno becomes idle, so I won’t consider it for any real world projects/experiments) but the problem is – as you need more resources, it can easily get quite expensive and slow(if you don’t overthrow more dynos). By the way, if you’re aware of the Heroku’s Routing Secret (causing horrible response time for Rails applications) then it’s the time to look for alternative options.

What are some good alternatives to Heroku ?

Heroku is a Platform as a Service, so it lets you concentrate fully on the application development, by taking care of the server administration stuffs. So, If you want similar experience then you should try Redhat’s OpenShift or Cloud Foundry.

openshift

1. OpenShift

OpenShift is a free and open source PaaS for deploying web applications (supports all cool frameworks such as Ruby On Rails, Sinatra, Django, Node.js etc). They also provide a free plan to get you started – with 3 small gears for free! (gears are resource units, each with some limited amount of RAM (512 MB) and disk space (1GB)). For Rails , currently supported databases are MySQL, MongoDB and PostgreSQL.

Deploying Rails applications is also very easy (checkout an example rails app demo on github).
Know More on OpenShift

2. Cloud Foundry

Cloud Foundry is another PaaS platform for deploying web apps, it’s an open and scalable platform, from the VMware. It also supports a number of frameworks and tools.

checkout Cloud Foundry Project for more details and you can find a Core provider for Cloud Foundry.

There are lots of awesome Cloud Foundry providers including AppFog.

AppFog – it’s a PaaS built on Cloud Foundry, they also have a free plan with 2GB RAM, 50GB bandwidth etc to help you get started.

About AppFog

If you need more control over the things, then IaaS (Infrastructure as a Service) may be best suitable for you. AWS(Amazon Web Services) is the best option for that (Google has also offered similar services such as GCE(Google Compute Engine) but it’s very new and lacks a lot of features).

3. AWS

AWS offers complete set of services for building highly scalable web applications. It has S3 (scalable storage), EC2 (virtual servers on demand), Block Storage, Cloudfront (CDN), Cloudwatch, RDS (Relational Databases such as MySQL), DynamoDB (NoSQL store), Route 53, VPC, SES (Email Notifications), SNS (Push Notifications) and lot of other cool services to help you quickly build, deploy and scale.

checkout AWS services and features

Recently, Amazon has also announced a PaaS service, based on the top of their existing AWS services – Elastic Beanstalk, with the aim of simplifying deploying/managing applications.

4. VPS

Virtual Private Server (VPS) is a good option for small or medium sized predictable web applications. You can easily setup Capistrano for automated deployment. Once the things are setup, VPS management is not so hard as it seems, especially if you got a reliable and awesome VPS provider such as Linode or Digital Ocean (plan starts at $5/mo with 512 MB RAM, 20GB SSD and 2TB Bandwidth, not sure checkout the review of digital ocean)

If you’re not sure, where to begin then checkout this guide about deploying Rails applications to VPS

5. Cloud 66

Cloud 66 is an Application Stack Management as a Service, it’s a platform for provisioning, configuring, deploying and managing your web applications. it allow you to easily deploy (to any infrastructure such as VPS, AWS, Joyent Cloud, Rackspace and more) and scale, so you don’t have to worry about your configuring/monitoring your servers anymore. You also get a free plan to try (includes deploying to 1 server).

Cloud 66

Thank You for reading so far :) Checkout my recent fun projects  created using sinatra : this one on jumble and other one for generating funny slogans (fun weekend projects)

Learning Rails ?

Check out this interactive video course on Ruby on Rails at Treehouse.

Update #1. Fixed some typo.
Update #2. Added AppFog.
Update #3. Added Cloud 66.
Update #4. Added more details about OpenShift gear

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)