Get Shiny Server on DigitalOcean

· 3 min read · Tutorials

Get Shiny Server on DigitalOcean

Get Shiny Server on DigitalOcean

Get Shiny Server on DigitalOcean (or any Ubuntu Server)

TL;DR: Deploy R Shiny apps to the web by installing open-source Shiny Server on a Linux machine. Covers R installation, server config, and how to host multiple apps or per-user directories.


Why Host Your Own?

Services like shinyapps.io are great, but hosting your own Shiny Server offers:

  • Cost savings: $5-10/month VPS vs. expensive pro tiers.
  • Control: No limits on active hours or instance size.
  • Privacy: Keep data on your own controlled infrastructure.
  • Custom domains: Use apps.yourlab.com easily.

Part 1: Prerequisites

Start with a fresh Ubuntu 22.04 or 24.04 server (see Set up a remote server).

1. Install R

Use the official CRAN repository for the latest R version (Ubuntu default is often old).

# Update indices
sudo apt update -qq

# Install helper to add repo
sudo apt install -y --no-install-recommends software-properties-common dirmngr

# Add CRAN key and repo
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
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"

# Install R
sudo apt install -y r-base

2. Install Shiny Package

Install system dependencies first (for httpuv, openssl, etc.).

sudo apt install -y libcurl4-gnutls-dev libcairo2-dev libxt-dev libssl-dev

# Install Shiny package (as root, so it's available globally)
sudo su - -c "R -e \"install.packages('shiny', repos='https://cran.rstudio.com/')\""

Part 2: Install Shiny Server

Download the latest version from RStudio (Posit).

# Check latest version at: https://posit.co/download/shiny-server/
wget https://download3.rstudio.org/ubuntu-18.04/x86_64/shiny-server-1.5.22.1017-amd64.deb

# Install
sudo gdebi shiny-server-1.5.22.1017-amd64.deb

Verify it's running:

sudo systemctl status shiny-server

It should say active (running).

Open Firewall:
Shiny Server runs on port 3838 by default.

sudo ufw allow 3838

Visit http://your-server-ip:3838 in your browser. You should see the "Welcome to Shiny Server!" page.


Part 3: Deploying Apps

Location 1: System-wide Apps (/srv/shiny-server)

This is the default location.

  1. Delete sample apps:

    sudo rm -rf /srv/shiny-server/*
    
  2. Deploy your app:
    Use git to pull your app code.

    cd /srv/shiny-server
    sudo git clone https://github.com/yourname/my-shiny-app.git
    
  3. Install App Dependencies:
    If your app uses ggplot2 or dplyr, install them system-wide:

    sudo su - -c "R -e \"install.packages(c('ggplot2', 'dplyr'))\""
    

Your app is now at: http://your-server-ip:3838/my-shiny-app/

Location 2: Per-User Apps (~/ShinyApps)

Allow users to host apps in their home directories (e.g., http://server:3838/miana/app1).

  1. Edit Config:

    sudo nano /etc/shiny-server/shiny-server.conf
    
  2. Add user_dirs:

    run_as :HOME_USER:;
    
    server {
      listen 3838;
      
      location / {
        user_dirs;  # Enable ~/ShinyApps
        directory_index on;
      }
    }
    
  3. Create Directory:

    mkdir ~/ShinyApps
    
  4. Restart Server:

    sudo systemctl restart shiny-server
    

Part 4: Production Best Practices

1. Run on Port 80/443 (Nginx Reverse Proxy)

Don't make users type :3838. Use Nginx to forward traffic.

sudo apt install nginx

Edit config: /etc/nginx/sites-available/shiny

server {
    listen 80;
    server_name apps.yourdomain.com;

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

Enable it:

sudo ln -s /etc/nginx/sites-available/shiny /etc/nginx/sites-enabled/
sudo systemctl restart nginx

2. Secure with HTTPS (Let's Encrypt)

Never run apps over HTTP.

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d apps.yourdomain.com

3. Log Files

Troubleshooting? Check the logs:

  • System logs: /var/log/shiny-server.log
  • App logs: /var/log/shiny-server/app-name-timestamp.log

Troubleshooting

"An error has occurred" (Grey Screen)

The generic error message hides details.

  1. Check the logs in /var/log/shiny-server/.
  2. Ensure all R packages used in library() calls are installed system-wide (using sudo R).

"Disconnected from server"

Cause: App timeout or WebSocket issue.
Fix: Increase timeout in shiny-server.conf:

app_idle_timeout 300; # 5 minutes

Permission Denied

Shiny Server runs as user shiny (by default). Ensure it can read your files:

sudo chown -R shiny:shiny /srv/shiny-server

See also:


Originally posted on WeChat Official Account: bioinfo_studyroom

Server Setup R