In this how-to, we are going to see how to deploy easily a Ruby on Rails application to Azure. We will setup a PostgreSQL database as well, since it is the most used database for Rails apps, and this is not the default setup in Azure.

This article will be divided into three chapters. First, we will create a simple Rails app. Then, we will deploy it on Azure. Finally, we will setup the PostgreSQL database.

Creating an example Rails application

For the example, let’s consider a brand new Rails app.

rails new example-app --database=postgresql

cd example-app

bundle

git add .

git commit -m “Initial commit”

Now, we will give it a light data structure.

rails generate scaffold User name:string email:string

rails db:create

rails db:migrate

git add .

git commit -m “Implement User model, views and controllers”

Our example application is now fully working. To see the results, just start it locally:

rails server

Now, you should be able to access it on your web browser :

As you can see, when you visit localhost:3000/users, you can can list, show, create, update and delete users from the database we’ve just created together. This very simple use case is enough for today’s objective. Now it’s time to move on to the next chapter, and deploy the application to Azure.

Deploying the application on Azure

In this part, we will go step by step, from creating a Microsoft Azure account, to having our example-application in production.

Step 1: Log in to Azure

You need any MS account to be able to sign up to Azure (Hotmail, Live, Outlook, etc.). If you don’t, you can create one at signup.live.com.

Once you’ve got your account, you need to link it to Azure at signup.azure.com.

Step 2: Download the Azure CLI

You need to install the Azure command line tool to be able to do any of the next steps. It’s available at Azure-CLI, and you can call it from your terminal with the command

az

Sign your CLI up with:

az login

Follow the steps described by your terminal to get your credentials. Then, log in with:

az login -u <username> -p <password>

Step 3: Set your deployment user credentials

Now, you need to set your deployment user credentials, by tipping:

az webapp deployment user set --user-name <username> --password <password>

<username> and <password> have to be defined by you, and will be used to authorize your future app deployments.

Step 4: Create a resource group

Azure uses resource groups to set your deployment environment up. You can attribute assets to your groups, depending on your application requirements. These resources can be virtual machines, databases, web apps, etc.

Let’s create our resource group, called exampleResourceGroup:

az group create --location westeurope --name exampleResourceGroup

Being in France, I’ve selected westeurope, but you should of course define yours depending on your needs.

Step 5: Create plan + webapp

The first resource to create inside exampleResourceGroup is a plan for our example-app Rails webapp:

az appservice plan create --name exampleAppServicePlan --resource-group exampleResourceGroup --is-linux

Now, we can create our exampleApp webapp, associated to the plan. By definition, it will also be part of the resource group:

