ubuntu下设置jupyter notebook 2017年07月29日 19:28:34 小旋锋 阅读数:8329 标签: ubuntu 更多 个人分类: python 二三事 来源:http://blog.csdn.net/suzyu12345/article/details/51037905 Ipython Notebook现在已经改名为Ipython jupyter,是最知名最好用的

ubuntu下设置jupyter notebook

来源:http://blog.csdn.net/suzyu12345/article/details/51037905

Ipython Notebook现在已经改名为Ipython jupyter,是最知名最好用的Python数据分析工具。
下面讲讲怎么在Linux下安装ipython jupyter,以及远程访问,我这里是在虚拟机中配置ipython,windows访问虚拟机中的ipython jupyter。

1. 安装anaconda

anaconda是目前python数据分析最好用的发行版,集成了很多常用的数据分析模块,如果是自己安装python环境,坑很多的。
在linux下安装也很简单,将anaconda.sh上传到linux后,执行 bash anaconda.sh,根据提示安装即可,最后一部是询问是否将python加入环境变量,选择yes,当然你也可以在安装完后手动添加到bash中。
下面的安装完后的.bashrc添加的python环境变量:

# added by Anaconda3 4.2.0 installer
export PATH="/home/zhenyu/anaconda3/bin:$PATH"

2. 配置ipython jupyter

2.1.生成配置文件

# 生成配置文件
jupyter notebook --generate-config
# 此时生成配置文件:
# Writing default config to: /home/zhenyu/.jupyter/jupyter_notebook_config.py

# 创建登录密码
# 打开ipython,生成密钥
$ ipython
from notebook.auth import passwd
passwd()
Enter password:
Verify password:
Out[2]: 'sha1:6f6193fcfbd5:614c4ba185334868fc8bbce2e9890b3ef7d1a79b'  
# 我这里创建的密码是123456,对应的密钥是sha1xxxx的那一串
# 然后退出ipython

2.2.创建自签名的证书

使用openssl创建一个自签名证书,由于是自签名所以浏览器会提示警告,选择信任exception即可。如果不想引起警告,需具备合格证compliant certificate,参考[http://arstechnica.com/security/2009/12/how-to-get-set-with-a-secure-sertificate-for-free/]
如果是内网访问不担心安全问题,不使用ssl速度会快一些。

# 在linux下执行,遇到询问的地方一路回车即可
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

# 会在当前文件夹下生成 mycert.pem,我将它移到.jupyter/secret文件夹下面,方便管理
# 先创建.secret文件夹
cd .jupyter
mkdir secret  
# 移动
cd ~
mv mycert.pem .jupyter/secret/

2.3.修改配置文件

# 打开刚才创建的.jupyter/jupyter_notebook_config.py,先备份源文件,然后再修改
# 备份
$ cp .jupyter/jupyter_notebook_config.py .jupyter/jupyter_notebook_config.py_bak

# 修改如下,可以先删除里面的内容添加,也可以修改,或者直接在头部添加,反正里面的原先的内容都是注释掉的:
vi /home/zhenyu/.jupyter/jupyter_notebook_config.py

c = get_config()
# Kernel config
c.IPKernelApp.pylab = 'inline'  # if you want plotting support always

c.NotebookApp.ip = '*'  # 就是设置所有ip皆可访问,在144行
c.NotebookApp.open_browser = False  # 禁止自动打开浏览器
# 密钥,在194行。该密钥就是2.1步生成的
c.NotebookApp.password = 'sha1:74d233d59da1:50d7ef60a58456e2016dc427547fb42cdd971cea'
c.NotebookApp.port = 6789  # 访问端口,在197行
# 自签名证书位置,如果不使用ssl,可以不设置
c.NotebookNotary.secret_file = '/home/zhenyu/.jupyter/secret/mycert.pem'
c.NotebookApp.keyfile = '/home/zhenyu/.jupyter/.secret/mykey.key'
# 设置目录,存放创建的ipython notebook文件
c.NotebookApp.notebook_dir = '/home/zhenyu/ipython'

3. 防火墙开放端口

启动jupyter notebook后,在虚拟机中打开浏览器可以在访问ipython jupyter,但是远程是无法连接的,因为防火墙啊。

# 使用root用户
su
# 开放6789端口
/sbin/iptables -I INPUT -p tcp --dport 6789-j ACCEPT
保存
/etc/rc.d/init.d/iptables save
重启服务
service iptables restart

4.远程访问

# 启动ipython jupyter,不使用ssl
jupyter notebook
# 或者开启ssl
# jupyter notebook --certfile=mycert.pem --keyfile mykey.key
jupyter notebook --certfile=/home/zhenyu/.jupyter/secret/mycert.pem

# 输出,看最后一行,此时jupyter notebook 可以接受任何IP访问。
[I 12:55:05.929 NotebookApp] [nb_conda_kernels] enabled, 2 kernels found
[W 12:55:05.960 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 12:55:06.078 NotebookApp] The Jupyter Notebook is running at: http://[all ip addresses on your system]:6789/

此时打开浏览器输入 http://192.168.138.130:6789/即可访问虚拟机中的ipython notebook。

5.日志和后台进程

上面的启动方式,会在当前目录生成一个日志文件,我忘了叫上面名字,总之随着jupyter notebook的运行,日志文件会越来越大,如果不是很重要,可以设置不记录日志,方法是将所有的输出都重定向到/dev/null 2>&1 &
此外,上面的启动方式是启动一个前台进程,如果ssh连接断开,jupyter notebook也就失效了,所以需要将jupyter notebook作为一个后台进程启动,在linux中是nohup命令。

# 不启动ssl,不记录日志输出,作为后台进程启动jupyter notebook
nohup jupyter notebook >/dev/null 2>&1 &

6.停止jupyter notebook

jupyter notebook作为后台进程启动后,如果想要停止它,可以先找到进程ID,然后kill。

# 查看进程
ps -ef | grep 'jupyter notebook'
# 输出如下,这里的21983即为进程id,
# hadoop    22136  21983  0 09:10 pts/1    00:00:00 grep jupyter notebook
# 杀死进程
kill -9 21983
# 此时浏览器无法再连接jupyter notebook了吧。

参考

http://jupyter-notebook.readthedocs.io/en/latest/public_server.html
http://www.cnblogs.com/zhanglianbo/p/6109939.html
http://blog.csdn.net/gavin_john/article/details/53177630
http://jingyan.baidu.com/article/335530daa4707f19cb41c3ef.html

原文地址:https://www.cnblogs.com/it-tsz/p/9852727.html