Centos7搭建Jupyter Notebook

Leo 2019年10月20日 1,168次浏览

Centos7自带Python2,且没提供Python3的架包,因此要安装Python3需要从clone源码并编译。本文默认在root目录(即~目录)上进行操作。

1.更新yum

yum update -y

2.安装必要的软件

yum -y install \
zlib-devel \
bzip2-devel \
openssl-devel \
ncurses-devel \
sqlite-devel \
readline-devel \
tk-devel \
libffi-devel \
wget \
gcc \
make
  1. Clone Python3.7源码并解压

    wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz && tar -zxvf Python-3.7.4.tgz

4.进入解压后的目录,执行编译前的configure操作:

cd Python-3.7.4 && ./configure prefix=/usr/local/pyth

编译源码,在Python-3.7.4目录执行以下命令:

make && make install

创建软连接

ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

至此Python3和pip3已经安装好了,我们可以通以下命令来检查是否安装成功:

python3 --version
pip3 --version

成功安装完Python3以后就可以安装Jupyter Notebook了

先升级pip,并安装Jupyter Notebook

python3 -m pip install --upgrade pip
pip3 install ipython jupyter notebook

生成配置文件

jupyter notebook --generate-config

我们可以在 /user/.jupyter 中找到 jupyter_notebook_config.py ,即Jupyter的配置文件,在此之前为了确保安全,我们先利用Python生成SHA密钥对

In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password: ****
Verify password: ****
Out[2]: 'sha1:a937e51de9a1:a567707768cd50d0ac1d40a4fb739510ddb3d8cb'

请务必记住这个密钥对,待会配置的时候要用。

我们打开 Jupyter Notebook 的配置文件,并在最后插入以下内容:

c.NotebookApp.ip = '*'  # 允许所有IP访问
c.NotebookApp.password = u'sha1:a937e51de9a1:a567707768cd50d0ac1d40a4fb739510ddb3d8cb' # SHA密钥
c.NotebookApp.open_browser = False # 是否默认打开浏览器
c.NotebookApp.port = 8888 #端口
c.NotebookApp.enable_mathjax = True 
c.NotebookApp.allow_remote_access = True  # 允许远程访问
c.NotebookApp.notebook_dir = '/root/workspace/js_python' # Jupyter工作目录

运行 Jupyter Notebook ,至此,我们可以通过IP:8888来访问Jupyter Notebook了

Update 2020/1/8

配置开机自启动:

编辑 /etc/systemd/system/jupyter.service 插入

[Unit]
Description=Jupyter Notebook
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/python3/bin/jupyter-notebook
KillMode=process
Restart=on-failure
RestartSec=30s

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable jupyter.service
sudo systemctl start jupyter.service
检查 systemctl status jupyter

即可实现开机自启