#!/bin/bash ################################################################################ # Script: jupyter-notebook.sh # Author: Eric Ruzanski # Description: This script installs and configures the classic Jupyter Notebook, # serves it securely via an Nginx reverse proxy, and makes it # accessible through a web browser via the provided domain or # default reverse DNS (rDNS) of the Linode. If the Linode is ever # rebooted, or Jupyter Notebook stops running, simply start # Jupyter Notebook from the command line using 'jupyter notebook &'. # # GitHub Repository: # https://github.com/ericruzanski/StackScripts/blob/main/jupyter-notebook.sh # # Jupyter Notebook Docs: # https://jupyter-notebook.readthedocs.io/en/latest/ # # Disclaimer: This script is provided as-is without any warranties. ################################################################################ ## Jupyter Notebook Settings # # ## Linode/SSH Security Settings # # # # ## Domain Settings # # # ## Enable logging set -x exec > >(tee /dev/ttyS0 /var/log/stackscript.log) 2>&1 ## Import the Bash StackScript Library source ## Import the DNS/API Functions Library source ## Import the OCA Helper Functions source ## Run initial configuration tasks (DNS/SSH stuff, etc...) source ## Register default rDNS export DEFAULT_RDNS=$(dnsdomainname -A | awk '{print $1}') ## Set absolute domain if any, otherwise use DEFAULT_RDNS if [[ $DOMAIN = "" ]]; then readonly ABS_DOMAIN="$DEFAULT_RDNS" elif [[ $SUBDOMAIN = "" ]]; then readonly ABS_DOMAIN="$DOMAIN" else readonly ABS_DOMAIN="$SUBDOMAIN.$DOMAIN" fi create_a_record $SUBDOMAIN $IP $DOMAIN ## Update system, set hostname & install basic security set_hostname apt_setup_update ufw_install fail2ban_install ## Add UFW rules ufw allow http ufw allow https ufw allow 8888 ## Prepare the Python venv apt-get install python3-venv python3-pip -y mkdir /root/jupyter-notebook mkdir /opt/notebooks python3 -m venv /root/jupyter-notebook source /root/jupyter-notebook/bin/activate python3 -m pip install notebook # Configure Jupyter Notebook jupyter notebook --generate-config CONFIG_FILE="/root/.jupyter/jupyter_notebook_config.py" HASHED_PASSWORD=$(python3 -c "from jupyter_server.auth import passwd; print(passwd('$NOTEBOOK_PASSWORD'))") sudo tee -a $CONFIG_FILE < /etc/nginx/sites-available/reverse-proxy.conf server { listen 80; server_name ${ABS_DOMAIN}; access_log /var/log/nginx/reverse-access.log; error_log /var/log/nginx/reverse-error.log; location /wss/ { proxy_pass http://127.0.0.1:8888; proxy_http_version 1.1; proxy_buffering off; proxy_set_header Upgrade \$http_upgrade; proxy_set_header Connection "Upgrade"; proxy_read_timeout 86400; } location /api/kernels/ { proxy_pass http://127.0.0.1:8888; proxy_http_version 1.1; proxy_buffering off; proxy_set_header Upgrade \$http_upgrade; proxy_set_header Connection "Upgrade"; proxy_read_timeout 86400; } location /terminals/ { proxy_pass http://127.0.0.1:8888; proxy_http_version 1.1; proxy_buffering off; proxy_set_header Upgrade \$http_upgrade; proxy_set_header Connection "Upgrade"; proxy_read_timeout 86400; } location / { proxy_pass http://127.0.0.1:8888; } } END ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf # Enable and start NGINX systemctl enable nginx systemctl restart nginx sleep 90 ## Configure SSL apt-get install python3-certbot-nginx -y certbot run --non-interactive --nginx --agree-tos --redirect -d ${ABS_DOMAIN} -m ${SOA_EMAIL_ADDRESS} -w /var/www/html/ ## Cleanup stackscript_cleanup ## Start Jupyter Notebook source /root/jupyter-notebook/bin/activate jupyter notebook