正文
配置基于centos下的远程Jupyter Notebook访问
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
最近在学习一些服务器上的操作,学着熟悉Liunx系统,记录下自己踩过的坑吧
1、开机后更新系统:
yum -y upgrade
yum - y update
2、查看已安装的应用
yum list
3、接下来因为要用到anaconda+Python环境,配置下:
1.首先,你要知道系统现在的python的位置在哪儿:
[root@root ~]# whereis python
python: /usr/bin/python2.7 /usr/bin/python /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz
可以知道我们的python在 /usr/bin目录中
[root@root ~]# cd /usr/bin/
[root@root bin]# ll python*
lrwxrwxrwx. 1 root root 7 2月 7 09:30 python -> python2
lrwxrwxrwx. 1 root root 9 2月 7 09:30 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 8月 4 2017 python2.7
可以看到,python指向的是python2,python2指向的是python2.7,因此我们可以装个python3,然后将python指向python3,然后python2指向python2.7,那么两个版本的python就能共存了。
2.因为我们要安装python3,所以要先安装相关包,用于下载编译python3:
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
#运行了以上命令以后,就安装了编译python3所用到的相关依赖
3.默认的,centos7也没有安装pip。
#运行这个命令添加epel扩展源
yum -y install epel-release
#安装pip
yum install python-pip
4.用pip装wget
pip install wget
5.用wget下载python3的源码包
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
6.编译python3源码包
#解压
xz -d Python-3.6.4.tar.xz
tar -xf Python-3.6.4.tar
#进入解压后的目录,依次执行下面命令进行手动编译
cd Python-3.6.4
./configure prefix=/usr/local/python3
make && make install
# 如果出现can't decompress data; zlib not available这个错误,则需要安装相关库
#安装依赖zlib、zlib-devel
yum install zlib zlib
yum install zlib zlib-devel
如果最后没提示出错,就代表正确安装了,在/usr/local/目录下就会有python3目录
7.添加软链接
#将原来的链接备份
mv /usr/bin/python /usr/bin/python.bak
#添加python3的软链接
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python
#测试是否安装成功了
python -V
8.更改yum配置,因为其要用到python2才能执行,否则会导致yum不能正常使用
vi /usr/bin/yum
把#! /usr/bin/python修改为#! /usr/bin/python2
vi /usr/libexec/urlgrabber-ext-down
把#! /usr/bin/python 修改为#! /usr/bin/python2
4、在Linux下删除文件用rm命令,具体用法如下:
rm [选项] 文件
选项说明: -f -force 忽略不存在的文件,强制删除,无任何提示 -i --interactive 进行交互式地删除 -r | -R --recursive 递归式地删除列出的目录下的所有目录和文件 -v --verbose 详细显示进行的步骤 命令实例: 1、常规删除a.txt文件
[root]# rm a.txt
2、强行删除file.log文件
[root]# rm -f file.log
3、删除dirname目录下的所有东西
[root]# rm -ivrf dirname
5、在 Centos 下安装Anaconda
下载:wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.1-Linux-x86_64.sh
安装:bash Anaconda3-5.3.1-Linux-x86_64.sh
接下来会自动安装至:/root/anaconda/
注:若没有将地址添加到PATH中可用:
export PATH=/root/anaconda3/bin:$PATH
测试anaconda已经安装好:conda -V
创建专门的运行环境
conda create -n tensorflow
启动Tensorflow运行环境
source activate tensorflow
#关闭运行环境
#source deactivate tensorflow
#添加清华大学 TUNA提供的Anaconda仓库镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
conda install numpy #测试是否添加成功
#搜索可用版本
anaconda search -t conda tensorflow
#选择一个较新的CPU或GPU版本,如jjh_cio_testing/tensorflow的1.0.1版本,输入如下命令查询安装命令
anaconda show jjh_cio_testing/tensorflow
#使用最后一行的提示命令进行安装
conda install --channel https://conda.anaconda.org/jjh_cio_testing tensorflow
#conda会自动检测安装此版本的Tensorflow所依赖的库,如果你的Anaconda缺少这些依赖库,会提示你安装。
测试:
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
6、服务器端安装好 Anaconda 后就要进行远程访问了,此时我使用的是windows系统,之前一直使用 Xshell 的方式进行服务器配置环节。
服务器端启动 Jupyter Notebook :
检查本地是否安装成功并生成配置文件
jupyter notebook –generate-config –allow-root
生成远程访问密码
#利用ipython生成密码
[root@localhost ~]# ipython
Python 3.6.2 |Anaconda, Inc.| (default, Sep 30 2017, 18:42:57)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: '*******'
修改配置文件
vi /root/.jupyter/jupyter_notebook_config.py
c.NotebookApp.ip='*'
c.NotebookApp.password = u'*******'将上述生成的密码复制到这里
c.NotebookApp.open_browser = False
c.NotebookApp.port =8888
测试远程访问
远程连接服务器后,终端输入Jupyter notebook 打开服务。在浏览器中输入<serverip>:8888,测试是否访问成功,访问不成功需将服务器防火墙关闭:
systemctl stop firewalld.service
7、当需要使用服务时:
(1) 在服务端输入 shell 指令:
jupyter notebook --ip=0.0.0.0 --no-browser --port=1111
将会创建一个没有浏览器的notebook,ip这里指定为0.0.0.0(比较随意,不会和其他用户产生冲突),端口号为xxxx,一般端口选择1024-49151中任一即可,如1111,为避免与其他服务端口冲突,建议自己另设置一个端口号。本文以端口号 1111 为例。
(2)在 Windows 上打开git的 bash 终端(或者PowerShell也可),输入:
ssh -N -f -L localhost:1112:localhost:1111 username@serverIP
第一个localhost填入xxxx+1,第一个localhost填入xxxx,username为服务器端的用户名,serverIP填入服务器ip。
执行命令后需要输入服务器用户名密码,输出完成后没有其他显示,保持bash终端不要关闭。
(3)在个人电脑上打开浏览器,输入http://localhost:xxxx+1 即可
8、配置 tensorflow 时遇到两个小问题:
(1).遇到了ERROR: Cannot uninstall 'wrapt'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
办法1:输入 pip install -U --ignore-installed wrapt enum34 simplejson netaddr
参考:https://www.cnblogs.com/xiaowei2092/p/11025155.html
(2)遇到了ERROR: tensorboard 1.14.0 has requirement setuptools>=41.0.0, but you'll have setuptools 39.1.0 which is incompatible.
原因: setuptools 版本太低
办法:更新setuptools版本 输入 pip install --upgrade setuptools