{"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

<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

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

sudo a2ensite your.subdomain.here<\/code><\/pre>\n\n\n\n

And then check to make sure it is properly installed with:<\/p>\n\n\n\n

sudo apache2ctl configtest<\/code><\/pre>\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

sudo a2enmod rewrite<\/code><\/pre>\n\n\n\n

After that there should be no more errors. Finally, run Let’s Encrypt (assuming it’s already installed, else see here<\/a> how and what you should install).<\/p>\n\n\n\n

sudo certbot --apache<\/code><\/pre>\n\n\n\n

Follow the brief questions, and that’s it! Well done, your connection to your R Studio server installation is now encrypted. Safe surfing everyone!<\/p>\n\n\n\n

(Note: this post was updated on 10 July 2022. Some corrections were made to the code and need to activate Apache rewrite plugin.<\/em>)<\/p>\n","protected":false},"excerpt":{"rendered":"

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, 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 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[85],"tags":[34,43,65,51],"class_list":["post-205","post","type-post","status-publish","format-standard","hentry","category-tutorial","tag-linux","tag-r","tag-r-studio","tag-ssl"],"_links":{"self":[{"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/posts\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/comments?post=205"}],"version-history":[{"count":5,"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/posts\/205\/revisions"}],"predecessor-version":[{"id":325,"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/posts\/205\/revisions\/325"}],"wp:attachment":[{"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/media?parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/categories?post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/tags?post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}