Tag Archives: ruby

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

ssh root@SERVER_IP_ADDRESS

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://github.com/sstephenson/rbenv.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://github.com/sstephenson/ruby-build.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 keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7
sudo apt-get install apt-transport-https ca-certificates
sudo sh -c "echo 'deb https://oss-binaries.phusionpassenger.com/apt/passenger 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/example.com

Here is a sample config you can use. Replace example.com 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/example.com /etc/nginx/sites-enabled/example.com
sudo service nginx reload

Setting up git/bitbucket Add your project to git (if you’ve not done that already), create an account at bitbucket.org 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@bitbucket.org:USERNAME/YOUR-REPO-NAME.git  
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 www.example.com will be redirected to example.com, 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).

References

ruby-2.0

Ruby 2.0.0 “stable” is released!

Ruby 2.0.0-p0 is the first stable release for Ruby 2.0, with a lot of new features and improvements. Ruby 2.0 is very compatible with 1.9, so it will be much easier to migrate to Ruby 2.0 (as compared to 1.8 to 1.9, read the official announcement).

ruby-2.0

What’s new in Ruby 2.0.0 ?

  • core features (better API with keyword arguments, UTF-8 default encoding etc)
  • Built-in libraries (Lazy Enumerator, Onigmo, Async exception handling API..)
  • Debug Support (DTrace support)
  • Performance improvements (GC optimization, faster require, VM optimization etc)

New to Ruby ?

Ruby is an easy to learn scripting language, similar to Python, Perl, PHP etc. It’s optimized for programmer’s happiness :)

Checkout these free tutorials on Ruby.

Ruby 2.0 is out!

Want to Learn Ruby ? Try these free tutorials!

Ruby is one of the most popular programming language, specially when it comes to web development, with the popular frameworks like Ruby On Rails, Sinatra, Padrino etc. it’s very easy to learn and often recommended to absolute beginners.

Ruby is a scripting language – just like Python, PHP and Perl, but ruby is somewhat more beginner friendly than others and certainly it’s more human friendly.  it has elegant syntax that leads to easily readable and maintainable code, and cool built-in methods and conventions makes programming a lot fun.Learning Ruby

Okay, you’ve decided to learn ruby (that’s great!) and here are some cool stuffs to help you get started! Checkout these free tutorials now and enjoy programming with ruby.

#1. Try Ruby

Try Ruby is an interactive ruby tutorials, where you can start running your ruby programs in browser. it’s really good for beginners, just give it a try – you will get a nice overview of ruby, probably within 15 minutes.

#2. Ruby Monk

it’s another great interactive tutorial, along with some code challenges. You will learn a lot of basic stuffs, very quickly!

#3. Leanstreet Ruby Class

A great interactive tutorial, just started (currently in beta), but it’s simply awesome!

#4. humble little ruby book

A good introductory book on ruby, available in a pdf format or you can view it online.

Learning Ruby on Rails ?

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

You got the basic idea about ruby, now you can start exploring on your own. The only way to learn ruby is to write programs in ruby  – so start writing tons of code in ruby. start a simple project (e.g a command line app or anything) in ruby and write/read a lot of code.

Update 1 : Added a new resource for ruby.

Ruby 2.0 is out!

How to execute/run Ruby program/script on Ubuntu [12.04/12.10]

Ruby is one of the most popular programming language, it’s a scripting language – like Perl, Python or PHP. Ruby is very easy to learn, it’s also very Human Friendly – so for beginners – it’s a good one to begin with. Ruby has become very popular, mostly in web development, with the Rails framework (most popular and easy to learn web application development framework for ruby).

If you want to learn ruby or may be just started, with Ubuntu (most probably – 12.04 LTS, if you prefer latest stable release), then this post may help you in setting up the ruby environment (on Ubuntu/Linux Mint/other_similar_distributions) and running your first ruby program; you should also checkout the recommended links section for some best free online tutorials (for ruby).

Install Ruby : Getting Ready for Fun

You can install ruby from the system package – using the apt-get or from Synaptic Package Manager but you’re recommended to use RVM (Ruby Version Manager) for installing ruby( instead of  apt-get install ruby1.9.1). RVM is a great tool that allows you to manage multiple versions of ruby – without any hassle, so the rvm way may seems unnecessary at this point but it’s better in long run.

I’ve already explained about installing ruby on Ubuntu/Linux Mint and I’m not going to repeat that again, Just go through this link – installing ruby on rails in Ubuntu 12.04 and follow the steps 1-3 (leave the 4th step because you just want the ruby right now).

Write your first Ruby program

Open the gedit and write the following lines of code

#! /usr/bin/env ruby
puts "Hello, Ruby"
# it just prints Hello, Ruby on the screen (comment)

Now, save the file as hello_world.rb (Ruby source code has the extension .rb ) in your Home directory (~).

Run/Execute Your first ruby program

Open a terminal and type the commands –

cd ~
chmod +x hello_world.rb
ruby hello_world.rb

This video might give you some additional hints if you need.

Recommended Link(s) for Learning Ruby (Free Online Resources)

Learning Rails ?

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

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!