Jupyter离线安装部署

1. 环境

1.1 安装环境

RedHat 6.8

1.2 准备安装包

Anaconda3-5.2.0-Linux-x86_64.sh

2. 安装部署

以下使用root用户操作

2.1 添加jupyter用户组
groupadd jupyter
useradd -g jupyter jupyteradmin
passwd jupyteradmin 
2.2 创建/jupyter目录
mkdir -p /jupyter/data/jupyteradmin  /jupyter/log
chown -R jupyteradmin:jupyter /jupyter
chmod -R 775 /jupyter

以下使用 jupyteradmin 用户操作

2.3 安装anaconda

利用jupyteradmin 用户执行安装:

bash Anaconda3-5.2.0-Linux-x86_64.sh

此时会打印许可条款,然后询问是否修改默认安装位置,如下图所示:

Anaconda3 will now be installed into this location:
/home/jupyteradmin/anaconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/home/jupyteradmin/anaconda3] >>> 

点击ENTER(不修改默认路径),开始安装。

安装到最后,询问是否将Anaconda3的安装位置加入/home/jupyteradmin/.bashrc文件中的PATH环境变量,选择yes,如下图所示:

Do you wish the installer to prepend the Anaconda3 install location to PATH in your /home/jupyteradmin/.bashrc ? [yes|no]
[no] >>> yes

安装完成之后,执行source命令,使.bashrc文件生效:

source .bashrc
2.4 验证anaconda是否安装成功

因为linux系统自带的python版本为 2.6.x,而我们安装的Anaconda-5.2.0 对应python版本为3.6.5,因此可以通过python的版本号来判断anaconda是否安装成功。

具体操作是:使用jupyteradmin 用户在命令行输入python -V ,如果输出为Python 3.6.5 :: Anaconda, Inc.,则说明Anaconda安装成功。如下图所示:

[jupyteradmin@localhost ~]$ python -V
Python 3.6.5 :: Anaconda, Inc.

另外,利用pip list 可以查看anaconda所集成的各种科学包及其版本号,其中包括 jupyter notebook相关包以及常见的python包。

[jupyteradmin@localhost ~]$ pip list
Package                            Version  
---------------------------------- ---------
alabaster                          0.7.10   
anaconda-client                    1.6.14   
anaconda-navigator                 1.8.7    
anaconda-project                   0.8.2    
asn1crypto                         0.24.0   
astroid                            1.6.3    
astropy                            3.0.2    
attrs                              18.1.0   
Babel                              2.5.3    
backcall                           0.1.0    
backports.shutil-get-terminal-size 1.0.0    
beautifulsoup4                     4.6.0    
bitarray                           0.8.1    
bkcharts                           0.2      
blaze                              0.11.3   
bleach                             2.1.3    
bokeh                              0.12.16  
boto                               2.48.0   
Bottleneck                         1.2.1    
## 未完
2.5 配置 jupyter notebook
  • 生成密钥

在python 命令行中输入下列命令:

>>> from notebook.auth import passwd
>>> passwd()
## 此时会提示输入两次密码,并生成密钥(包括 sha1: 字符串),复制该密钥。
>>> exit()

虽然passwd()函数可以直接传递字符串作为参数,比如passwd('mypassword'),但这样会将密码保存在输入历史记录中,缺乏安全性,因此不这样用。

  • 生成配置文件
 [jupyteradmin@localhost local]$ jupyter notebook --generate-config

此时会显示配置文件所在目录: /home/jupyteradmin/.jupyter/jupyter_notebook_config.py

  • 修改配置
[jupyteradmin@localhost local]$ vim /home/jupyteradmin/.jupyter/jupyter_notebook_config.py

文件末尾添加

c.NotebookApp.ip = '*' #notebook服务器监听的IP地址
c.NotebookApp.open_browser = False  # 禁止自动打开浏览器
c.NotebookApp.port = 8888  # 默认端口
c.NotebookApp.password = u'sha1:0505dc9375e3:3609ff1d0424417f7b363e00d55e713dea7bbfa0' # 复制的密钥
c.ContentsManager.root_dir = '/jupyter/data/jupyteradmin'  # jupyter notebook打开时显示的本地路径,即jupyteradmin的工作路径
c.PAMAuthenticator.encoding = 'utf8' #解决读取中文路径或者文件乱码的问题

注意:jupyter默认采用HTTP协议,对应web页面为‘ http://ip:port/ ’。

为了提高安全性,建议采用SSL加密通信,具体操作是:将SSL/TLS的证书文件和私钥文件的完整路径也添加到配置文件中。如下所示:

# The full path to an SSL/TLS certificate file.
c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'

# The full path to a private key file for usage with SSL/TLS.
c.NotebookApp.keyfile = u'/absolute/path/to/your/certificate/mykey.key'

