目录
一、环境安装
二、安装 ipython jupyter notebook
三、配置 ipython jupyter notebook
四、使用 Jupyter
在上一篇笔记中我在VMware虚拟机中安装了 pyenv 环境,使用 anaconda3-4.1.0 的 pyenv的Python环境 进行ipython jupyter notebook安装
1. 安装pip
[root@zhang ~]# yum install -y epel-release python-pip python-devel
2. 安装开发工具集
[root@zhang ~]# yum groupinstall 'Development Tools' -y
注:pip 存在于 anaconda3-4.1.0 的Python版本中
[root@zhang ~]# pip install ipython jupyter notebook
pyenv: pip: command not found
The `pip' command exists in these Python versions:
anaconda3-4.1.0
#切换当前全局Python版本
[root@zhang ~]# pyenv global anaconda3-4.1.0
#安装ipython jupyter notebook
(anaconda3-4.1.0) [root@zhang ~]# pip install ipython jupyter notebook
如果你只在本机使用notebook,那么这一步可以省去,使用 jupyter notebook 命令启动jupyter。如果你想把notebook做为公共服务供其它人使用,配置允许远程访问:
1. 设置openssl以保证其安全性
注:可以 回车 使用默认设置
#可以 回车 使用默认设置
(anaconda3-4.1.0) [root@zhang ~]# openssl req -x509 -nodes -days 365 -newkey rsa:4096 -keyout zhangkey.key -out zhangcert.pem
Generating a 4096 bit RSA private key
..................................++
..........................................................................................................................................++
writing new private key to 'zhangkey.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:CQ
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:
2. 设置 Jupyter 登录密码
注:设置过程中要设置用于远程登录的密码,要记住 sha1
值,后面的配置文件中需要使用
(anaconda3-4.1.0) [root@zhang ~]# ipython
Python 3.5.1 |Anaconda 4.1.0 (64-bit)| (default, Jun 15 2016, 15:32:45)
Type "copyright", "credits" or "license" for more information.
IPython 4.2.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'sha1:ee43794e60d9:30e9a40dff15e6214d7033a1e06b0df751e6b616'
3. 生成配置文件
(anaconda3-4.1.0) [root@zhang ~]# jupyter notebook --generate-config
Writing default config to: /root/.jupyter/jupyter_notebook_config.py
4. 添加一个工作目录
(anaconda3-4.1.0) [root@zhang ~]# mkdir /opt/zhangjupyter
5. 在生成的配置文件中添加如下内容
(anaconda3-4.1.0) [root@zhang ~]# vi /root/.jupyter/jupyter_notebook_config.py
# Configuration file for jupyter-notebook.
c = get_config()
c.NotebookApp.password = 'sha1:ee43794e60d9:30e9a40dff15e6214d7033a1e06b0df751e6b616'
c.NotebookApp.port = 8888
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.certfile = '/root/zhangcert.pem'
c.NotebookApp.keyfile = '/root/zhangkey.key'
c.ContentsManager.root_dir = '/opt/zhangjupyter'
配置文件详解:
c.NotebookApp.password 为刚刚在 IPython 中生成的 sha1,用于验证密码登录
c.NotebookApp.certfile、c.NotebookApp.keyfile 为 Httpd 添加 SSL,内容为 pem文件和key文件的绝对路径
c.NotebookApp.port 访问端口
c.NotebookApp.ip 访问IP
c.ContentsManager.root_dir 这里是Jupyter的工作目录
6. 开启防火墙相应端口
(anaconda3-4.1.0) [root@zhang ~]# firewall-cmd --zone=public --add-port=8888/tcp --permanent
success
(anaconda3-4.1.0) [root@zhang ~]# systemctl restart firewalld.service
1. 运行 Jupyter
注: Ctrl + C 即可关闭运行
(anaconda3-4.1.0) [root@zhang ~]# jupyter notebook
[W 18:41:03.410 NotebookApp] Unrecognized JSON config file version, assuming version 1
[I 18:41:04.052 NotebookApp] [nb_conda_kernels] enabled, 1 kernels found
[I 18:41:04.579 NotebookApp] [nb_anacondacloud] enabled
[I 18:41:04.665 NotebookApp] ✓ nbpresent HTML export ENABLED
[W 18:41:04.666 NotebookApp] ✗ nbpresent PDF export DISABLED: No module named 'nbbrowserpdf'
[I 18:41:04.669 NotebookApp] [nb_conda] enabled
[I 18:41:04.670 NotebookApp] Serving notebooks from local directory: /opt/zhangjupyter
[I 18:41:04.670 NotebookApp] 0 active kernels
[I 18:41:04.670 NotebookApp] The Jupyter Notebook is running at: https://[all ip addresses on your system]:8888/
[I 18:41:04.670 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
2. 后台运行
注:可以使用 kill 进程号ID 关闭运行
(anaconda3-4.1.0) [root@zhang ~]# nohup jupyter notebook > /opt/zhangjupyter/zhangjupyter.log 2>&1 &
[1] 55093
该命令将使得 Jupyter 在后台运行,并将日志写在 /opt/zhangjupyter/zhangjupyter.log 文件中
3. 检查是否成功运行
#安装 netstat 命令,查看相应端口是否开启
(anaconda3-4.1.0) [root@zhang ~]# yum install -y net-tools
#grep 后面加上相应的端口号
(anaconda3-4.1.0) [root@zhang ~]# netstat -auntlp |grep 8888
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 55093/python
tcp 0 0 192.168.200.69:8888 192.168.200.1:39440 ESTABLISHED 55093/python
tcp 0 0 192.168.200.69:8888 192.168.200.1:39432 ESTABLISHED 55093/python
tcp 0 0 192.168.200.69:8888 192.168.200.1:39431 ESTABLISHED 55093/python
tcp6 0 0 :::8888 :::* LISTEN 55093/python
参考博客:
https://blog.csdn.net/ScarlettYellow/article/details/80458780
https://blog.csdn.net/tina_ttl/article/details/51031113