{"id":205,"date":"2022-01-11T03:01:57","date_gmt":"2022-01-11T03:01:57","guid":{"rendered":"https:\/\/ap.pstek.nl\/pstek_wp\/blog\/?p=205"},"modified":"2022-07-11T13:27:01","modified_gmt":"2022-07-11T13:27:01","slug":"https-for-self-hosted-r-studio-and-r-shiny-apps","status":"publish","type":"post","link":"https:\/\/ap.pstek.nl\/pstek_wp\/2022\/https-for-self-hosted-r-studio-and-r-shiny-apps\/","title":{"rendered":"Https for Self-Hosted R Studio and R Shiny Apps"},"content":{"rendered":"\n
One of the wonderful things about R Studio (and R Shiny) is that you can host them on your own Linux server (together with Lime Survey<\/a>, for example). This allows you to access these resources from different locations, let’s you run them for hours or days in the background, and let’s you share them with others; all essentially for free.<\/p>\n\n\n\n However the R Studio server installation<\/a> is typically only accessible from port 8787 (R Shiny from port 3838), which cannot be secured using SSL services like Let’s Encrypt<\/a> (which give you the https<\/strong> in the URL). <\/p>\n\n\n\n Let’s Encrypt only works on port 80, the “normal” web surfing port. So should you just accept an unencrypted connection to your R installation? Certainly not!<\/p>\n\n\n\n Especially because an unencrypted connection is much easier to hack (including stealing your login and password), and if you download data from your R server, that would be unencrypted too. It’s a risk you probably shouldn’t take, especially if you’re dealing with sensitive data, like personal information.<\/p>\n\n\n\n To use Let’s Encrypt, first setup a virtual host (learn more about that here for Apache<\/a>). Just as you can setup virtual host for a normal website, you can do the same for R Studio, but with some adjustment<\/a>. An example of the virtual host code (your.subdomain.here<\/strong>) is shown below. Just replace with your subdomain, and on an Apache webserver (running on Ubuntu 20 in my case), it should work just fine.<\/p>\n\n\n\n For a Shiny server you should replace the 8787 port in the virtual host code above with 3838. If you do face issues, maybe also check here<\/a>.<\/p>\n\n\n\n Once you’ve succeeded setting up the virtual server, enable it…<\/p>\n\n\n\n And then check to make sure it is properly installed with:<\/p>\n\n\n\n It is possible that after this step, you receive an error about the “RewriteEngine”-part of the code. In this case, be sure the enable the rewrite-function in Apache using:<\/p>\n\n\n\n<VirtualHost *:80>\n ServerName your.subdomain.here\n\n# Specify path for Logs\n ErrorLog ${APACHE_LOG_DIR}\/error.log\n CustomLog ${APACHE_LOG_DIR}\/access.log combined\n\n RewriteEngine on\n\n# Rewrite the url supplied to ensure https is applied\n RewriteCond %{SERVER_NAME} =your.subdomain.here\n\n# Following lines should open rstudio directly from the url\n RewriteCond %{HTTP:Upgrade} =websocket\n RewriteRule \/(.*) ws:\/\/localhost:8787\/$1 [P,L]\n RewriteCond %{HTTP:Upgrade} !=websocket\n RewriteRule \/(.*) http:\/\/localhost:8787\/$1 [P,L]\n ProxyPass \/ http:\/\/localhost:8787\/\n ProxyPassReverse \/ http:\/\/localhost:8787\/\n\n RewriteRule ^ https:\/\/%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]\n\n<\/VirtualHost><\/code><\/pre>\n\n\n\n
sudo a2ensite your.subdomain.here<\/code><\/pre>\n\n\n\n
sudo apache2ctl configtest<\/code><\/pre>\n\n\n\n
sudo a2enmod rewrite<\/code><\/pre>\n\n\n\n