Tag Archives: rails

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
bundle

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

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

Set up ruby-on-rails

How to install Ruby on Rails in Ubuntu 12.04 LTS

Ubuntu 12.04 LTS (Long Term Support) – Precise Pangolin has already released, and you might have upgraded from 11.10 or installed Ubuntu 12.04 LTS. In this post you will learn how to setup Ruby on Rails on a newly installed Ubuntu 12.04 LTS.

Ruby on Rails is a very popular web development framework, it is based on the principle of  “Convention over Configuration”. Although, application development using Rails is quite easy and fun but setting up rails development environment may be difficult and frustrating, specifically for beginners. I had already written a post about setting up rails on Ubuntu 11.10  but few things has changed and need to be updated for the new version of Rails, Ruby and Ubuntu.

“Hassle Free” Rails Installer for Ubuntu 12.04

Although I’ve explained the installation steps in detail but if already know how these things work then better save time by running the script – that will install Ruby on Rails on Ubuntu 12.04 along with the dependencies and RVM.

Download the Rails Installer Script (From Github)

First change some settings in Gnome Terminal. Go to Edit -> Profile Preferences -> Title and Command and check the “Run Command as login shell”  box.

Login Shell - Terminal

Then make the script executable and execute it (you may have to enter the login password once) –

sudo chmod +x rails-installer.sh
./rails-installer.sh

If anything goes wrong then let me know through comments.

Step by Step Instruction for Setting Up Rails on Ubuntu 12.04 LTS

Just follow these simple steps and withing a couple of minutes you would be creating some nice applications (and of course using Rails).

step 1.  Install git and cURL

First of all, update your package repository.

sudo apt-get update

git is a simple, fast and efficient version control system. It is easy to learn, so even if you don’t have any experience with git you can try it in your next rails project (or any other project). you will love it.

sudo apt-get install git

Curl is a simple command line utility for getting file over web protocols, based on libcurl. To install curl simply execute –

sudo apt-get install curl

step 2. Install RVM and Dependencies

RVM is not strictly required but it makes ruby management a lot easier. You can try different implementations of ruby, different versions of ruby and all without any pain. So it’s strongly recommended. but RVM requires the command to be executed as login shell, so open a terminal and go to Edit -> Profile Preferences -> Title and Command and check the box that says “Run Command as a login shell“. (look at the above snapshot)

curl -L get.rvm.io | bash -s stable

Now, you must load the RVM

source ~/.rvm/scripts/rvm

Then install additional dependencies specified by the RVM –

rvm requirements

sudo apt-get -y install build-essential openssl libreadline6 libreadline6-dev zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

Installing Javascript Runtime
In newer version of Rails, you also need a Javascript runtime. Although you could install it from the package repository but it’s very outdated. So I recommend installing it using the PPA. (and I’ll also update the script)

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

step 3. Install Ruby

Now, you got RVM running, installing and running multiple versions or just one version of Ruby is very simple. To install Ruby just pass the version number to rvm install command (or some other implementation of ruby if you want; RVM also supports rbx, ree, JRuby, IRonRuby other than the default MRI) –

rvm install 1.9.3

Then select the Ruby version you want to use, (or make it default so that you don’t have to select it again in new session)

rvm use 1.9.3 --default

Now, you can check the version of ruby, you’re running right now-

ruby -v

step 4. Install Rails

RVM installs ruby as well as the gem utility (managing ruby libraries). To install rails, simply install it using the gem. It will automatically install the latest version unless you specify the version explicitly.

gem install rails

Now You’re ready to go.

rails new app_name
cd app_name
rails server

Now, open a browser and go to http://localhost:3000. :)

Voila :)

You should also check out this Ruby on Rails Interactive Video Course on Treehouse.

Note : When your Rails Application is ready, you may want to deploy it to a real server, I recommend Digital Ocean (@$5/mo, you get 512 MB RAM, 20 GB SSD and 1TB Bandwidth, cool, isn’t it.) for that, also checkout the Digital Ocean review if you want to know more about them. If you need any help then read this article about Deploying Rails Application to VPS.

Update 1 : Added a video as a reference for Rails Installer Script.

Update 2 : checkout my new rails application: Railyo – Rails freelancing jobs for cool developers!

Set up ruby-on-rails

install Ruby on Rails in ubuntu 10.10/11.04

ruby-on-rails

‘Ruby on Rails’  (ROR) is one of the most popular and productive open source framework that allows you to develop web applications very quickly with a lot of fun.

Ruby is a scripting language (like PHP) that focuses on ease of development and beautiful code. Ruby on Rails or simply ‘Rails’ is a web Apps development framework for Ruby programming language.

Rails is a cross-platform – web Apps development framework and this post will guide you in installing ‘Ruby on Rails’ framework on Ubuntu 11.04 (Natty Narwhal) or 10.10 (Maverick Meerkat) or 10.04 (Lucid Lynx) LTS..procedure is same for all versions. gedit text editor is enough for ruby programming – (usually better than heavy weight IDE’s), you can also customize gedit to look like TextMate (very popular (text editor) among ruby developers who uses Mac OS X).

Ruby version 1.8.7 is installed by default, in most of the Linux based operating system or distributions such as Ubuntu, so what you need to install is – ‘Rails’ , Gems (Gems are modular components or libraries very similar to Package in java), git (git is one of the best – free and open source version control system (developed by Linus Torwalds), it is not strictly necessary but recommended) and curl. Before you move on for installing ROR, Apache (Not necessary because ROR ships with WEBrick, the default HTTP server for rails apps) and MySQL or SQLite should be installed.

Installing Ruby on Rails in Ubuntu

#1 : Install git (not necessary but recommended), the version control system.

sudo apt-get install git

or install from source – Download git (Latest Version : 1.7.5.4). Then extract the archive and move to the extracted directory and type the following command(s) to install it –

make && sudo make install

#2 : Install Curl (Command Tool for transferring data with URL Syntax).

Download it (Latest version : 7.21.7) and install in the similar way as you installed git (in step 1, using source)

#3 : Install  Ruby Version Manager (RVM), a command line tool which allows you to run multiple versions of ruby – such as 1.8.7 as well as 1.9.2. Gem will be installed along with this, if not, then install rubygems before proceeding to next step.

bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

Then type the command given below, to check whether it has been correctly installed, the output after executing the command will be – ‘rvm is a function’

type rvm | head -1

#4 : Install Ruby 1.9.2 (1.8.7 is installed by default)

rvm install 1.9.2
rvm use 1.9.2

#5 : Install Rails

gem install rails

#6 : Install SQLite3 (Not necessary if you want to use MySQL but ROR uses SQLite database by default, so you need to install ruby and sqlite related packages and ORM libraries).

gem install sqlite3

That’s All..now you are ready to rock with ‘Ruby on Rails’. Create new Application using simple command and start the server.

rails new apps_name
cd apps_name
rails server

Now open your browser and type the following address in the address bar –
http://localhost:3000

Learning Rails ?

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

Update! Checkout my rails application: Railyo – job board for rails Freelancers!