knitr::opts_chunk$set(echo = TRUE)

This document is (originaly / mainly taken / greatly inspired) from Dean Attali tutorial to setup an Rstudio and Shiny server. The only purpose of this document is to give up-to-date guidance for the specificities of the sear app shiny server setup.

There will be (for sure ...) specificities related to the machine/server you will try do deploy on. When errors occurs, box breath, take a walk, drink something good, look at the error log and find your way. Also add some information here about it so others (and your future self ;) ) can build on it !

nginx

Install nginx

sudo apt update
sudo apt -y install nginx

The default file that is served is located at /var/www/html/index.nginx-debian.html The configuration file is located at /etc/nginx/nginx.conf

The default nginx settings only allow file uploads of 1MB, to be able to upload larger files, you’ll need to set the client_max_body_size nginx parameter in nginx.conf file in http{} section.

sudo nano /etc/nginx/nginx.conf
client_max_body_size 1G;

Restart the nginx service and check it's status.

sudo systemctl restart nginx
sudo systemctl status nginx

R

Kinetic (22.10) (The DigitalOcean Droplet) is not yet LTS and therfore is not supported by CRAN so we use Jammy (22.04)

You can get the name of your release and replace "Jammy" with the command $(lsb_release -cs).

sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"

Add the public key to the repository

gpg --keyserver keyserver.ubuntu.com --recv-key E298A3A825C0D65DFD57CBB651716619E084DAB9
gpg -a --export E298A3A825C0D65DFD57CBB651716619E084DAB9 | sudo apt-key add -

Install R

sudo apt update
sudo apt install r-base

Dependencies

The project r2u offer a way to integrate CRAN and apt to manage system libraries dependancies for R libraries in an automated manner. If you successfully installed it you don't have to manually install the system dependencies and you can go strait to install R dependencies.

The R library terra require gdal-config but does not tell R about it. So the above doesn't work and you have to manually install gdal-config. Not sure if both gdal-bin and libgdl-dev are needed

sudo apt install gdal-bin libgdal-dev

NO LONGER NEEDED To use python virtual env

sudo apt install python3.10-venv

System

sudo apt install libcurl4-gnutls-dev libxml2-dev libssl-dev libfontconfig1-dev libharfbuzz-dev libfribidi-dev libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev gdal-bin libgdal-dev libudunits2-dev libsodium-dev

R

Install R packages as su (Super User) to make them available to all users on the machine.

sudo su - -c "R -e \"install.packages('devtools')\""
sudo su - -c "R -e \"install.packages('shiny')\""

Also need to install sear dependencies :

sudo su - -c "R -e \"install.packages(c('clock', 'config', 'DBI', 'dplyr', 'DT', 'golem', 'hms', 'lubridate', 'plotly', 'raster', 'readr', 'reticulate', 'RSQLite', 'sf', 'shinydashboard', 'shinyFeedback'))\""


sudo su - -c "R -e \"install.packages(c('shinyWidgets', 'spsComps', 'suncalc', 'tidyr', 'uuid', 'waiter', 'widgetframe', 'shinyjs', 'sodium', 'shinymanager', 'keyring', 'gsw', 'pracma'))\""

Rstudio server

sudo apt install gdebi-core

Check the latest version of Rstudio Server

SystemRelease <- system2("lsb_release", "-cs", stdout = T)
RstudioDown <- system2("curl", "https://posit.co/download/rstudio-server/", stdout = T)
stringr::str_subset(RstudioDown, paste0(SystemRelease, ".*", "rstudio-server.*\\.deb"))
wget https://download2.rstudio.org/server/jammy/amd64/rstudio-server-2023.03.1-446-amd64.deb

Install it

sudo gdebi rstudio-server-2023.03.1-446-amd64.deb

Shiny server

Could automatically fetch the latest with some web scrapping technique ?

latest Shiny Server

SystemRelease <- system2("lsb_release", "-cs", stdout = T)
RstudioDown <- system2("curl", "https://posit.co/download/shiny-server/", stdout = T)
stringr::str_subset(RstudioDown, paste0(".*", "shiny-server.*\\.deb"))
wget https://download3.rstudio.org/ubuntu-18.04/x86_64/shiny-server-1.5.20.1002-amd64.deb
sudo gdebi shiny-server-1.5.20.1002-amd64.deb

Change the default hosting location after listen 3838; in the shiny server configuration file.

sudo nano /etc/shiny-server/shiny-server.conf
location / {
  run_as :HOME_USER:;
  user_dirs;
}

location / {
  run_as shiny;
  site_dir /srv/shiny-server;
  log_dir /var/log/shiny-server;
  directory_index on;
}

Add the lines preserve_logs true; and sanitize_errors false; at the end of the file

Restart and check status

sudo systemctl restart shiny-server
sudo systemctl status shiny-server

Note on UQAR network

The UQAR network doesn't allow connection to a server service based on port number. The solution is to use a reverse proxy, so that nginx will listen on port 80 (default HTTP port) at the URL /shiny and will internally redirect that to port 3838. Same for RStudio - we can have nginx listen at /rstudio and redirect it to port 8787.

With this setup, when hosting a shiny user_dirs; model, the apps will be available at the URL: ip/shiny/user/app Example: http://10.7.48.7/shiny/raphael/sear/

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

Add those line above server {}

map $http_upgrade $connection_upgrade {
  default upgrade;
  ''      close;
}

Add those line after server_name _;

location /shiny/ {
  proxy_pass http://127.0.0.1:3838/;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection $connection_upgrade;
  rewrite ^(/shiny/[^/]+)$ $1/ permanent;
}

location /rstudio/ {
  proxy_pass http://127.0.0.1:8787/;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection $connection_upgrade;
}

Restart and check status

sudo systemctl restart nginx
sudo systemctl status nginx

Sysadmin

This section intend to describe how to mangage the system side of the sear app.

The sear user

When a shiny app is launched, the run_as parameter in the conf file determine the system user as which the app will run. To correctly manage the system side authorization and persistent data storage, we create a super user with the name sear. When arriving on the log in page, it's this user who is responsible for checking the credentials against the user database. It's also this admin user who can create new user with shinymanager. The idea is that when a new user is created with shinymanager, an user is also created on the system. a copy of sear is made in /home/user/ShinyApps/sear and shiny-server has to restart to serve it to the user ... not the best but may be more secure has the user permission would be limited to it's home directory. The alternative would be to log in the user in the system from within shiny ... not sure about the feasibility. a third option is to keep the process run_as sear but to change the working directory to the home user.

In both cases, here is the bash script to initiate the sear user



raphidoc/sear documentation built on April 14, 2025, 2:13 a.m.