Today I upgraded to Shiny 1.3.0 and my Shiny server stopped rendering my apps correctly. Rstudio’s @winston and @jcheng came to my rescue and determined that it was a problem with my nginx proxy settings. That prompted me to write up these notes so next time I have to set up a server, I won’t be starting from scratch.

Install and configure Nginx

Here is the Rstudio article. Here is my corrected nginx.conf file:

/etc/nginx/nginx.conf

#user nobody;
worker_processes  1;
error_log  /data/log/nginx_error.log  debug;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    access_log /data/log/nginx_access.log combined;

    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;

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

        # Open shiny server
        location / {
          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;
          proxy_read_timeout 20d;
          proxy_buffering off;
        }

        # Open shiny server
        location /apps/ {
          proxy_pass http://127.0.0.1:3838/apps/;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
          proxy_read_timeout 20d;
          proxy_buffering off;
        }

        # Rstudio server
        location /editor/ {
          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;
          proxy_read_timeout 20d;
          proxy_buffering off;
        }


        listen      80;
        server_name nycsa0204;

        #charset koi8-r;
        access_log  /data/log/nginx_access.log;
        error_log /data/log/nginx_error.log;
        error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }

}

What I like about the above nginx.conf file is that if I use servername as the URL it will show the shiny index and if I browse to the servername/editor it will open the Rstudio server IDE.

Rstudio server configuration

The only change I made here was to run it under my username:

/etc/rstudio/rserver.conf

# Server Configuration File
server-user=bosr

Shiny server configuration

Shiny runs as user shiny. You can also set where to want your log files:

/etc/shiny-server/shiny-server.conf

# Instruct Shiny Server to run applications as the user "shiny"
run_as shiny;

access_log /data/log/shiny-access.log combined;

sanitize_errors off;

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

  # Define a location at the base URL
  location / {

    simple_scheduler 15;

    # Host the directory of Shiny Apps stored in this directory
    site_dir /data/shiny;

    # Log all Shiny output to files in this directory
    log_dir /data/log;

    # 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;
  }
}

Permissions

My shiny apps are located in /data/shiny, so I want both users (bosr and shiny) to have access to that location. This is done by creating a shiny-apps group and making both users a member of that group. That way I can use Rstudio server to develope apps (as user bosr) and users can run them on Shiny (as user shiny).

sudo groupadd shiny-apps
sudo usermod -aG shiny-apps bosr
sudo usermod -aG shiny-apps shiny
cd /data/shiny
sudo chown -R bosr:shiny-apps .
sudo chmod g+w .
sudo chmod g+s .

I also want to use the Rstudio Server IDE to install packages into the default library folder, so I need to change the ownership and permissions of that folder. Here it is based on my centOS server:

chmod 755 /usr/lib64/R/library
chown -v bosr:shiny-apps /usr/lib64/R/library

You can verify the changes by typing ls -l /usr/lib64/R/library.

To speed up package installs, configure R to use more than one core when compile source code.

/usr/lib64/R/etc/Renviron.site (create the file if it doesn’t already exist)

MAKEFLAGS=-j4

Set up automatic drive mappings as needed

/etc/fstab

# /etc/fstab
# Created by anaconda on Thu Feb  1 07:41:28 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg00-rootvol /                       xfs     defaults        0 0
UUID=ba660186-313b-4482-82aa-23ce7ef7727b /boot                   xfs     defaults        0 0
/dev/mapper/vg00-homevol /home                   xfs     defaults        0 0
/dev/mapper/vg00-tmpvol /tmp                    xfs     defaults        0 0
/dev/mapper/vg00-varvol /var                    xfs     defaults        0 0
/dev/mapper/vg00-crashvol /var/crash              xfs     defaults        0 0
/dev/mapper/vg00-auditvol /var/log/audit          xfs     defaults        0 0
/dev/mapper/vg00-swapvol swap                    swap    defaults        0 0
/dev/mapper/datafs-datafslv1    /data   xfs     defaults       0 0
//(server name)/research                            /media/research cifs credentials=/etc/fstab.cred,fil$

/etc/fstab.cred

Instead of putting your password in the fstab file, put it in a separate credential file with the proper premissions. They you only have to change the password in one place every time to change it.

username=bosr
password=(secret)
domain=(domain)

Troubleshooting

If you want to downgrade to an older version of a package, here is any easy way to do so:

library(devtools)
install_version("shiny", version = "1.2.0", repos = "http://cran.us.r-project.org")

If you want to start R without any automatic loading of packages:

R -vanilla