I would like to introduce you to a method for deploying Ruby on Rails applications entirely on your own terms. It's not a free solution, but it’s definitely affordable and you will have a much more fun coding in Ruby without all the paywall drawbacks. No need to explore the secrets of DevOps.
Every web developer has written at least one application and dreamed of sharing it with friends, but not everyone has a DevOps skills. And when it comes to real deployments, we are looking for automated solutions with the minimum effort required.
I don't know about you, but when I an early Ruby on Rails enthusiast, I started my first deployments on the cloud application platform Heroku. As a student, the price was pretty much the only argument I needed to convince me. Heroku had a free plan, which seemed like an ideal solution for me in those days. All I needed to do was create an account, install Heroku toolbelt, execute a few commands and my application was online. It was a really exciting time.
On a free plan, the application went to sleep after 4 hours of inactivity and it took a few seconds to wake it once asleep. No surprise, after a while it started to annoy me. No one wants to wait, right? There are also limits on number of records in your database and a lot more drawbacks. Some of you may say: "Buy a premium plan and stop complaining dude", while others: "A penny saved is a penny earned".
I would like to introduce you to a method for deploying Ruby on Rails applications entirely on your own terms.
It's not a free solution, but it’s definitely affordable and you will have a much more fun coding in Ruby without all the paywall drawbacks. No need to explore the secrets of DevOps. All you have to do is clone my repository from chef-vps and follow the instructions from the README. If you know the basics of ssh, and how to fire a shell command, it should be enough to get started.
Our goal is to serve multiple applications on a single machine using subdomains. So, we need to have a server and the easiest way to accomplish this is to buy a VPS. For example, a $5/month plan on DigitalOcean is enough to start with a few apps for the development process.
During the creation of your VPS, you can choose an operation system, which is a great starting point. Of course you need to install many tools if you want your server to actually serve RoR apps. All of tools can be installed using chef-solo and cookbooks provided by the chef community. No need to reinvent the wheel again.
I would like to share with you an example of such a machine setup. It can serve RoR applications based on a PostgreSQL database and I hope you will find some helpful content here.
Setup has been tested on Ubuntu 12.04 x64, 14.04 x64.
To begin you need a VPS to which you can connect through ssh. Assume we have one and we can connect to it via:
$ ssh vps.com.
The domain name is also required as we want to serve apps using subdomains. Let's say we have a vps.com domain pointed to our VPS IP address.
VPS needs ruby, nginx, unicorn and all the staff to run RoR apps. chef-solo will help us with installation of the tools.
First we need to create a node for our VPS under the
nodes/directory. Node will run 3 roles:
Install gems before you start cooking VPS (
$ gem install bundler if you don't have it yet):
$ bundle install.
You can fetch cookbooks if you want:
$ librarian-chef install.
To prepare&cook vps run:
knife solo bootstrap vps.
Ok, now you must have a great RoR app that you cannot wait any longer to share with the world. Let's do this!
Create a role for the app (the name can be the same as application name) which runs the "rails_app" recipe:
The application will be available under a subdomain, in this case: http://example_app.vps.com. In addition to the created role still need a node to run application role:
To cook this node, you need ssh config for example_app.vps.com, as shown below:
Cook application node as a root user:
knife solo cook root@example_app.com.vps.
VPS is ready to go, all thats left is to deploy RoR application source code via Capistrano.
Add capistrano and unicorn gems to Gemfile:
Next bundle new gems:
$ bundle install.
Create file Capfile into application root folder:
Create config/deploy.rb file:
and create a production deployment config file:
Also remember to add
.gitignore as those files are already on VPS and commit all your changes.
We're almost there... Let's deploy our app on VPS finally:
$ bundle exec cap production deploy.
Your application is live! Yay!
You need Vagrant and VirtualBox installed:
In chef-vps directory run:
$ bundle install.
Install Vagrant plugins:
$ vagrant plugin install vagrant-vbguest
$ vagrant plugin install vagrant-omnibus
then bring vagrant machine up and running:
$ vagrant up --provision.
This creates the environment, provisioning run, and that's all!
Check this out :)
$ vagrant ssh
ps. If you experience any troubles in developing your RoR project, you may want to check out our Ruby on Rails consulting.