Skip to content

Shiny server configuration with Nginx load balancer

Notifications You must be signed in to change notification settings

gexijin/shinyserver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hosting Shiny apps via Nginx load balancer

The free version of the Shiny server uses one R process on a single CPU core. Therefore it can handle few concurrent users. In this repo, we set up a load balancer using Nginx that connects users with one of the many instances of the Shiny server deployed as Docker containers. Developed by Kevin Son, a graduate student in the Ge lab, this method has been used to host large apps like iDEP and ShinyGO for many years.

To enable the https protocol, please see the https branch of this repo. That branch also has additional information on nested nginx load balancer.

image

Prerequisites

  • A Linux server with port 80 open. This has been tested on Ubuntu and CentOS.
  • Make sure Git is installed.

Steps

  1. Install Docker and Docker-compose.

    curl -fsSL https://get.docker.com -o install_docker.sh
    sudo sh install_docker.sh
    sudo apt install docker-compose
    
  2. Fork this repository, which contains two demo Shiny apps under the shinyapps folder. You can replace these apps with your own once the server is set up. Log in to the Linux server, and clone the forked repository. Here I am using the URL for this repo.

    cd
    git clone https://github.com/gexijin/shinyserver.git
    
  3. Build the Nginx Docker image as specified by this Dockerfile.

    cd ~/shinyserver/
    sudo docker build ./nginx/. -t nginx  --pull
    
  4. Build the Docker image for the Shiny server, as configured by this Dockerfile. This process might be slow, as all the R packages needed for the Shiny app must be pre-installed, using this R script, which needs to be changed according to your app. Note that the docker image has to be called 'webapp'.

    cd ~/shinyserver/
    sudo docker build . -t webapp --pull
    
  5. Start the containers. Your Shiny app is cloned in 10 Docker containers, which can run at the same time. These containers are managed by the Nginx container, as specified in the Docker-compose.yml file. Depending on your resources, you can change the number of containiners. Make sure you edit the Nginx configuration file. and this command, so that they are exactly the same.

    sudo docker-compose up -d --scale webapp=10
    
  6. The two Shiny apps are hosted at http://xx.xxx.xxx.xxx/app1/ and http://xx.xxx.xxx.xxx/app2/. Note that xxx.xxx is your ip address. App1 reads a local file stored in the data folder using relative path (../../data/). App2 is the demo app from RStudio.

  7. Replace the apps and deploy. Now you can clone a local copy of your forked repo to your laptop. Replace the app.R code under the app1 folder with your own R code for the Shiny app. After your are done with the development, push your code to GitHub. Then update your code on the Linux server and restart the server:

    cd ~/shinyserver/
    sudo git pull
    sudo docker-compose down  # shut down
    sudo docker-compose up -d --scale webapp=10
    
  8. Data files. Your data needs to be stored in the shinyserver/data folder, either in the repo or upload directly to the Linux server. From the Shiny apps, these files can be read in by df <- read.csv("../../data/demo_data.csv").

  9. R packages. All the R packages need to be listed in librarySetup.R file under the config folder. These packages needs to be installed when building the docker image for the Shiny server. If a new package is added to your shiny app, the image needs to be rebuild by repeating step 5.

Please let me know if you have any questions or comments.

About

Shiny server configuration with Nginx load balancer

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published