Linux 下远程 搭建 Jupyter Notebook 服务器配置及无法连接原因

本身自己机器上安装了太多东西,有点慢,本来也喜欢所有的东西搬到服务上,所以如果能安装一个远程的Jupyter 服务器,用起来就会很方便。

记录下自己的安装步骤

1. 安装ipython, jupyter

  1. pip install ipython

  2. pip install jupyter


2. 生成配置文件

 
  1. [root@50eb5057baac /]# jupyter notebook --generate-config

  2. Writing default config to: /root/.jupyter/jupyter_notebook_config.py


3. 生成密码

  1. root@50eb5057baac /]# ipython

  2. Python 3.5.1 (default, Oct 21 2016, 21:37:19)

  3. Type 'copyright', 'credits' or 'license' for more information

  4. IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

  5.  
  6. In [1]: from notebook.auth import passwd

  7.  
  8. In [2]: passwd()

  9. Enter password:

  10. Verify password:

  11. Out[2]: 'sha1:43b95b731276:5d330ee6f6054613b3ab4cc59c5048ff7c70f549'

  12.  
  13. In [3]:

3. 修改默认配置文件

vi /root/.jupyter/jupyter_notebook_config.py 
 
  1. c.NotebookApp.ip='*' #设置访问notebook的ip,*表示所有IP,这里设置ip为都可访问  
  2. c.NotebookApp.password = u'sha1:5df252f58b7f:bf65d53125bb36c085162b3780377f66d73972d1' #填写刚刚生成的密文  
  3. c.NotebookApp.open_browser = False # 禁止notebook启动时自动打开浏览器(在linux服务器一般都是ssh命令行访问,没有图形界面的。所以,启动也没啥用)  
  4. c.NotebookApp.port =8889 #指定访问的端口,默认是8888。  

4.    启动jupyter notebook

  1. [root@346086094cbe /]#jupyter notebook --config /root/.jupyter/jupyter_notebook_config.py --allow-root   
  2. [W 17:17:04.106 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.    
  3. [I 17:17:04.111 NotebookApp] Serving notebooks from local directory: /    
  4. [I 17:17:04.112 NotebookApp] 0 active kernels    
  5. [I 17:17:04.112 NotebookApp] The Jupyter Notebook is running at:    
  6. [I 17:17:04.112 NotebookApp] http://[all ip addresses on your system]:8889/    
  7. [I 17:17:04.112 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).  </code>  

5. 然后你就可以在浏览器里敲入你的地址 http://yourip:8889/, 看到如下界面。


安装成功

6. 启动Jupyter的开发窗口,点击右上角的new 按钮

7.后台运行

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

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

上边的命令jupyter notebook  改为jupyter notebook --config /root/.jupyter/jupyter_notebook_config.py --allow-root 

8.停止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了吧。

远程访问失败原因:

1.启动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

2.关闭防火墙:命令

systemctl stop firewalld.service 

3.启动时命令jupyter notebook --config /root/.jupyter/jupyter_notebook_config.py --allow-root   

 jupyter_notebook_config.py要用绝对路径

猜你喜欢

转载自blog.csdn.net/qq_35843543/article/details/81409269