当使用SSL时,在浏览器端使用 'https://ip:port/ ’ 访问jupyter页面。

2.6 验证 jupyter notebook是否配置成功

在终端输入jupyter notebook命令:

[jupyteradmin@localhost ~]$ jupyter notebook

终端将显示一系列notebook的服务器信息 ,内容如下:

[jupyteradmin@localhost root]$ jupyter notebook
[I 14:26:07.618 NotebookApp] Writing notebook server cookie secret to /home/jupyteradmin/.local/share/jupyter/runtime/notebook_cookie_secret
[W 14:26:07.825 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 14:26:07.859 NotebookApp] JupyterLab beta preview extension loaded from /home/jupyteradmin/anaconda3/lib/python3.6/site-packages/jupyterlab
[I 14:26:07.859 NotebookApp] JupyterLab application directory is /home/jupyteradmin/anaconda3/share/jupyter/lab
[I 14:26:07.882 NotebookApp] Serving notebooks from local directory: /jupyter/data/jupyteradmin
[I 14:26:07.882 NotebookApp] 0 active kernels
[I 14:26:07.882 NotebookApp] The Jupyter Notebook is running at:
[I 14:26:07.882 NotebookApp] http://localhost.localdomain:8888/
[I 14:26:07.883 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

输出的信息(上述第8~9行)会显示:The Jupyter Notebook is running at: http://localhost.localdomain:8888/ 。在本地浏览器上登陆 http://ip:8888/(其中, ip 是运行jupyter notebook的服务器ip)即可访问 jupyter notebook工作空间。当使用SSL时,url头部为 https://

第一次登陆时需要输入密码,就是生成sha1密钥时输入的密码。

登陆成功之后,进入到 Jupyter主页面,展示内容应为 /jupyter/data/jupyteradmin(即ContentsManager.root_dir)路径下的文件列表。

点击页面右上方的”新建”(或new)>> “Python3”,会创建一个名为Untitled的文件,并自动打开该文件的编辑页面。

在第一个单元格中,输入print("hello jupyter!"), 再点击工具栏中的Run,单元格下方将输出 hello jupyter!

如果以上操作都顺利完成,则说明 jupyter配置无误。

友情提示:

为了让 Jupyter Notebook一直在后台运行,关了ssh 也不停止服务,可以使用nohup命令启动:

[jupyteradmin@localhost ~]$ nohup jupyter notebook &
2.8 其他说明
2.8.1 防火墙设置

为了提升安全性,运行jupyter notebook的服务器可以做如下防火墙配置:

  • 只允许特定IP客户端访问jupyter页面。

要想实现IP访问控制,需要配置防火墙规则,使得只有特定IP的客户机能够连接jupyter服务端口c.NotebookApp.port

假设c.NotebookApp.port=8888 ,只允许IP为192.168.141.96的客户机能够访问,则进行以下操作:

# 备份iptables,如果系统配置目录中没有防火墙配置文件,即/etc/sysconfig/iptables不存在,则跳过这一步;
cp /etc/sysconfig/iptables /var/tmp

# 修改iptables
iptables -I INPUT -p TCP --dport 8888 -j DROP
iptables -I INPUT -s 192.168.141.96 -p TCP --dport 8888 -j ACCEPT

# 保存iptables
service iptables save

# 重启防火墙
service iptables restart
  • 允许127.0.0.1(本地主机)能够连接49152~65535的端口。

服务器使用这些端口与notebook内核通信,内核通信端口是由ZeroMQ随机选择的,每个内核可能需要多个连接,因此必须有大范围的可访问端口。

注:ZeroMQ是一个基于消息队列的多线程网络库,提供跨越多种传输协议的套接字。

2.8.2 密码重置

如果想要更改或重置密码,可以在终端输入jupyter notebook password,如下图:

$ jupyter notebook password
Enter password:  ****
Verify password: ****
[NotebookPasswordApp] Wrote hashed password to ~/.jupyter/jupyter_notebook_config.json

这里将提示您输入两次密码(新密码),哈希后的密码被存储在jupyter_notebook_config.json中。

之后重启 jupyter notebook 服务器,在浏览器上访问 jupyter页面,就可以使用新密码登录了。

2.8.3 端口设置

如果在多个终端同时启动了Jupyter Notebook,默认端口“8888”已被占用,服务器会自动切换到端口 8889 上运行,如果8889被占用,则会切换到8890,以此类推…… 默认会尝试50个其他端口。

如果您想自定义端口号来启动 Jupyter Notebook,可以直接在终端中输入以下命令:

jupyter notebook --port <port_number>

其中,“<port_number>”是自定义端口号,直接以数字的形式写在命令当中,数字两边不加尖括号“<>”。如:jupyter notebook --port 9999

这种在命令行中进行的端口设置是临时的,要想永久修改,必须更改jupyter配置文件中的c.NotebookApp.port参数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值