jupyter notebook 安装及远程访问
由于本人喜欢pip管理包工具,因为安装jupyter notebook采用pip进行安装了。以下都是在python3.7版本下的操作。
一、本地访问的配置方法:
1.把pip升级到最新版本
pip install --upgrade pip
2.安装Jupyter Notebook
pip install jupyter
3.运行Jupyter Notebook
1)默认启动
jupyter notebook
执行命令之后,在终端中将会显示一系列notebook的服务器信息,同时浏览器将会自动启动Jupyter Notebook。
启动过程中终端显示内容如下:
$ jupyter notebook [I 08:58:24.417 NotebookApp] Serving notebooks from local directory: /Users/catherine [I 08:58:24.417 NotebookApp] 0 active kernels [I 08:58:24.417 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/ [I 08:58:24.417 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
- 注意:之后在Jupyter Notebook的所有操作,都请保持终端不要关闭,因为一旦关闭终端,就会断开与本地服务器的链接,你将无法在Jupyter Notebook中进行其他操作啦。
如果你同时启动了多个Jupyter Notebook,由于默认端口“8888”被占用,因此地址栏中的数字将从“8888”起,每多启动一个Jupyter Notebook数字就加1,如“8889”、“8890”……
二、远程访问的配置方法:
安装好以后直接输入jupyter notebook便可以在浏览器中使用。但是它默认只能在本地访问,如果想把它安装在服务器上,然后在本地远程访问,则需要进行如下配置:
1. 登陆远程服务器
2. 生成配置文件
jupyter notebook --generate-config
随便开一个python的命令窗体,创建一个密文的密码:
In [1]: from notebook.auth import passwd In [2]: passwd() Enter password: Verify password: Out[2]: 'sha1:ce23d945972f:34769685a7ccd3d08c84a18c63968a41f1140274'
把生成的密文‘sha:ce…’复制下来
4. 修改默认配置文件
vim ~/.jupyter/jupyter_notebook_config.py
进行如下修改:
c.NotebookApp.ip = '*' #表示监听全部地址 c.NotebookApp.password = u'sha1:fac6a5e2226f:8a3008541dac4b356487b6cef8b45d0bf9aec4ca' # 就是上面拷贝的密码。 c.NotebookApp.open_browser = False c.NotebookApp.port = 8888 #随便指定一个端口 c.NotebookApp.allow_root =True #允许root用户 c.NotebookApp.allow_remote_access = True #必须有这项,不能远程客户机无法访问。 c.NotebookApp.notebook_dir = '/root/ipython' #工作目录 说明:要提前在root在新建一个目录ipython作为工作目录。
5. 启动jupyter notebook:
采用后台运行方式:
nohup jupyter notebook 2>&1 | cat>/dev/null &
6. 远程访问
此时应该可以直接从本地浏览器直接访问http://address_of_remote:8888
就可以看到jupyter的登陆界面。
三、采用Nginx反向代理的配置方法:
jupyter配置:
c.NotebookApp.ip = '127.0.0.1' #只用监听本地IP c.NotebookApp.password = u'sha1:fac6a5e2226f:8a3008541dac4b356487b6cef8b45d0bf9aec4ca' # 就是上面拷贝的密码。 c.NotebookApp.open_browser = False c.NotebookApp.port = 8888 #随便指定一个端口 c.NotebookApp.allow_root =True #允许root用户 #c.NotebookApp.allow_remote_access = True #不让远程客户机访问。 c.NotebookApp.notebook_dir = '/root/ipython' #工作目录
nginx 配置:
upstream notebook { server 127.0.0.1:8888; #jupyter监听的端口。 } server { listen 80; server_name py.xxx.com; location / { proxy_pass http://notebook; proxy_set_header Host $host; proxy_set_header X-Real-Scheme $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #终端采用的是WebSocket,所以需要配置如下 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 120s; proxy_next_upstream error; } }
最后请注意一点:
请用pip list查看一下tornado的版本是不是5.1.1,截至目前,如果用tornado-6.0.1版本,jupyter就会出现连接不到python3服务器的问题(客户端网页可以进去)。所以一定要用tornado-5.1.1版本的