az webapp create --resource-group exampleResourceGroup --plan exampleAppServicePlan --name exampleApp --runtime “ruby|2.3” --deployment-local-git`

As a result, you’ll receive several information relative to your webapp newly created. The important here is to store the deployment url. It looks like:

https://<deployment_username>@exampleApp.scm.azurewebsites.net/exampleApp.git

To deploy your application, you will have to push it there.

The address exampleApp.azurewebsites.net can now be visited on any web browser, but it actually doesn’t contain anything.

Step 6: Push you application to Azure

Now, to deploy your application, you just need to define locally the correct git remote, and push it to the Azure server:

git remote add azure https://<deployment_username>@exampleApp.scm.azurewebsites.net/exampleApp.git git push azure master az webapp restart --name exampleApp --resource-group exampleResourceGroup

The last command is optional, but to be sure that everything worked well, it safer to restart the application onto the server.

Step 7: See the results of your deployment

Just visit the webapp address (exampleApp.azurewebsites.net), it’s alive! 👍 But, everything is not finalized. Since only the webapp has been deployed, it is not working correctly. The data is missing. Then, our example application doesn’t know how to deal with our User model. In the next step, we will see how to deal with the database, and how to have the Rails application communicating with it.

Setting up the PostgreSQL database

At the moment, the only resource enabled in exampleResourceGroup is our example webapp. It doesn’t have any database to communicate with. Also, Azure proposes by default MySQL, and the documentation is mostly written for this database. Being Rails devs, we are going to see how to deal with PostgreSQL.

Step 1: Create a database in the resource group

Create your PostgreSQL database:

az postgres server create --name databaseName --resource-group exampleResourceGroup --location “North Europe” --admin-user <usernname> --admin-password <password>

You have to define a name for the DB, and an admin user (username and password).

Sending this command, you will receive a result like:

{

“administratorLogin”: <username>,

“fullyQualifiedDomainName”: “databaseName.postgres.database.azure.com”,

“location”: “northeurope”,

“name”: “databaseName”,

“resourceGroup”: “exampleResourceGroup”,

…

}

Store it!

Step 2: Setup the firewall rules

Now, we have to setup the firewall, in order to correctly define the access to the database:

az postgres server firewall-rule create --name allIP --server databaseName --resource-group exampleResourceGroup --start-ip-address 0.0.0.0 --end-ip-address 255.255.255.255

For the example, we are going to allow every IP addresses (0.0.0.0 to 255.255.255.255). For a real production application, you have to correctly define the firewall rules.

Step 3: Check the database access

Check that you can access the database by typing:

psql -U <username>@databaseName -h databaseName.postgres.database.azure.com -p 5432 -d postgres

It should request your database password, and then let you access the PostgreSQL database.

Step 4: Setup rails to access the DB in production

Locally, you have to change Rails DB configurations. In the file config/database.yml, change the production section so it looks like this:

production:

<<: *default

host: <%= ENV[‘DB_HOST’] %>

port: 5432

database: <%= ENV[‘DB_DATABASE’] %>

username: <%= ENV[‘DB_USERNAME’] %>

password: <%= ENV[‘DB_PASSWORD’] %>

Step 5: Set environment variables

Locally, and on Azure, you have to define the corresponding environment variables. First, on the server:

az webapp config appsettings set --name exampleApp --resource-group exampleResourceGroup --settings DB_HOST=”databaseName.mysql.database.azure.com” DB_DATABASE=”postgres” DB_USERNAME=”<username>@databaseName” DB_PASSWORD=”<password>”

Then, in your .env file, at the root of your rails app, add the following lines:

DB_HOST=”databaseName.mysql.database.azure.com”

DB_DATABASE=”postgres”

DB_USERNAME=”<username>@databaseName”

DB_PASSWORD=”<password>”

Step 6: Migrate your production database

Migrate locally your production database, from the terminal:

rails db:migrate RAILS_ENV=production

Step 7: Final Azure config

Generate a secret, that we will call <rails_secret> below:

rails secret

Proceed to a last setup of azure:

az webapp config appsettings set --name exampleApp --resource-group exampleResourceGroup --settings RAILS_MASTER_KEY=”<rails_secret>” SECRET_KEY_BASE=”<rails_secret>” RAILS_SERVE_STATIC_FILES=”true” ASSETS_PRECOMPILE=”true”`

Here, we provide Azure the secret generated for our rails app, and we tell Rails to serve static files, and to precompile its assets.

We manually precompile the assets :

rake assets:precompile

Step 8: Push the application

Now that Azure is ready to deal with the database, and the database is correctly migrate locally, we can push the whole application to Azure. Migrations will be correctly applied to the production DB on the server.

One final advice: each time you change something in the database, you have to migrate locally the production DB before pushing it to the server:

rails db:migrate RAILS_ENV=production

Final step

Your Rails application is now deployed on Azure, and your PostgreSQL database is well setup. You can now visit it on your browser, it should be working perfectly.

For people in France next week, you can go deeper into Azure understanding going at Azure Red Shirt Dev Tour. It will take place at Cent Quatre in Paris on Tuesday, January 23rd. You can register for free at :