Skip to content

Instantly share code, notes, and snippets.

@tyrostone
Last active August 16, 2019 03:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyrostone/3b2201217571debc92b823e094c60fb6 to your computer and use it in GitHub Desktop.
Save tyrostone/3b2201217571debc92b823e094c60fb6 to your computer and use it in GitHub Desktop.
Shiny Server Tutorial

Shiny Server Tutorial Instructions

Prerequisites

  • AWS account is created
  • Non-root user is created
  • VPC exists in the us-east-1 region
    • We can go over the networking of what this should look like

Creating the Host

  1. Log into your AWS account
  2. Click EC2 --> Instances
  3. Click Launch Instance
    • Ubuntu Server 16.04 LTS (HVM), SSD Volume Type - ami-43a15f3e
    • t2.micro (for now, free)
    • Make sure subnet is public
    • Make sure you auto-assign a public IP (Enable)
    • Storage is dependent on your database, logging, etc. situation
    • Add a "Name" tag
  • Create or use a security group configured according to the information below (see Security Group Information)
    • Make sure you have whatever key pair you choose (or create a new one)
  1. Wait until your instance is in state "running"
    • Note the public IP of your instance (IPv4 Public IP)
  2. SSH onto the host
    • Need to use terminal or puTTY
    • Add your SSH key to the keychain (if terminal)
    • ssh ubuntu@${PUBLIC_IP}

Security Group Information

	* port 22 TCP (for SSH)
	* port 3838 TCP (for Shiny)
	* port 80 TCP (if installing nginx)
	* port 443 TCP (if installing nginx)

Installing R

  1. sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
  2. sudo add-apt-repository 'deb [arch=amd64,i386] https://cran.rstudio.com/bin/linux/ubuntu xenial/'
  3. sudo apt-get update
  4. sudo apt-get install r-base
  5. sudo -i R (test your installation)

Installing Shiny and Shiny Server

  1. sudo R -e "install.packages(c('shiny', 'rmarkdown'), repos='http://cran.rstudio.com/')"
  2. sudo apt-get install gdebi-core
  3. wget https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.7.907-amd64.deb
  4. sudo gdebi shiny-server-1.5.7.907-amd64.deb

Starting and Stopping the Server

Start

sudo systemctl start shiny-server

Stop

sudo systemctl stop shiny-server

Deploy Your App

  1. scp -r ${REPO}/ ubuntu@${PUBLIC_IP}:/home/ubuntu
  2. ssh ubuntu@${PUBLIC_IP}
  3. sudo mv /home/ubuntu/${REPO} /srv/shiny-server/

Advanced Setup

Running Behind a Reverse Proxy

Installation and Configuration

  1. sudo apt-get install -y nginx

  2. Make the following configuration changes: A) To /etc/nginx/nginx.conf: add: map $http_upgrade $connection_upgrade { default upgrade; '' close; }

    below # Basic Settings in the http {} block B) Create /etc/nginx/conf.d/shiny.conf (see shiny.conf file)

Logs

Access and Error logs

  • /var/log/nginx/access.log
    • /var/log/nginx/error.log

Shiny Logs

  • /var/log/shiny-server/${app}.log

Setting Up A Domain Name

Getting A Domain Name

  1. Navigate to www.freenom.com
  2. Find a domain name you would like (for free!)
  3. Checkout

DNS Management (Connecting Your Instance To Your Domain Name)

  1. https://my.freenom.com/knowledgebase.php?action=displayarticle&id=33
  2. https://www.youtube.com/watch?v=UpyAJkhXFFc

Super Advanced

More AWS Infrastructure

  • Consider using a load balancer and auto-scaling group
  • Consider writing a script to install R and your dependencies and adding it to your instance's userdata
  • Consider hosting your apps in S3 for versioning
  • Consider creating a base AMI that has R, Shiny Server installed, nginx configuration set up
    • Spin up an instance, install these dependencies, create an AMI
      • Select your Instance --> Image --> Create Image
  • Consider adding an EIP (Elastic IP) to your instance so your DNS records do not need to change
server {
listen 80;
listen [::]:80;
server_name ${PUBLIC_IP};
location / {
proxy_pass http://localhost:3838/;
proxy_redirect http://localhost:3838/ $scheme://$host/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 20d;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment