%%{init: {'theme': 'base', 'themeVariables': {'fontFamily': 'monospace', 'fontSize':'13px'}}}%%
flowchart TD
Browser(["<strong>Browser</strong>"])
SG{"Security group<br/><em>port 80 from our IP</em>"}
subgraph Instance["<strong>EC2 instance</strong>"]
Nginx("<strong>nginx</strong><br/>:80")
Welcome("Welcome page<br/><code>/</code>")
RS("<strong>RStudio Server</strong><br/>127.0.0.1:8787")
JH("<strong>JupyterHub</strong><br/>127.0.0.1:8001")
App("<strong>baseball-trajectory</strong><br/>127.0.0.1:8000")
end
Browser --> SG --> Nginx
Nginx --"<code>/</code>"--> Welcome
Nginx --"<code>/rstudio/</code>"--> RS
Nginx --"<code>/jupyter/</code>"--> JH
Nginx --"<code>/trajectory/</code>"--> App
style Browser fill:#E8A33D,stroke:#000,stroke-width:1px,color:#000
style SG fill:#D2562B,stroke:#fff,stroke-width:1px,color:#fff
style Nginx fill:#2A6F77,stroke:#fff,stroke-width:1px,color:#fff
style Welcome fill:#666,stroke:#999,stroke-width:1px,color:#fff
style RS fill:#1B2A41,stroke:#fff,stroke-width:1px,color:#fff
style JH fill:#1B2A41,stroke:#fff,stroke-width:1px,color:#fff
style App fill:#5B8C5A,stroke:#fff,stroke-width:1px,color:#fff
Accessibility
How do we reach RStudio Server, JupyterHub, and baseball-trajectory on the same instance without opening a port for each one? The answer from Lab 12 is a proxy. In this chapter we install the two new services, keep every port but 80 closed to the outside, and put nginx in front. RStudio Server lives at /rstudio, JupyterHub at /jupyter, and the app from Installing a Real Application at /trajectory.
What we’re building
Only port 80 opens in the security group. nginx listens there and reads the path of each request. It forwards /rstudio/ to RStudio Server on 8787, /jupyter/ to JupyterHub on 8001, and /trajectory/ to the app on 8000. Everything else gets the nginx welcome page:
baseball-trajectory already holds 8000, and JupyterHub uses 8000 by default. JupyterHub moves to 8001 so both can run. In Installing a Real Application only we could see the app, through an SSH tunnel; here anyone the security group allows can reach it. Configure SSL later moves it to 443.
Logging into the instance
We use the same AL2023 instance from Instance Size. It already has R 4.6.1 (from rig) and Python 3.12.1 in /opt. Start it, look up the new IP, and connect the same way as in Installing a Real Application, or with awsdev from SSH config.
The instance is a t3.small, which we kept for this chapter (see Staying on a t3.small). RStudio Server and JupyterHub add two more services that need memory, so check what’s free before we start:
free -hA user to log in with
RStudio Server and JupyterHub both log users in with their Linux username and password. ec2-user has no password (we log in with a key), so create a user for the lab and give it one:
sudo useradd -m datasci
sudo passwd datasci-m creates /home/datasci, which both services need for the user’s files and sessions.
Installing RStudio Server
Posit doesn’t build RStudio Server for Amazon Linux. We use the RHEL 9 package, for the same reason rig could use the RHEL 9 build of R: AL2023 is close enough to RHEL 9. Copy the current RHEL 9 download link from the RStudio Server download page and download it:
cd /tmp
curl -LO https://download2.rstudio.org/server/rhel9/x86_64/rstudio-server-rhel-VERSION-x86_64.rpmdnf installs the package and the libraries it depends on:
sudo dnf install -y ./rstudio-server-rhel-VERSION-x86_64.rpmThe package installs a systemd unit and starts it. Check that it’s running:
sudo systemctl status rstudio-serverKeeping RStudio Server private
RStudio Server listens on every address by default. We want it reachable only through nginx, the same way baseball-trajectory binds to 127.0.0.1. Open its config file:
sudo vim /etc/rstudio/rserver.confAdd one line:
www-address=127.0.0.1Restart RStudio Server to load the change:
sudo systemctl restart rstudio-serverWe could also set www-root-path=/rstudio here. We don’t need to, because nginx sends the same information in a header (see below).
Installing JupyterHub
JupyterHub has two parts. The hub logs users in and starts a notebook server for each one. configurable-http-proxy, a Node.js program, sits in front and routes each user’s traffic to their server. That means JupyterHub needs Node.js as well as Python:
sudo dnf install -y nodejs npm
sudo npm install -g configurable-http-proxynpm install -g puts the proxy in /usr/local/bin. Check that it’s on the PATH:
which configurable-http-proxyThe Python side gets its own virtual environment in /opt, built from the /opt Python just like baseball-trajectory:
sudo /opt/python/3.12.1/bin/python3.12 -m venv /opt/jupyterhub/.venv
sudo /opt/jupyterhub/.venv/bin/pip install jupyterhub jupyterlabConfiguring JupyterHub
JupyterHub writes a default config file that lists every setting, all commented out. Generate it in /etc/jupyterhub:
sudo mkdir -p /etc/jupyterhub
cd /etc/jupyterhub
sudo /opt/jupyterhub/.venv/bin/jupyterhub --generate-configOpen the file and add three settings at the top:
sudo vim /etc/jupyterhub/jupyterhub_config.pyc.JupyterHub.bind_url = 'http://127.0.0.1:8001/jupyter'
c.Spawner.cmd = ['/opt/jupyterhub/.venv/bin/jupyterhub-singleuser']
c.Spawner.default_url = '/lab'- 1
-
The address, port, and path JupyterHub serves from.
127.0.0.1keeps it private,8001avoidsbaseball-trajectory, and/jupytermatches the path innginx. - 2
- The program that starts each user’s notebook server, with its full path into the venv.
- 3
- Users land in JupyterLab instead of the classic notebook page.
The path in bind_url is the setting the chapter warns about. JupyterHub writes its links relative to that path. Without /jupyter, the login page loads but every link on it points at / and gets the nginx welcome page instead.
Running JupyterHub as a service
JupyterHub doesn’t ship a unit file, so we write one, the same way we did for baseball-trajectory in Installing a Real Application:
sudo vim /etc/systemd/system/jupyterhub.service[Unit]
Description=JupyterHub
After=network.target
[Service]
User=root
Environment="PATH=/opt/jupyterhub/.venv/bin:/usr/local/bin:/usr/bin:/bin"
WorkingDirectory=/etc/jupyterhub
ExecStart=/opt/jupyterhub/.venv/bin/jupyterhub -f /etc/jupyterhub/jupyterhub_config.py
Restart=on-failure
[Install]
WantedBy=multi-user.target- 1
-
The hub runs as
rootbecause it has to start each user’s server as that user. This is the opposite ofbaseball-trajectory, which runs as its own service account. - 2
-
The
PATHhas to include/usr/local/bin, or the hub can’t findconfigurable-http-proxy. - 3
- The hub writes its cookie secret and user database here.
Load the unit, then enable and start it:
sudo systemctl daemon-reload
sudo systemctl enable --now jupyterhub
sudo systemctl status jupyterhubConfiguring nginx
AL2023’s nginx package already has a server block for port 80 in /etc/nginx/nginx.conf. It serves the welcome page, and it reads any file in /etc/nginx/default.d/. That means we don’t have to replace nginx.conf, as the chapter does on Ubuntu. We add two small files instead. Install nginx first:
sudo dnf install -y nginxConfirm the default server block includes default.d:
grep -n 'listen\|default.d' /etc/nginx/nginx.confThe WebSocket map
RStudio and JupyterLab both keep a WebSocket open to the browser (see Configure SSL). nginx only passes it through when the request asks for an upgrade. A map sets that header for us, and it has to live in the http block, which reads every file in conf.d/:
sudo vim /etc/nginx/conf.d/websocket.confmap $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}The three locations
The location blocks go in default.d/, so they join the existing port 80 server:
sudo vim /etc/nginx/default.d/lab12.conflocation /rstudio/ {
rewrite ^/rstudio/(.*)$ /$1 break;
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_set_header X-RStudio-Root-Path /rstudio;
proxy_set_header Host $host:$server_port;
}
location /jupyter/ {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
location /trajectory/ {
proxy_pass http://127.0.0.1:8000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
}- 1
-
RStudio Server expects paths that start at
/, songinxstrips/rstudiobefore passing the request on. - 2
- Keeps an idle RStudio session’s connection open instead of dropping it after 60 seconds.
- 3
-
Tells RStudio Server it lives at
/rstudio, so the links it writes include the prefix. - 4
-
No
rewritehere. JupyterHub already serves from/jupyter(itsbind_url), so the path passes through unchanged. - 5
-
The trailing
/onproxy_passstrips/trajectory/before the request reaches the app. The app thinks it lives at/, and it works because Shiny writes its links relative to the page.
Each service handles the path its own way. RStudio Server gets the prefix stripped and a header telling it where it lives. JupyterHub keeps the prefix because its own config already expects it. baseball-trajectory gets the prefix stripped and needs nothing else, so its unit file stays as it is.
Testing and starting nginx
nginx -t checks every file for errors before we start the server:
sudo nginx -tEnable and start nginx:
sudo systemctl enable --now nginxss shows every service and the address it listens on. Only nginx should listen on all addresses (0.0.0.0:80); the rest stay on 127.0.0.1:
sudo ss -tlnpBefore touching the security group, test all three paths from the instance itself. curl -I asks for the headers only:
curl -I http://127.0.0.1/rstudio/
curl -I http://127.0.0.1/jupyter/
curl -I http://127.0.0.1/trajectory/A 302 is a good sign for the first two. RStudio Server and JupyterHub redirect a visitor who isn’t logged in to their sign in page. The app has no login, so it should answer 200 OK, the same as the curl -I in Installing a Real Application.
Opening port 80
From outside, the request still hangs. The security group only allows port 22. Add a rule for port 80 from our local machine:
aws ec2 authorize-security-group-ingress \
--group-name dev-sg \
--protocol tcp --port 80 \
--cidr YOUR_IP/32 \
--profile dev-deployThe chapter opens port 80 to everyone (0.0.0.0/0). We limit it to our own IP, the same as the SSH rule. This site is plain HTTP, so the datasci password crosses the internet unencrypted until Configure SSL adds HTTPS.
Trying it out
Open http://YOUR_PUBLIC_IP_ADDRESS/ in a browser. The nginx welcome page confirms the proxy and the security group work. Then try each path:
http://YOUR_PUBLIC_IP_ADDRESS/rstudio/: RStudio Server’s sign in page. Sign in asdatasci.http://YOUR_PUBLIC_IP_ADDRESS/jupyter/: JupyterHub’s login page. Sign in asdatasci, and JupyterLab opens.http://YOUR_PUBLIC_IP_ADDRESS/trajectory/:baseball-trajectory, with no SSH tunnel this time.
Type http:// in full. Some browsers try https:// first, and nothing listens on 443 yet. Keep the trailing / too, since each location block matches a path that ends in /.
With both sessions open, check memory again:
free -hRecap
One open port can serve several services, including the app we could only reach through an SSH tunnel before.
nginxlistens on80and sends each path to a service that only listens on127.0.0.1.Each service needs to know its path. RStudio Server learns it from the
X-RStudio-Root-Pathheader, and JupyterHub learns it frombind_url.baseball-trajectorynever needs to know, becausenginxstrips the prefix and Shiny’s links are relative.On AL2023, the
locationblocks go in/etc/nginx/default.d/and the WebSocketmapgoes in/etc/nginx/conf.d/, songinx.confstays untouched.The new security group rule opens port
80to our IP only, because plain HTTP sends passwords unencrypted. Configure SSL adds HTTPS.