昨天发了Jupyter的使用,补一篇Jupyter服务器的搭建~

一、搭建Jupyter

1.安装

使用virtualenv建虚拟环境。在虚拟环境中安装jupyter、matplotlib等等需要的库。这里不赘述了。

2.配置

为Jupyter 相关文件准备一个目录

mkdir /data/jupyter
cd /data/jupyter

建立一个目录作为 Jupyter 运行的根目录

mkdir /data/jupyter/root

我们以需要密码验证的模式启动 Jupyter,所以要预先生成所需的密码对应的密文。
使用下面的命令创建一个密文的密码

python2

python -c "import IPython;print IPython.lib.passwd()"

python3

python -c "import IPython;print(IPython.lib.passwd())"

执行后需要输入并确认密码,然后程序会返回一个 'sha1:...' 的密文,留好了,我们接下来将会用到它。

查看用户目录 ~/.jupyter 路径下是否存jupyter_notebook_config.py 文件。若不存在,产生此文件。

jupyter notebook --generate-config

编辑此文件,在最后写入

c.NotebookApp.ip = '*'  # 允许访问此服务器的 IP,星号表示任意 IP
c.NotebookApp.password = u'sha1:xxx:xxx' # 之前生成的密码 hash 字串
c.NotebookApp.open_browser = False # 运行时不打开本机浏览器
c.NotebookApp.port = 8888 # 使用的端口
c.NotebookApp.enable_mathjax = True # 启用 MathJax

3.运行

这时采用 IP:端口号 或者 域名:端口号的方式就可以访问正常使用了。

域名访问默认80端口,接下来我们使用最常用的Nginx做代理,实现直接使用域名进行访问,隐藏端口信息。先对Jupyter进行以下修改

将配置文件ip改为只有本机才能访问

c.NotebookApp.ip = '0.0.0.0' # 127.0.0.1 也可以的

后台运行起来

nohup jupyter notebook > /dev/null 2>&1 &

二、Nginx代理

1.安装与设置

$ apt-get install nginx

2.查看版本

$ nginx -v

3.启动停止

安装完成后,使用 nginx 命令就可以直接启动 Nginx

$ nginx

也可以使用服务器

$ service nginx start
$ service nginx stop
$ service nginx restart

访问 http://YOUR_IP 或者 域名 就可以看到Nginx的测试页面。

4.配置

配置文件在nginx目录下,nginx.conf文件中可以看到代理的部分在在这里 /etc/nginx/sites-enabled/defalut

$ sudo vim /etc/nginx/sites-enabled/defalut

修改其中的 location / 部分。

server {
server_name DOMAIN IP_ADDRESS; # 服务器域名和 IP 地址
listen 80;
...
...
location / {
proxy_pass http://127.0.0.1:YOUR_PORT
}
}

按照上面的方法配置 Jupyter Notebook,如果仅仅对端口号进行代理转发,会出现 terminal 可以正常创建而 notebook 无法正常创建或者使用的情况。因为 Jupyter 会对 http 请求进行判断,所以反向代理时需要设置正确的信息。正确配置 nginx 反向代理的方式如下:

server {
server_name DOMAIN IP_ADDRESS; # 服务器域名和 IP 地址
listen 80;
...
...
location / {
proxy_pass http://127.0.0.1:YOUR_PORT/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
}
}

重启Nginx服务让设置生效。

$ sudo nginx -s reload

一切正常,晚上吃鸡!不不不,开始干活!

5.可选

目前的URL为 http://dyan.club/tree?,可以在 jupyter_notebook_config.py 中增加 base_url 作为url的路径,来表示服务器上的Jupyter目录的地址。更新后的地址为 http://dyan.club/ipython/tree?

c.NotebookApp.base_url = '/ipython/'
--制定url的path,默认是根目录

目前没有启用ssl,安全性不够,也可以增加ssl协议增强安全性。


参考文献
[1] https://bitmingw.com/2017/07/09/run-jupyter-notebook-server/
[2] https://jupyter.readthedocs.io/en/latest/install.html
[3] http://blog.takwolf.com/2016/10/19/setup-nginx-on-ubuntu/index.html

拓展阅读
[1] http://nbviewer.jupyter.org/

