The author selected the Free and Open Source Fund to receive a donation as part of the Write for DOnations program.
While many people turn to the open-source programming language R for statistical and graphics applications, Shiny is an R package that allows you to convert your R code into interactive webpages. With Shiny, you can use Shiny Server (available in both a free, open-source format and a paid, professional format) to host and manage Shiny applications and interactive R markdown documents.
In this tutorial, you will install and configure Shiny and the open-source version of Shiny Server on a server running Ubuntu 20.04. You will also install an additional package to run interactive R Markdown documents.
To complete this tutorial, you will need:
An Ubuntu 20.04 server with a sudo non-root user and a firewall, which you can set up by following the Ubuntu 20.04 initial server setup guide.
The latest version of R, which you can install with Step 1 of How To Install R on Ubuntu 20.04.
Nginx installed with access to ports 80
and 443
. Follow How To Install Nginx on Ubuntu 20.04 to install Nginx. In Step 2, use the command sudo ufw allow 'Nginx Full'
to open both port 80 and port 443 instead of sudo ufw allow 'Nginx HTTP'
, which only opens port 80.
A registered domain name. This tutorial uses example.com
throughout. You can purchase a domain name on Namecheap, get one for free on Freenom, or use the domain registrar of your choice.
Both of the following DNS records set up for your server. Follow the DNS Quickstart for details on how to add them.
example.com
pointing to your server’s public IP address.www.example.com
also pointing to your server’s public IP address.A Let’s Encrypt SSL certificate for the domain, which can be installed by following How To Use Certbot Standalone Mode to Retrieve Let’s Encrypt SSL Certificates. In Step 2, obtain the SSL certificate using the command sudo certbot --nginx -d example.com -d www.example.com
instead of the certbot certonly
command given in that article.
Before installing Shiny Server, you need to install the Shiny R package, which provides the framework for running Shiny web applications. While you can install R packages directly from R or the command line, the latter method is recommended to ensure that the packages are installed for all users and not just for the user currently running R.
To install the Shiny R package from the official repository so that it is available to all users, use the following command:
- sudo su - -c "R -e \"install.packages('shiny', repos='http://cran.rstudio.com/')\""
With su -
, you run the command as root. The c
flag will pass the command to the user referenced by su -
. In this case, the command in quotation marks will be passed to sudo.
When complete, the output states that the installation is done and identifies where to find the downloaded source packages:
- Output...
- * DONE (shiny)
-
- The downloaded source packages are in
- ‘/tmp/RtmpPCeOoz/downloaded_packages’
With Shiny in place, you can install Shiny Server and bring up its default welcome screen in your browser.
In this step, you will install Shiny Server and adjust the firewall to allow traffic through the port that Shiny Server listens on.
The latest version for Ubuntu available at the time of writing is Shiny Server 1.5.18.987. Use the following command to download a pre-built binary for 64-bit architectures.
- wget https://download3.rstudio.org/ubuntu-18.04/x86_64/shiny-server-1.5.18.987-amd64.deb
Next, use the following command to verify the integrity of the downloaded file with the Sha256 checksum listed on the RStudio Shiny Server download page:
- sha256sum shiny-server-1.5.18.987-amd64.deb
If the checksums don’t match, re-download the file and verify its integrity.
Shiny Server depends on GDebi for its installation. GDebi is a tool that installs local deb
packages while simultaneously resolving and installing additional dependencies.
Update your package list and then install the gdebi-core
package:
- sudo apt update
- sudo apt install gdebi-core
Next, install Shiny Server with the following gdebi
command:
- sudo gdebi shiny-server-1.5.17.973-amd64.deb
You will be prompted to confirm installation:
OutputShiny Server
Shiny Server is a server program from RStudio, Inc. that makes Shiny applications available over the web. Shiny is a web application framework for the R statistical computation language.
Do you want to install the software package? [y/N]:y
Type y
to confirm that you want to install the package.
The end of the command’s output will now show that a service named shiny-server
is active and running:
Output● shiny-server.service - ShinyServer
Loaded: loaded (/etc/systemd/system/shiny-server.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2022-02-06 01:37:23 UTC; 14ms ago
Main PID: 51306 (shiny-server)
...
Use the ss
command to verify that shiny-server
is listening on port 3838
:
- sudo ss -plut | grep -i shiny
The -plut
flags investigate where shiny-server
is listening:
p
is short for processes and shows the processes using socket.l
is short for listening and displays the listening sockets.u
will display UDP sockets.t
will display TCP sockets.The output will show the following line:
Outputtcp LISTEN 0 511 *:3838 *:* users:(("shiny-server",pid=51306,fd=18))
When you have confirmed that shiny-server
is listening on port 3838
, modify the firewall to allow traffic through to Shiny Server:
- sudo ufw allow 3838
Visit http://your_server_ip:3838
in a web browser to bring up the default Shiny Server homepage, which welcomes you to Shiny Server and congratulates you on your installation. You will see a small box on the right-hand side of the screen with a message saying An error has occurred. When you install rmarkdown
in Step 4, the error message will be replaced with an interactive Shiny Doc.
NOTE: The firewall rule allowing traffic through port 3838 is temporary. Before completing the tasks in Step 3, pointing your browser to https://your_server_ip:3838
will fail to load anything. After completing the tasks in step 3, it will no longer be necessary to access the Shiny Server through that port. You will remove the firewall rule in Step 5.
You now have both Shiny and Shiny Server installed and listening on port 3838
.
In the next step, you will set up a reverse proxy using Nginx and install an SSL certificate for your domain, which will allow you to access Shiny Server securely on port 443
.
In this step, you will configure Nginx to forward incoming requests to Shiny Server through WebSocket, which is a protocol for messaging between web servers and clients.
Open the main Nginx configuration file, nginx.conf
, for editing:
- sudo nano /etc/nginx/nginx.conf
In this file, you can create configuration variables for any Nginx server block to use.
Using Nginx’s map module, create variables for the values that WebSocket needs by copying the following directive into the http
block:
http {
...
# Map proxy settings for RStudio
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
}
The map
directive compares $http_upgrade
(the value of the client’s Upgrade header) to the conditions in the curly braces. If the value is an empty string (''
), then map
creates the $connection_upgrade
variable and sets it to close
. Otherwise, map
creates the $connection_upgrade
variable and sets it to the default value upgrade
.
When finished, save and close the file.
Next, open your domain’s server block:
- sudo nano /etc/nginx/sites-available/example.com
Find and edit the location
block to contain only the following directives:
location / {
proxy_pass http://your_server_ip:3838;
proxy_redirect http://your_server_ip:3838/ https://$host/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 20d;
}
proxy_pass
tells Nginx to forward requests coming in at the root of the web server application to the server’s IP address listening on port 3838
.
proxy_redirect
rewrites the incoming string, http://your_server_ip:3838/
, to its HTTPS equivalent on the server processing the request. The $host
variable evaluates to the hostname of the server Nginx is running on.
proxy_set_header
redefines or appends fields to the request header passed to the proxied server.
proxy_read_timeout
sets a timeout for reading a response from the proxied server between two successive read operations.
When finished, save and close the file.
Before activating the changes you just made in /etc/nginx/sites-available/example.com
, test the file for errors:
- sudo nginx -t
If there are no errors, execute the following command to prevent the default Nginx welcome page from loading:
- sudo unlink /etc/nginx/sites-enabled/default
Then activate the changes by running this command:
- sudo systemctl restart nginx
You have now secured Shiny Server with a reverse proxy and SSL certificate. Try loading https://example.com
(without specifying an IP address and port number) to check that your setup works.
Next, you will configure your setup for interactive R Markdown documents.
In addition to hosting Shiny applications, Shiny Server is useful for hosting interactive R Markdown documents. Your working Shiny Server can host Shiny applications, but it cannot host interactive R Markdown documents without the rmarkdown
R package. You can install the rmarkdown
R package in the same fashion you used to install the shiny
package in Step 1.
Use the following command to install rmarkdown
:
- sudo su - -c "R -e \"install.packages('rmarkdown', repos='http://cran.rstudio.com/')\""
To verify the installation, go to https://example.com
. The error encountered in Step 2 should now be replaced with an interactive plot. Alternatively, you can visit https://example.com/sample-apps/rmd/
to see the full plot by itself.
In Step 2, you created a firewall rule to allow access to the Shiny Server via its port number. Now that it can be accessed via https://example.com
, it is not necessary to provide public access to that port. In this step, you will remove that firewall rule.
To start, review all existing rules using the following command:
- sudo ufw status numbered
The output will look similar to this:
- OutputStatus: active
-
- To Action From
- -- ------ ----
- [ 1] OpenSSH ALLOW IN Anywhere
- [ 2] Nginx Full ALLOW IN Anywhere
- [ 3] 3838 ALLOW IN Anywhere
- [ 4] OpenSSH (v6) ALLOW IN Anywhere (v6)
- [ 5] Nginx Full (v6) ALLOW IN Anywhere (v6)
- [ 6] 3838 (v6) ALLOW IN Anywhere (v6)
The rules that pertain to Shiny Server’s firewall are labeled 3838
(rules 3 and 6 in the output above). Delete the first rule (in the output above, rule 3):
- sudo ufw delete 3
With one rule removed from the list, the rule numbers will change (in this case, rule 6 will now be rule 5). Review the rule numbers with the following command:
- sudo ufw status numbered
The output will be similar to the previous one:
- OutputStatus: active
-
- To Action From
- -- ------ ----
- [ 1] OpenSSH ALLOW IN Anywhere
- [ 2] Nginx Full ALLOW IN Anywhere
- [ 3] OpenSSH (v6) ALLOW IN Anywhere (v6)
- [ 4] Nginx Full (v6) ALLOW IN Anywhere (v6)
- [ 5] 3838 (v6) ALLOW IN Anywhere (v6)
Delete the other 3838
rule (rule 5 in the output above):
- sudo ufw delete 5
To apply the changes, reload the firewall:
- sudo ufw reload
Shiny Server is still active but will no longer be publicly accessible via the port number.
You now have a fully-functioning Shiny Server that can host Shiny applications and interactive R Markdown documents.
When you’re ready to begin deployment, you can find the configuration file for Shiny Server at /etc/shiny-server/shiny-server.conf
. By default, it’s set to serve applications in the /srv/shiny-server/
directory, which means that any Shiny application placed at /srv/shiny-server/app_name
will be available to the public at https://example.com/app_name/
.
To learn more about customizing Shiny Server for your exact needs, visit the Shiny Server Administrator’s Guide or read the tutorials on rstudio.com. For more on writing interactive R markdown documents, check out the R Markdown page on rstudio.com.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Installed a droplet with Ubuntu 20.04 – everything goes well until step 3 here; the test
goes fine, but after restart the IP does not respond – no connection (adding the port :3838 works)