Skip to content

Instantly share code, notes, and snippets.

@zhuliguang
Forked from yangj1e/notebook.sh
Last active September 5, 2016 04:27
Show Gist options
  • Select an option

  • Save zhuliguang/52b7d6fb63ab5dc133caaad32eec5dc3 to your computer and use it in GitHub Desktop.

Select an option

Save zhuliguang/52b7d6fb63ab5dc133caaad32eec5dc3 to your computer and use it in GitHub Desktop.
Deploy Jupyter Notebook server
#!/usr/bin/env bash
# CHANGE THIS FOR YOUR OWN DOMAIN NAME AND EMAIL
email=YOUREMAIL@MAILBOX.COM
domain=YOURDOMAIN.COM
# Install anaconda and some other packages I often use
wget https://repo.continuum.io/archive/Anaconda3-4.1.1-Linux-x86_64.sh
bash Anaconda3-4.1.1-Linux-x86_64.sh -b
echo 'PATH="/home/ubuntu/anaconda3/bin:$PATH"' >> ~/.bashrc
export PATH=$PATH:/home/ubuntu/anaconda3/bin
cd ~
sudo chown -R $USER /home/ubuntu/anaconda3
sudo chmod -R +x /home/ubuntu/anaconda3
# Install pip if you need to
# sudo apt-get install -y python3-pip
conda install scipy -y
conda install bokeh -y
conda install scikit-learn -y
conda install -c -y anaconda basemap=1.0.7
# necessary libs for seaborn and seaborn itself
sudo apt-get install -y libqtgui4
sudo apt-get install -y libsm6
sudo apt-get install -y libxrender1
conda install -y seaborn
# pip3 install ggplot # still have problems. will explore in the future
#R kernal for jupyter
conda install -y -c r r-essentials
# Generate notebook configuation
jupyter notebook --generate-config
#
# From here you have two choices.
# Either use openssl to get a self-signed certificate
#
cd ~
mkdir certs
cd certs
certdir=$(pwd)
# uses openssl
# openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout privkey.pem -out cert.pem
#
# Or use letsencrypt, but you have to renew your certificate every 90 days
#
git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt
sudo ./letsencrypt-auto certonly --standalone --agree-tos --redirect --duplicate --text --email $email -d $domain
sudo cat /etc/letsencrypt/live/$domain/privkey.pem > ../privkey.pem
sudo cat /etc/letsencrypt/live/$domain/cert.pem > ../cert.pem
key=$(python3 -c "from notebook.auth import passwd; print(passwd())")
# add to jupyter configuation file
cd ~
sed -i "1 a\
c = get_config()\\
c.NotebookApp.certfile = u'$certdir/cert.pem'\\
c.NotebookApp.keyfile = u'$certdir/privkey.pem'\\
c.NotebookApp.ip = '*'\\
c.NotebookApp.open_browser = False\\
c.NotebookApp.password = u'$key'\\
c.NotebookApp.port = 8888" .jupyter/jupyter_notebook_config.py
# make ubuntu the owner of ~/.local/
sudo chown $USER -R ~/.local/
# Redirect all port 443 traffic to port 8888 (so that you can use default https)
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8888
# To delete this rule
# iptables -t nat --line-numbers -n -L
# iptables -t nat -D PREROUTING [number#]
echo 'cd ~/notebook' >> ~/.bashrc
echo 'jupyter notebook' >> ~/.bashrc
#running the server with tmux
mkdir notebook
cd notebook
tmux new -s nb
#!/bin/sh
email=leon.l.zhu@gmail.com
domain=liguangzhu.info
cd ~/certs/letsencrypt
sudo rm -f /etc/letsencrypt/live/$domain/privkey.pem
sudo rm -f /etc/letsencrypt/live/$domain/cert.pem
sudo ./letsencrypt-auto certonly --standalone --agree-tos --redirect --duplicate --text --email $email -d $domain
sudo cat /etc/letsencrypt/live/$domain/privkey.pem > ~/certs/privkey.pem
sudo cat /etc/letsencrypt/live/$domain/cert.pem > ~/certs/cert.pem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment