<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/shiny-server-series-part-2-running-shiny-on-multiple-ports/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.jasperginn.nl/shiny-server-series-pt2/"> Jasper Ginn's blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><em>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.</em></p>
<p><em>I noticed there were some errors in the first part of this series. These are now fixed. The revised post is available <a target="_blank" rel="nofollow" href="https://www.jasperginn.nl/shiny-server-series-pt1/">here</a></em></p>
<p><strong>Part 1 of this series is available <a target="_blank" rel="nofollow" href="https://www.jasperginn.nl/shiny-server-series-pt1/">here</a></strong></p>
<p>In <a target="_blank" rel="nofollow" href="https://www.jasperginn.nl/shiny-server-series-pt1/">part 1</a> of this series, we set up shiny server on a VPS. Our setup currently looks like this:</p>
<ul>
<li>Rstudio server is running on port 8787</li>
<li>Shiny server is running on port 3838</li>
<li>We have a custom domain name pointing to the DigitalOcean VPS</li>
</ul>
<p>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.</p>
<h2>Resources used for this part</h2>
<p>This tutorial draws from <a target="_blank" rel="nofollow" href="https://support.rstudio.com/hc/en-us/articles/219045167-Shiny-Server-Quick-Start-Run-Shiny-Server-on-multiple-ports">Rstudio’s documentation</a> about shiny server configuration.</p>
<h2>Running shiny on multiple ports</h2>
<p>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:</p>
<figure>
<pre><code data-lang="shell"><span># Log into the server</span>
ssh root@&lt;your-VPS-ip>
<span># Switch to shiny user</span>
su shiny
<span># Copy the config file</span>
sudo cp /etc/shiny-server/shiny-server.conf /etc/shiny-server/shiny-server-backup.conf</code></pre>
</figure>
<p>Open the configuration file:</p>
<figure>
<pre><code data-lang="shell">sudo nano /etc/shiny-server/shiny-server.conf</code></pre>
</figure>
<p>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.</p>
<p>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:</p>
<figure>
<pre><code data-lang="text"># Log all Shiny output to files in this directory
log_dir /var/log/shiny-server;</code></pre>
</figure>
<p>Then, add ‘127.0.0.1’ next to ‘listen 3838’ (denoted by ‘2’ in the image below).</p>
<p>Finally, add the following lines below the last ‘}’ (denoted by ‘3’ in the image below):</p>
<figure>
<pre><code data-lang="text"># 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;
  }
}</code></pre>
</figure>
<p>Hit <code>control+x</code> and <code>Y</code> and <code>enter</code> to save your changes.</p>
<p><img alt="" src="https://i0.wp.com/www.jasperginn.nl/assets/images/blog/shiny_series_pt2/SSS_prt2_1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>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:</p>
<figure>
<pre><code data-lang="shell"><span># Navigate to the /srv/ folder</span>
<span>cd</span> /srv
<span># Create a folder for the private shiny server</span>
sudo mkdir private-server
<span># Enter the folder</span>
<span>cd </span>private-server
<span># Give shiny read/write permissions</span>
sudo chown -R shiny:shiny-apps .
sudo chmod g+w .
sudo chmod g+s .</code></pre>
</figure>
<p>Now, we just need to restart shiny server:</p>
<figure>
<pre><code data-lang="shell">sudo service shiny-server restart</code></pre>
</figure>
<p>You are now running shiny server on both port 3838 and 4949!</p>
<p>Now, we simply need to arrange a new route to the nginx configuration. To do this, open up the config file:</p>
<figure>
<pre><code data-lang="shell">sudo nano /etc/nginx/sites-enabled/default</code></pre>
</figure>
<p>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.</p>
<figure>
<pre><code data-lang="text">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 &quot;upgrade&quot;;
}</code></pre>
</figure>
<p>It should look like the configuration in the image below:</p>
<p><img alt="" src="https://i0.wp.com/www.jasperginn.nl/assets/images/blog/shiny_series_pt2/SSS_prt2_2.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Hit <code>control+x</code> and <code>Y</code> and <code>enter</code> to save your changes.</p>
<p>You can now access your second shiny server on <code>http://www.&lt;yourdomainname>.&lt;extension>/private-apps/</code></p>
<p>In the next part, we’ll add an SSL security certificate to the server.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.jasperginn.nl/shiny-server-series-pt2/"> Jasper Ginn's blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/oS7tv7L_MwE"></a></div>