Shiny server series part 1: setting up

April 9, 2017
By

(This article was first published on Jasper Ginn's blog, and kindly contributed to R-bloggers)

This guide is part of a series on setting up your own private server running shiny apps. There are many guides out there with great advice on how to set up an R shiny server and related software. I try to make a comprehensive guide based in part on these resources as well as my own experiences. I always aim to properly attribute information to their respective sources. If you notice an issue, please contact me.

Welcome to the first part of the shiny server series!

Recently, I have had to set up several shiny servers that required some custom tweaks like user authentication, running shiny server on multiple ports and setting up SSL. With the help of numerous resources, I got everything running smoothly. I decided to publish these steps on my blog so that others might also use them. In this first part, we’ll be setting up the private server and installing shiny.

Resources used for this part

This guide draws from other guides on the web. The guides below were used to put together this resource.

  1. This guide shows you how to install R on Ubuntu 16.04
  2. Dean Attali’s blog post is one of the best and most comprehensive guides on setting up your own shiny server.
  3. This guides – is used to couple a domain name to your server.

What you’ll need

In order to complete this entire series, I expect you have access to the following:

  • A Virtual Private Server (VPS, see below)
  • A domain name, such as www.iamadomainname.com. These are available on e.g. namecheap.

If you have these two things, you may proceed!

A VP…What?

A Virtual Private Server (VPS) is a service that you can purchase from providers such as DigitalOcean, Amazon AWS and Google Cloud Services. Instead of buying a physical server and planting it in your home somewhere, you essentially rent a small portion of a server from a provider which then looks like a normal server environment: you have your own OS and you can manage your own users and so on.

In this guide, I’ll be using DigitalOcean, but you can use any VPS from any provider as long as it runs on Ubuntu 16.04 and has at least 512MB RAM and one processor. You can use this link to sign up to DigitalOcean. You’ll receive $10 in credit.

Setting up a VPS on DigitalOcean

After signing up to DO, you will see the following screen

There won’t be much going on here … yet! Click on ‘create droplet’ to set up your VPS. The options are relatively straightforward here. You want to select a Ubuntu 16.04 distribution.

Then, you can decide how much power your droplet will have. I chose the smallest version, which works perfectly fine, but feel free to take a bigger size if you like.

Select an appropriate data centre (usually that means choosing one in or near your own country)

The final set of options can look arcane if you’re not used to them. Fortunately, the only option you really need to pay attention to is the SSH key.

An SSH key functions as a unique identifier for you, the owner of the VPS, and adds a layer of security to your server. DigitalOcean provides a tutorial for Windows users and Unix users on their website. This step is optional: you don’t have to do it, but I strongly recommended.

After you’ve set up the SSH access, you can choose a suitable name for your droplet and press ‘create’.

Congratulations! You are now the proud owner of your own VPS.

Accessing your server and setting up shiny

Click on ‘droplets’ at the top right of the DO menu. Copy the IP address of your droplet and open up a terminal or PuTTY. Log into your server:

ssh [email protected]

Setting up a user

Since we don’t want to use the root user to install everything, we’ll create a new user called ‘shiny’. When you are prompted to enter a name, office number etc. you can just hit enter:

adduser shiny

Next, we give the shiny user admin rights and switch to the shiny user:

# Give shiny admin rights
gpasswd -a shiny sudo
# Switch to shiny user
su shiny

Installing dependencies

Firstly, update the list of packages:

sudo apt-get update

Then add the CRAN repository to this list (copy these commands one by one):

# Add a trusted key
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
# Add the CRAN repo
sudo add-apt-repository 'deb [arch=amd64,i386] https://cran.rstudio.com/bin/linux/ubuntu xenial/'
# Update the list again
sudo apt-get update

If your droplet has 1G of RAM or less, we need to set up some swap space. This will allow the server to use hard-drive space as additional RAM when it needs to:

# Set up swap
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/

Now, we’re ready to install R and other dependencies for shiny server:

# Install R
sudo apt-get -y install r-base r-base-dev
# Install shiny server dependencies
sudo apt-get -y install libapparmor1 gdebi-core
# These are used by R libraries
sudo apt-get install -y libxml2-dev libcurl4-openssl-dev libssl-dev

Copy and execute the following lines to install these R packages:

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

Now we’re ready to install shiny server:

# Download rstudio server:
wget https://download2.rstudio.org/rstudio-server-1.0.136-amd64.deb
# Download shiny server:
wget https://download3.rstudio.org/ubuntu-12.04/x86_64/shiny-server-1.5.1.834-amd64.deb
# Install
sudo gdebi rstudio-server-1.0.136-amd64.deb
sudo gdebi shiny-server-1.5.1.834-amd64.deb

The rstudio and shiny server are now accessible on ports 8787 and 3838: you can access them by navigating to :8787 in a browser.

While this is fine, it’s a bit difficult to remember and not very ideal to share with others. This is easily solvable! We can simply point a domain name to our new server.

Pointing a domain name to your server

This step involves two actions: one from the provider you bought the domain name from, and one on the DO dashboard.

Changing name servers

The first thing we’ll do is to change the name servers of your domain name so that they point to the DO name servers. Most providers will give this option and the process is largely the same, but I’ll be using my namecheap account.

Once you are logged into your namecheap account, you will see the following screen:

Click on the ‘manage’ button for the domain name you want to use. You will now see the following screen:

Click on the arrow (surrounded by green box) and select ‘custom DNS’ (surrounded by blue box). Add the DO name servers as shown in the image below:

Save your changes and close the tab.

Setting up A and CNAME records on DigitalOcean

In your DO dashboard, click on the ‘networking’ option

Enter your domain name (without ‘www’) and click ‘add domain’

We’re going to add one A-record and two CNAME records. Firstly, add ‘@’ under the hostname as in the image below and click ‘Create Record’:

Next, create a CNAME record. Under ‘hostname’ enter ‘*’, and under ‘is an alias of’, enter ‘@‘, as shown in the picture below:

Next, create another CNAME record where you input ‘www’ under ‘hostname’ and ‘@’ under ‘is an alias of’:

The entire setup should look like the image below

You are now done setting up your domain name. Nominally, it can take up to 24 hours before your domain name points to your server, but usually this takes less than several hours.

Configuring nginx on your server

OK. So now your server can be accessed using a custom domain name. This is great, but rstudio and shiny server are still only accessible using the ports on which they are hosted. We’ll now set up Nginx on your server to manage the routes to the shiny and studio servers.

Go back to the terminal where you’re logged into the server, and execute the following:

sudo apt-get -y install nginx

Now, make a backup of the nginx configuration:

sudo cp /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/default-backup

Open up the configuration file

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

As in the image below, add ‘localhost’ to the server name (green box).

Then, copy the following lines to the config file and paste them in the location shown in the image (area surrounded by pink box).

        location /apps/ {
              proxy_pass http://127.0.0.1:3838/;
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection “upgrade”;
        }
		  location /editor/ {
              proxy_pass http://127.0.0.1:8787/;
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection “upgrade”;
        }

Hit control+x and then Y+enter, and your changes will be saved. Your rstudio and shiny servers are now accessible using e.g. www../apps

Adding shiny applications

The easiest way to install shiny applications is to create a github or bitbucket repository for the shiny application, which you then clone in the shiny server directory. However, first we need to set up user rights for our shiny user.

Setting up permissions

First, navigate to the shiny server directory where the apps live:

cd /srv/shiny-server

Then, give the shiny user read/write permissions:

sudo groupadd shiny-apps
sudo usermod -aG shiny-apps shiny
sudo chown -R shiny:shiny-apps .
sudo chmod g+w .
sudo chmod g+s .

Example: hosting an application

This part shows how you can store your shiny apps on github and host them on your server.

Sign up for a github account if you have not already. You can also choose to use bitbucket: the process is largely the same.

Create a repository and follow the steps to add files to it.

Follow the steps on the next screen to add files to your repository

When you’ve pushed your server.R and ui.R files, it should look like this

You can now clone this repository in your server by clicking the ‘clone or download’ button (surrounded by the pink box). Copy the url and go back to the terminal where you are logged into your server. Then, execute the following commands:

# Navigate to the folder with shiny applications
cd /srv/shiny-server
# Clone the repository
git clone https://github.com/JasperHG90/TenK-example.git
# Install dependencies
sudo su - -c "R -e \"devtools::install_github('JasperHG90/TenK')\""

You can now visit the shiny application by navigating to www../apps/TenK-example in a browser.

Some notes on installing R packages

When installing R packages, it’s best to use the following code:

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

This ensures that packages are installed globally and that every user has access to it.

Using packrat

You can use packrat – a package management system authored by rstudio – for shiny applications. Packrat is quite useful because it creates a local repository (with, for example, specific package versions) of packages. The advantage of this approach is that it improves the portability of the application and ensures that apps that use the same dependencies but, for example, different versions of that dependency can easily run on the same system. On the flip side, it does not work very well with certain specific libraries such as rgdal. You can read more about packrat on this page.

Setting up packrat

Setting up packrat is easy. In rstudio, start a new project (or open an existing one).

Go to the packrat section and check the box

Packrat will now make a local repository with necessary packages. This can take a while.

Don’t forget to push these changes to your github repository.

Cloning the repo and restoring the library

Getting the repository with the packrat library is very similar to the process outlined above.

First, install the packrat package:

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

Navigate to the /srv/shiny-server folder and clone the repository:

# Navigate to the folder
cd /srv/shiny-server
# Clone the repo
git clone https://github.com/JasperHG90/tenk-example-2.git

Now, enter the repository and start an R session:

# Enter the folder
cd tenk-example-2
# Start an R session
R

Packrat should automatically kick in and start installing packages. If it doesn’t, execute the following:

packrat::restore()

Execute q() to quit the R session

Future content in this series

This wraps up the first part of the shiny server series. You now have a VPS with a custom domain name and running rstudio plus shiny servers. This setup forms the basis on which the next installments of this series build.

Subsequent parts in this series will focus on the following topics

Part 2: running shiny server on multiple ports

We’ll run shiny server on multiple ports so that we have one server to host public shiny apps and another server to host private apps for a select group of people. We’ll add user authentication to this private server in part 4 of this series.

Part 3: adding an SSL security certificate

Before we can add user authentication to shiny, we want to set up an SSL certificate to buff security on our server. This encrypts traffic from and to your server.

Part 4: adding user authentication to your server using Auth0

In this part, we’ll use Auth0, a free service for up to 7.000 users, to add authentication to our shiny server running private applications. To this end, we’ll slightly adapt the excellent Auth0 tutorial to work with our setup.

Part 5: hosting a static website on your server

Most websites have a front end and a back end. The front end is what you see when you visit a page. The back end usually consists of one or more databases and services such as user authentication. Conversely, a static website is simply a front end: you pre-compile all necessary materials before hosting it on a server. Static websites do not require databases and are typically fast to load. Examples of services that help you make static websites are Jekyll and Pelican, to name a few.

You can use the GitHub approach outlined above for shiny applications for static websites as well. You create a repository, upload the static website, and then clone it in the directory from which nginx hosts its example page.

To leave a comment for the author, please follow the link and comment on their blog: Jasper Ginn's blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)