This is detailed description of how to set-up a Shiny server (community edition) on an AWS EC2 instance, behind an OAuth2 security layer (Google OAuth2 in this case, but it can be something else).
- A Google Developer account
- An Amazon Web Services account
- a VPC with a public subnet
- A domain (or subdomain, or wildcard domain) certificate bundle and key files.
- Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
- From the Amazon EC2 console dashboard, choose Launch Instance.
- On the Choose an Amazon Machine Image (AMI) page, choose the Amazon Linux AMI.
- On the Choose an Instance Type page, select the hardware configuration and size of the instance to launch. Choose something larger than a t2.medium.
- On the Configure Instance Details page, choose a VPC and a corresponding public subnet. (You have to know in advance which of the subnets is public http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/vpc-ip-addressing.html#vpc-public-ip-addresses)
- On the Configure Security Group page, use security groups that allow you at least SSH access to you and world wide access to HTTPS.
Make sure you have access to the private key that is used to connect to the instance. This needs to be loaded in your SSH agent.
- Open the Amazon VPC console at https://console.aws.amazon.com/vpc/.
- In the navigation pane, choose Elastic IPs.
- Choose Allocate New Address, and then Yes, Allocate.
- Select the Elastic IP address from the list, choose Actions, and then choose Associate Address.
- In the dialog box, choose Instance from the Associate with list, and then select your instance from the Instance list. Choose Yes, Associate.
The two certificate files (bundle certificate and private key) need to be placed on the server. Assuming you are in a folder that conatins both of them, you can easily scp
them to the future Shiny server. Replace x.x.x.x
with the Elastic IP you have previusly created.
scp cert.bundle [email protected]:
scp cert.key [email protected]:
nginx is the first reverse proxy used. It is nginx which will redirect all traffic to a second reverse proxy.
To execute this step, you will first need to be logged in the EC2 instance via SSH
. The user you should be using is ec2-user and you must start from the home folder /home/ec2-user
.
sudo yum -y install nginx
wget https://gist.github.com/cosmincatalin/221d9c563ca3cad6ce09a70e87d2ac62/raw/nginx.conf
.- Dont' forget to modify the
server_name shiny.example.com
in this file after downloading it, sovim nginx.conf
. wget https://gist.github.com/cosmincatalin/221d9c563ca3cad6ce09a70e87d2ac62/raw/nginx.sh
sudo bash nginx.sh
This is why you're here, right?
wget https://gist.github.com/cosmincatalin/221d9c563ca3cad6ce09a70e87d2ac62/raw/shiny.sh
.- Modify Shiny server version if you want. So
vim shiny.sh
sudo bash shiny.sh
Create an A record with your DNS provider so that you point the domain name to the Elastic IP. If this is done correctly you should be able to see something working when accessing your domain similar to https://shiny.example.com.
You now need to setup a Google OAuth2 client. This part of the guide was shamelessly copied from https://github.com/bitly/oauth2_proxy#google-auth-provider.
- Create a new project: https://console.developers.google.com/project
- Choose the new project from the top right project dropdown (only if another project is selected)
- In the project Dashboard center pane, choose "Enable and manage APIs"
- In the left Nav pane, choose "Credentials"
- In the center pane, choose "OAuth consent screen" tab. Fill in "Product name shown to users" and hit save.
- In the center pane, choose "Credentials" tab.
- Open the "New credentials" drop down
- Choose "OAuth client ID"
- Choose "Web application"
- Application name is freeform, choose something appropriate
- Authorized JavaScript origins is your domain ex: https://shiny.example.com
- Authorized redirect URIs is the location of oath2/callback ex: https://shiny.example.com/oauth2/callback
- Choose "Create"
- Take note of the Client ID and Client Secret
This is the second reverse proxy to which nginx is communicating with. It is this proxy that ensures security and authentication.
wget https://gist.github.com/cosmincatalin/221d9c563ca3cad6ce09a70e87d2ac62/raw/oauth2_proxy.cfg
- You will need to edit the
oauth2_proxy.cfg
by modifying the following: client_id
which can be found in Google Cloud Console > API Manager > Credentials > Your OAuth2 ClientID > Client IDclient_secret
which can be found in Google Cloud Console > API Manager > Credentials > Your OAuth2 ClientID > Client secretcookie_secret
something random and decently sized.email_domains
this how you restrict access to allow only users having emails under specific domains.wget https://gist.github.com/cosmincatalin/221d9c563ca3cad6ce09a70e87d2ac62/raw/oauth2_proxy.sh
sudo bash oauth2_proxy.sh &
That's it, you're done. Open https://shiny.example.com and you should be asked to authenticate. After a successful authentication, you should be able to see a working sample page from the Shiny server. You can of course choose to configure oauth2_proxy with other credential providers, too.
If you have questions, you can always reach me via email.
Take a look at my other R
elated gists: