Shiny server series part 2: running shiny on multiple ports

April 15, 2017
By

(This article was first published on Jasper Ginn's blog, and kindly contributed to R-bloggers)

This guide is part of a series on setting up your own private server running shiny apps. There are many guides 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.

I noticed there were some errors in the first part of this series. These are now fixed. The revised post is available here

Part 1 of this series is available here

In part 1 of this series, we set up shiny server on a VPS. Our setup currently looks like this:

  • Rstudio server is running on port 8787
  • Shiny server is running on port 3838
  • We have a custom domain name pointing to the DigitalOcean VPS

In this part, we’ll configure shiny server to run on two ports; one on 3838 and one on 4949 (although this is arbitrary and you could use any port you’d like). This way, we can set up our server to service a publicly accessible shiny server instance as well as an instance that will be password protected. This is useful if you want to share an app with a select group of people or just for development purposes.

Resources used for this part

This tutorial draws from Rstudio’s documentation about shiny server configuration.

Running shiny on multiple ports

These steps are fairly straightforward. First, log into your VPS, switch to the shiny user and back up the shiny configuration file in case something goes wrong:

# Log into the server
ssh [email protected]
# Switch to shiny user
su shiny
# Copy the config file
sudo cp /etc/shiny-server/shiny-server.conf /etc/shiny-server/shiny-server-backup.conf

Open the configuration file:

sudo nano /etc/shiny-server/shiny-server.conf

This configuration governs the port on which shiny runs, as well as the location where it stores error logs and where shiny can find your apps.

Firstly, add the following line under ‘run_as shiny’ (denoted by ‘1’ in the image below). This ensures that shiny outputs error logs in this folder:

# Log all Shiny output to files in this directory
log_dir /var/log/shiny-server;

Then, add ‘127.0.0.1’ next to ‘listen 3838’ (denoted by ‘2’ in the image below).

Finally, add the following lines below the last ‘}’ (denoted by ‘3’ in the image below):

# Define a server that listens on port 4949
server {
  listen 4949 127.0.0.1;

  # Define a location at the base URL
  location / {
    # Host the directory of Shiny Apps stored in this directory
    site_dir /srv/private-server;

    # When a user visits the base URL rather than a particular application,
    # an index of the applications available in this directory will be shown.
    directory_index on;
  }
}

Hit control+x and Y and enter to save your changes.

This configuration ensures that shiny listens on both ports 3838 and 4949. As you can see in the config file, the private shiny server does not look for shiny apps in the ‘/srv/shiny-server’ folder, but rather in the ‘/srv/private-server’ folder. We need to create this folder and give the shiny user read and write permissions:

# Navigate to the /srv/ folder
cd /srv
# Create a folder for the private shiny server
sudo mkdir private-server
# Enter the folder
cd private-server
# Give shiny read/write permissions
sudo chown -R shiny:shiny-apps .
sudo chmod g+w .
sudo chmod g+s .

Now, we just need to restart shiny server:

sudo service shiny-server restart

You are now running shiny server on both port 3838 and 4949!

Now, we simply need to arrange a new route to the nginx configuration. To do this, open up the config file:

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

Then, copy paste the following lines just below the editor route. Indent the lines so they line up with the other routes, and remember to only use spaces, no tabs.

location /private-apps/ {
    proxy_pass http://127.0.0.1:4949/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

It should look like the configuration in the image below:

Hit control+x and Y and enter to save your changes.

You can now access your second shiny server on http://www../private-apps/

In the next part, we’ll add an SSL security certificate to the server.

To leave a comment for the author, please follow the link and comment on their blog: Jasper Ginn's blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)