Create a Ruby App with Web Apps on Linux

Note

Azure Web App on Linux is currently in public preview and supports running web apps natively on Linux.

Azure Web Apps provides a highly scalable, self-patching web hosting service. This quickstart shows you how to create a basic Ruby on Rails application you then deploy it to Azure as a Web App on Linux.

Hello-world

Prerequisites

If you don't have an Azure subscription, create a free account before you begin.

Download the sample

In a terminal window, run the following command to clone the sample app repository to your local machine:

git clone https://github.com/Azure-Samples/ruby-docs-hello-world
Note

Azure Web App on Linux is currently in public preview and supports running web apps natively on Linux.

Run the application locally

Run the rails server in order for the application to work. Change to the hello-world directory, and the rails server command starts the server.

cd hello-world\bin
rails server

Using your web browser, navigate to http://localhost:3000 to test the app locally.

Hello-world

Modify app to display welcome message

Modify the application so it displays a welcome message. Change the application's controller so it returns the message as HTML to the browser.

Open ~/workspace/hello-world/app/controllers/application_controller.rb for editing. Modify the ApplicationController class to look like the following code sample:

class ApplicationController > ActionController :: base
  protect_from_forgery with: :exception 
  def hello
    render html: "Hello, world from Azure Web App on Linux!"
  end
end

Your app is now configured. Using your web browser, navigate to http://localhost:3000 to confirm the root landing page.

Hello World configured

Launch Azure Cloud Shell

The Azure Cloud Shell is a free Bash shell that you can run directly within the Azure portal. It has the Azure CLI preinstalled and configured to use with your account. Click the Cloud Shell button on the menu in the upper-right of the Azure portal.

Cloud Shell

The button launches an interactive shell that you can use to run all of the steps in this topic:

Screenshot showing the Cloud Shell window in the portal

Create a Ruby web app on Azure

Use the az appservice plan create command to create an app service plan for your web app.

  az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --is-linux

Next, issue the az webapp create command to create the web app that uses the newly created service plan. Notice that the runtime is set to ruby|2.3. Don't forget to replace <app name> with a unique app name.

  az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <app name> --runtime "ruby|2.3" --deployment-local-git

Once the web app is created, an Overview page is available to view. Navigate to it. The following splash page is displayed:

Splash page

Deploy your application

Use Git to deploy the Ruby application to Azure. The web app already has a Git deployment configured. You can retrieve the deployment URL by issuing an az webapp deployment command.

az webapp deployment source show --name <app name> --resource-group myResourceGroup

Notice that the Git URL has the following form based on your web app name:

https://<your web app name>.scm.azurewebsites.net/<your web app name>.git

Create deployment credentials with the az webapp deployment user set command.

A deployment user is required for FTP and local Git deployment to a web app. The user name and password are account level. They are different from your Azure subscription credentials.

In the following command, replace <user-name> and <password> with a new user name and password. The user name must be unique. The password must be at least eight characters long, with two of the following three elements: letters, numbers, symbols.

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

If you get a 'Conflict'. Details: 409 error, change the username. If you get a 'Bad Request'. Details: 400 error, use a stronger password.

You create this deployment user only once; you can use it for all your Azure deployments.

Note

Record the user name and password. You need them to deploy the web app later.

Run the following commands to deploy the local application to your Azure website:

git remote add azure <Git deployment URL from above>
git add -A
git commit -m "Initial deployment commit"
git push azure master

Confirm that the remote deployment operations report success. The commands produce output similar to the following text:

remote: Using sass-rails 5.0.6
remote: Updating files in vendor/cache
remote: Bundle gems are installed into ./vendor/bundle
remote: Updating files in vendor/cache
remote: ~site/repository
remote: Finished successfully.
remote: Running post deployment command(s)...
remote: Deployment successful.
To https://<your web app name>.scm.azurewebsites.net/<your web app name>.git
  579ccb....2ca5f31  master -> master
myuser@ubuntu1234:~workspace/<app name>$

Once the deployment has completed, restart your web app for the deployment to take effect by using the az webapp restart command, as shown here:

az webapp restart --name <app name> --resource-group myResourceGroup

Navigate to your site and verify the results.

http://<your web app name>.azurewebsites.net

updated web app

Note

While the app is restarting, attempting to browse the site results in an HTTP status code Error 503 Server unavailable. It may take a few minutes to fully restart.

Clean up deployment

After the script sample has been run, the follow command can be used to remove the resource group and all resources associated with it.

az group delete --name myResourceGroup

Next steps

Azure App Service Web App on Linux FAQ