Tony Patton walks you through the steps of hosting a Ruby on Rails application on Windows Azure. He points out the cloud solution isn't Windows-only.
As I continue down the Ruby on Rails path for a current project, it was necessary to find a place to host the solution for testing and production. The various cloud offerings made it an ideal solution as I would not have to worry about infrastructure.
While Amazon's cloud offerings are top-notch, I chose Windows Azure since I had free credits to use, thus reducing costs (in the short term). Here's a walk-through of Windows Azure options, as well as the details of publishing my site on Microsoft's cloud platform.
Windows Azure options
My first thought with Windows Azure was utilizing its Web Sites feature, but at this time it only supports .NET, Java, PHP, Node.js, and Python. The next option was utilizing the IAAS (Infrastructure as a Service) offerings to set up a server instance (virtual machine) and set up the Ruby on Rails environment on it. The following steps outline what is necessary to go this route.
- Create a Windows Azure virtual machine instance.
- Configure virtual machine endpoints for the Ruby on Rails application.
- Install/set up Ruby on Rails on the new virtual machine.
- Copy the Ruby on Rails application to the new virtual machine.
- Install additional Ruby on Rails features (gems) used by the application.
- Start Rails on the virtual machine and test the application.
Set up a new virtual machine
Before we start setting up a new virtual machine, you will need a Windows Azure account to go through the process. Signing up is free, and Microsoft offers free trials, so you can easily take it for a test drive without spending money. Let's move forward with creating a new Windows Azure virtual machine.
The process begins with the Windows Azure portal shown in Figure A. The recently redesigned interface is much more user friendly.
Windows Azure options for creating a new virtual machine.
Figure B shows the options available when Virtual Machines is selected from the left menu in Figure A and you choose Add New. The Quick Create option in Figure B allows you to quickly create a new instance using the operating system image chosen: Windows Server, various LINUX flavors, and many more options are available. The From Gallery option allows you to choose from a variety of configured images, as shown in Figure C where I choose Ubuntu 12.10.
You select Quick Create or From Gallery once you choose to add a new virtual machine in Windows Azure.
Choosing the image type for a new Windows Azure virtual machine.
Once a virtual machine type is selected, you configure it as shown in Figures D and E. A name is assigned to the virtual machine, along with size (number of processors and memory) and a username and password to be used to access the new instance. A DNS name is assigned (this will be part of the server's web application URL) as well as the subscription (how you will be paying).
The bottom of Figure E includes endpoint configuration -- that is, the ports available to access the instance and how the ports are mapped on the machine (if at all). In this example, the default Ruby on Rails port of 3000 is mapped to the standard HTTP port 80, so all requests over port 80 are automatically routed to the private/internal port 3000 on the machine.
Configuration options for a new Windows Azure virtual machine.
Configuring ports for a new Windows Azure virtual machine.
Figure F shows the Windows Azure management portal showing the status of our new virtual machine, which is running and accessible at techrepublicrr.cloudapp.net (the virtual machine will be deleted when this article is posted, so the URL will not be accessible).
The new Windows Azure virtual machine up and running.
Figure G shows the virtual machine details screen opened when double-clicking an instance name in Figure F.
The details of the new virtual machine instance.
The new virtual machine is accessible via SSH. The tool used to access the new instance via SSH will depend on your operating system. I use PuTTY since I am running Windows 7. Figure H shows its simple interface as you type in the host name (techrepublicrr.cloudapp.net) and use the standard SSH port of 22. This provides command-line access to the server.
Using PuTTY to access the new virtual machine.
Bring on Ruby
With our environment set up, we need to configure Ruby on Rails to properly host and run our application. The documentation outlines the following commands to install Ruby on the virtual machine.
sudo apt-get update –y sudo apt-get upgrade –y sudo apt-get install ruby1.9.1 ruby1.9.1-dev build-essential libsqlite3-dev nodejs – y sudo gem install bundler –no-rdoc –no-ri
These commands use the sudo program, which allows you to run programs with the security privileges of another user. You will be prompted for your password when these commands are entered. The final line installs the bundler tool that will be used to install necessary gems for your application. Once these commands are complete, you are ready to copy your Ruby on Rails application to the server.
Move the application to the new environment
SSH can be used to copy files from your local development environment to the new instance, but I prefer to use SFTP with a tool like FileZilla; this allows me to simply drag-and-drop the necessary files/directories to the new environment.
Once the Ruby on Rails application directory is on the new machine, you need to navigate to it via the command line (over SSH) and enter the necessary commands to set up the environment for the application. This includes the following commands, with the first line installing all necessary gems for the application and the final line starting the Rails server.
sudo bundle install rake db:migrate rails s
This starts the basic application with it accepting traffic on port 3000. Figure I shows the basic Ruby on Rails application loaded in Chrome. However, we want to serve a Dashing application, so we need to copy that application to a directory on the new virtual machine as well as make one change to the configuration.
A basic Ruby on Rails application loaded via our Windows Azure server.
Dashing uses port 3030 by default, so we need to edit our endpoint configuration to map port 3030 to the public port 80, thus all requests for port 80 are routed to the local or internal port 3030 on the server. Figure J shows the Edit Endpoint screen, which allows us to change our port mappings, and Figure K has the basic Dashing application loaded when hitting our new virtual machine address. The next step is to integrate with a source control and deployment systems to ease rollout, but that is a subject for another day.
Edit the virtual machine endpoint to properly deliver a Dashing application.
A Dashing application loaded from our new server.
Not just Windows
I was a bit apprehensive about going the Windows Azure route for hosting my Ruby on Rails application, since the Windows Azure product name indicates Windows only (Microsoft Azure would be a better name), but I was happy to learn you can host almost anything. Using Linux-based virtual machines is just one of the many options with the Microsoft cloud offering, so take advantage of Microsoft's free trial offer and see how it can help with your next project.
What cloud option did you choose for your Ruby on Rails application? Why did you choose that particular solution? Let us know in the discussion.