搭建Jupyter Notebook服务器的更多相关文章

  1. Python - 搭建Jupyter notebook环境

    1- Jupyter简介 HomePage:https://jupyter.org/ 安装指南:https://jupyter.org/install.html 官方文档:https://jupyte ...

  2. 使用亚马逊云服务器EC2做深度学习(二)配置Jupyter Notebook服务器

    这是<使用亚马逊云服务器EC2做深度学习>系列的第二篇文章. (一)申请竞价实例  (二)配置Jupyter Notebook服务器  (三)配置TensorFlow  (四)配置好的系统 ...

  3. 搭建Python3的jupyter notebook服务器

    摘要:搭建Python3 jupyter notebook. 激活Python3后,进入Python交互环境 1. 登陆远程服务器 2. 生成配置文件 1. $jupyter notebook --g ...

  4. Python3 jupyter notebook 服务器搭建

    1. jupyter notebook 安装 创建 jupyter 目录 mkdir jupyter cd jupyter/ 创建独立的 Python3 运行环境,并激活进入该环境 virtualen ...

  5. 在服务器搭建Jupyter notebook

    安装 Jupyter Notebook (这里虽然是对centos和Python2的,但是在Ubuntu16.04,Python3同样可以照着弄) Jupyter Notebook 简介 Jupyte ...

  6. 使用docker搭建jupyter notebook / jupyterlab

    说明 由于官方镜像实在是不怎么好用,所以我自己做了一个优化过的jupyter notebook的镜像 notebook_hub,使用我这个镜像搭建容器非常简单,下面就基于这个notebook_hub来 ...

  7. 【机器学习 Azure Machine Learning】使用Aure虚拟机搭建Jupyter notebook环境,为Machine Learning做准备(Ubuntu 18.04,Linux)

    问题描述 在Azure的VM中已经安装好Jupyter,并且通过jupyter notebook --port 9999 已经启动,但是通过本机浏览器,访问VM的公网IP,则始终是不能访问的错误.(T ...

  8. 使用centos linux vps搭建jupyter notebook踩坑日记

    今天我尝试用vps搭建在线jupyter notebook网站时遇到了这样一个问题: [W 21:48:07.243 NotebookApp] SSL Error on 9 ('171.115.101 ...

  9. vps上搭建jupyter notebook远程服务

    安装anaconda 使用如下命令下载: wget https://repo.continuum.io/archive/Anaconda3-5.0.0.1-Linux-x86_64.sh 如果非roo ...

随机推荐

  1. GIT团队合作探讨之二--Pull Request

    pull request是github/bitbucket给开发人员实现便利合作提供的一个feature.他们提供一个用户友好的web界面在进代码之前来讨论这些变更. 简单说,pull request ...

  2. Web API 2 入门——创建ASP.NET Web API的帮助页面(谷歌翻译)

    在这篇文章中 创建API帮助页面 将帮助页面添加到现有项目 添加API文档 在敞篷下 下一步 作者:Mike Wasson 创建Web API时,创建帮助页面通常很有用,以便其他开发人员知道如何调用A ...

  3. MEGER sentence in oracle

    MEGE Sentence This oracle tutorial explains how to use the oralce MEGER sentence with syntax and sam ...

  4. Google Colab 免费的谷歌GPU for deep learning

    Who wants to use a free GPU for deep learning?Google Colab is a free cloud service and now it suppor ...

  5. January 15 2017 Week 3 Sunday

    A good book is the best of friends, the same today and forever. 好书乃挚友,情谊永不渝. For a coder, that is no ...

  6. February 17 2017 Week 7 Friday

    The very essence of romance is uncertainty. 浪漫的精髓就在于它充满种种可能. If you want a happy life with enduring ...

  7. SAP CRM One order里user status和system status的mapping逻辑

    Below example show: How the mapping relationship between User status and System status maintained in ...

  8. mybatis插入返回主键

     useGeneratedKeys="true" keyProperty="id" <insert id="insertReturnPrimar ...

  9. Gluon.vision的几类数据集

    http://mxnet.apache.org/api/python/gluon/data.html import mxnet as mx from mxnet import nd,autograd, ...

  10. 2.spring:集合属性

    1.list  配置java.util.List类型的属性,需要指定<list>标签,在标签里面包含有一些元素,这些标签 可以通过<value>指定简单的常量值,通过<r ...