Running a Shiny Server on Microsoft Azure

Want to host your own Shiny Server on Azure? Want to be able to scale it when needed and turn it off when not? Then this is the tutorial for you! You may have read Dean Attali’s tutorial How to get your very own RStudio Server and Shiny Server with DigitalOcean. Well, I’ve forked his tutorial to explain how you achieve most of this on Microsoft Azure.

What’s Shiny?

Shiny is an R Package that lets you build interactive Web Applications with R, enabling you to quickly distribute your data analysis and insights to wider audiences in an interactive/self service manner. Some great Shiny examples are available on the RStudio Shiny Gallery and Show Me Shiny

What’s Microsoft Azure?

Azure is Microsoft’s cloud service provider. It provides a wide variety of cloud offerings from Infrastructure as a Service (like we use in this project) to Platform as a Service offerings (such as Azure Machine Learning Studio).  It enables you to easily host and scale your applications in a cost effective manner.

Sign up for Azure here and get £150 of Free Credit!

Getting Started

Creating a Shiny and RStudio Server in Azure couldn’t be simpler. I have left out a lot of options here such as Azure Active Directory Integration, High Availability and much more. You can read up on common requirements for applications in Azure, and the solutions should be applicable for your server too.

Once you have an Azure account, you are ready to start!

Provisioning Your Virtual Machine

Login:

Once you have your Azure account, or if you already have one, you will need to login to the portal at portal.azure.com. This is where you can manage your Microsoft account and cloud resources.

Creating a Virtual Machine:

Once logged in, you want to purchase and deploy a virtual machine. First, you want to select create new resource in the top left corner of the portal. Then, use the search bar in the new window that popped up and type Ubuntu. You should select Ubuntu 16.04.

Optional:  Instructions for upgrading to Ubuntu 18.04 with R 3.50 which you can try after following this tutorial. 

Once you have done this, you will be prompted with a server dialog. Accept this.

After this, configure the Virtual Machine and specify the machine properties.

You will need to provide the machine name, what type of storage you want, how you will connect (Choose Password for now. You can set up SSH in the future, if you want.), subscription (how you pay/manage costs), a resource group (a collection of Azure resources grouped together for a purpose),  and the server location.

Select a specification for your Azure Virtual Machine. For now, I choose the cheapest machine which is a level B1s, costing me £6.99 per month. More information on selecting Azure Virtual Machines. You can scale your virtual machines at any time and downscale them as well. When machines aren’t running (e.g. they’re shutdown), they will not charge you the main price, but will charge a basic fee for any data stored on their hard disks.

Next, configure other options, as above. You can configure properties such as High Availability if your requirements need it. You will want to configure your network to have a static IP address, so you can always access it with the same URL.

This is achieved by selecting the public IP address property, selecting create new, and assigning a static IP address to your Azure Virtual Machine.

You will also want to open several inbound ports for web traffic and SSH access. You can enable features such as automatic shutdown each night to save cost.

Once those steps are complete, you will be able to read a summary before creating your VM. You can also download the template and parameters to use the Azure Resource Manager API for quick deployment in the future.

Connecting to Your Virtual Machine:

To connect to your Azure Virtual Machine, you will need an SSH client. I recommend Putty, which is free.

You will need your public IP address, which can be found on your resource. If you click the connect button, you will get a pop out with all the information you need.

In the putty dialogue, you will need the following details in the host name field: <username>@<ipaddress>.
This will then prompt you for your password.

Once connected, you will have a terminal to execute all commands. (From this point onward, you could follow Dean’s tutorial).

Configuring Your Server:

Now, you’re onto the easy part…

First, you will need to update your virtual machine and install nginx, which will act as a web server and reverse proxy for our server.


sudo apt-get update
sudo apt-get upgrade
sudo apt-get -y install nginx

If you visit your public IP address in your web browser, you will see a welcome to nginx message.

More information on changing this can be found here.

Install R:

You will need to install R to be able to run a Shiny and/or RStudio Server.

We do this by first adding RStudio’s repository for Ubuntu Xenial (the version of Ubuntu we are running) to your sources list. This enables you to download R related binaries.

sudo sh -c 'echo "deb http://cran.rstudio.com/bin/linux/ubuntu xenial/" >> /etc/apt/sources.list'

Now add the public keys.

gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9
gpg -a --export E084DAB9 | sudo apt-key add -

Now you can add R to your Virtual Machine.

sudo apt-get update
sudo apt-get -y install r-base

To Run R, just type this command.

R

An R console window should show up where you can execute R code. Type q() to quit.

Now, you may have chosen a small virtual machine, but this will cause you some issues when installing packages because you might not have enough memory to get all the necessary files in your RAM. Dean came up with the suggestion to use Swap Memory like below:

sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
sudo /sbin/mkswap /var/swap.1
sudo /sbin/swapon /var/swap.1
sudo sh -c 'echo "/var/swap.1 swap swap defaults 0 0 " >> /etc/fstab'

These two commands are optional but will give you the dev tools library, which is useful for helping install from sites like Github.

sudo apt-get -y install libcurl4-gnutls-dev libxml2-dev libssl-dev

sudo su - -c "R -e \"install.packages('devtools', repos='http://cran.rstudio.com/')\""
stuff here

Installing R Packages:

The above example shows you how to install R packages and should be considered the correct way, as it installs it for all users on the VM and not the current user. So, to install the tidyverse, you would do this:

sudo su - -c "R -e \"install.packages('tidyverse')\""

Installing Shiny:

This installs the R package for use by your Azure Virtual Machine:

sudo su - -c "R -e \"install.packages('shiny', repos='http://cran.rstudio.com/')\""

This gets the latest Shiny Server from Rstudio:

sudo apt-get install gdebi-core
wget https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.9.923-amd64.deb
sudo gdebi shiny-server-1.5.9.923-amd64.deb

Dean has some great Shiny Quick References.

Optional Installing R Studio Server:

 

wget https://download2.rstudio.org/rstudio-server-1.1.456-i386.deb sudo gdebi rstudio-server-1.1.456-i386.deb

Making it work:

Dean says you should be able to access those resources now directly by their ports. This doesn’t happen by default in the Azure Virtual Machine. It is also considered bad practice to have unnecessary ports open, as they’re a security risk. I will show you below how to use nginx to act as a reverse proxy.

You will need to type this command in your putty terminal window:

sudo nano /etc/nginx/sites-enabled/default

This opens a the nginx site configuration in the Linux console text editor nano. You can leave this at any time by typing Control and X.

What you want to do is find the line in that file that has the word

 server_name _;

and add these directly below it:

location /shiny/ {
  proxy_pass http://127.0.0.1:3838/;
}

location /rstudio/ {
  proxy_pass http://127.0.0.1:8787/;
}

Save this file by exiting with Control X and typing yes.

Now, type this command to restart nginx:

sudo service nginx restart

These will give you the following ways to access the files: http://<my_serves_ip_address>/shiny and http://<my_serves_ip_address>/rstudio.

Its Alive!

Now, you’re all set up!  I hope you enjoy your R Stats Shiny Server running on Microsoft Azure! Tweet me when you’re set up or if you have any questions.

 

FAQ

To be completed once I’ve had some questions.