All Articles

Shiny Server Installation on Ubuntu 20.04

Introduction

Shiny Server is an open-source software that allows to self-host apps developed using Shiny package. This tutorial explains how to install Shiny Server on Ubuntu 20.04.

Prerequisites

Before you begin the tutorial, you should:

Install Dependencies

Install package dependencies.

sudo apt install software-properties-common dirmngr libgeos-dev libproj-dev libgdal-dev libxml2-dev libssl-dev libcurl4-openssl-dev

Install R

  1. Add the signing key.

     sudo wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
  2. Add the R 4.0 repository from Comprehensive R Archive Network (CRAN).

     sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
  3. Install the R package.

     sudo apt install r-base-dev
  4. Verify the installation.

     $ R -e 'R.version'
     version.string R version 4.1.3 (2022-03-10)

Install Shiny

  1. Install required compilers.

     sudo apt install make gcc g++ cmake gfortran
  2. Install Shiny package.

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

Install Shiny Server

  1. Install GDebi.

     sudo apt install gdebi-core
  2. Download Shiny Server.

     sudo wget https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.17.973-amd64.deb
  3. Install Shiny Server.

     sudo gdebi shiny-server-1.5.17.973-amd64.deb

Install Nginx

  1. Install Nginx.

     sudo apt install nginx
  2. Enable TCP Port 80 and 443.

     sudo ufw allow 'Nginx Full'
  3. Allow traffic through to Shiny Server.

     sudo ufw allow 3838
  4. Verify if Shiny Server is running.

     $ systemctl status shiny-server
     Active: active (running)

Nginx Configuration for Production

  1. Edit the Nginx configuration file.

     sudo nano /etc/nginx/nginx.conf

    Add the following lines(inside the existing http block) and save the file.

     http {
        ...           
        map $http_upgrade $connection_upgrade {
            default upgrade;
            '' close;
         }
     }        
  2. Edit default Nginx server block.

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

    Add the following lines above the server block and save the file.

     map $http_upgrade $connection_upgrade {
       default upgrade;
       ''      close;
     }

    Add the following lines inisde the location block.

     location / {
       proxy_pass http://127.0.0.1:3838/;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection $connection_upgrade;          
     } 

    Edit the server_name value and save the file.

     server_name yourdomain.tld www.yourdomain.tld;
  3. Test the new Nginx configuration.

     sudo nginx -t
  4. Restart Nginx to activate changes.

     sudo systemctl restart nginx

Navigate to your domain URL and you should see Shiny Server.

https://yourdomain.tld

Installing Packages

When you first navigate to your Domain URL there’s an error at the bottom right section. Install the missing rmarkdown package.

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

You can install any necessary package using the below command. Replace package-name with the actual package name.

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

Proper User permissions for Shiny Server

  1. Create a new usergroup.

     sudo groupadd shiny-apps
  2. Add shiny user and non-root sudo user to the above created group. In this tutorial opsuser is the non-root sudo user.

     sudo usermod -aG shiny-apps opsuser
     sudo usermod -aG shiny-apps shiny
  3. Change ownership of Shiny Server apps directory.

     cd /srv/shiny-server
     sudo chown -R dean:shiny-apps .
  4. Change directory permissions.

     sudo chmod g+w .
     sudo chmod g+s .

Deploying Shiny Apps

Shiny server serves apps from the /srv/shiny-server/ directory. Create the required app name and upload the shiny apps files. Navigate to yourdomain.tld/appname to view your Shiny app.

https://yourdomain.tld/appname

Conclusion

This completes the steps to install Shiny Server and deploy Shiny apps on Ubuntu 20.04. For more information, refer to the official Shiny Server Administrator’s Guide.

Published Jun 5, 2022

Memento Mori.