Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.
- This guide shows you how to install R on Ubuntu 16.04
- Dean Attali’s blog post is one of the best and most comprehensive guides on setting up your own shiny server.
- 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:
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:
Next, we give the shiny user admin rights and switch to the shiny user:
Installing dependencies
Firstly, update the list of packages:
Then add the CRAN repository to this list (copy these commands one by one):
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:
Now, we’re ready to install R and other dependencies for shiny server:
Copy and execute the following lines to install these R packages:
Now we’re ready to install shiny server:
The rstudio and shiny server are now accessible on ports 8787 and 3838: you can access them by navigating to < your-ip-address>: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:
Now, make a backup of the nginx configuration:
Open up the configuration file
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).
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.< your-domain-name>.< extension>/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:
Then, give the shiny user read/write permissions:
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:
You can now visit the shiny application by navigating to www.<your-domain-name>.<extension>/apps/TenK-example
in a browser.
Some notes on installing R packages
When installing R packages, it’s best to use the following code:
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:
Navigate to the /srv/shiny-server folder and clone the repository:
Now, enter the repository and start an R session:
Packrat should automatically kick in and start installing packages. If it doesn’t, execute the following:
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.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.