建立自己的 Jupyter Notebook 伺服器

如果你有自己的伺服器且不想要將自己的數據放在公有的雲上面,此時你可以安裝 Jupyter Notebook 在自己的伺服器上面,以下以 Linux CentOS 作為範例,如果想要安裝 python3 到 CentOS 上面也可以參考連結

yum update
yum upgrade
yum install python-pip3
pip3 install jupyter

啟動 Jupyter Notebook 使用:

jupyter notebook

有時候在啟動 jupyter notebook 的時候就已經會出現以下的問題:

Traceback (most recent call last):
  File "/usr/local/bin/jupyter-notebook", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/site-packages/jupyter_core/application.py", line 268, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/traitlets/config/application.py", line 663, in launch_instance
    app.initialize(argv)
  File "</usr/local/lib/python3.6/site-packages/decorator.py:decorator-gen-7>", line 2, in initialize
  File "/usr/local/lib/python3.6/site-packages/traitlets/config/application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/notebook/notebookapp.py", line 1716, in initialize
    self.init_resources()
  File "/usr/local/lib/python3.6/site-packages/notebook/notebookapp.py", line 1420, in init_resources
    resource.setrlimit(resource.RLIMIT_NOFILE, (soft, hard))
ValueError: not allowed to raise maximum limit

遇到這個情況需要改動 ulimit 到類似以下的設定值:

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 31202
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 4096
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

 

如果你有固定 IP 且想要透過遠端存取(適合買 VPS 的用戶),則需要設定密碼,你可以使用以下指令,或是到 /home/users/.jupyter 資料夾中修改 jupyter_notebook_config.json 檔案。

jupyter notebook --generate-config
jupyter notebook password

設定固定 IP 與接入 port:

jupyter notebook --ip=xxx.xxx.xxx.xxx --port=5000

如果想要取消所有安全設定可以使用以下:

jupyter notebook --ip='*' --NotebookApp.token='' --NotebookApp.password=''

設定自己想要的 Jupyter Notebook 主題:

pip3 install --upgrade jupyterthemes
jt -t oceans16 -tf merriserif -tfs 10 -fs 12 -nf ptsans -nfs 13 -T -N -f roboto

最後如果要在 Linux 端持續執行,建議使用 Linux Screen 功能,以下兩個指令分別是開啟一個新視窗與查詢現在存在的視窗

screen 
screen -ls
------------------------------------------
There is a screen on:
 4308.pts-0.vmi233311 (Detached)
1 Socket in /var/run/screen/S-yuting.

如果想要接入 4308.pts-0.vmi233311 視窗的話可以執行

screen -r 4308

進入視窗想要退出則是按下

Ctrl+a -> d

嘗試登入已經架好的 Jupyter Notebook.