昨天发了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. AWS的load balance

    Route53实现了地理上的load balance; ELB实现了region内的load balance CloudFront实现了静态内容的全网加速 ZULh?*;&T(

  2. 用WCAT进行IIS压力测试

    用WCAT进行IIS压力测试 分类: javascript专辑 IT信息化 2008-10-13 16:56 5754人阅读 评论(1) 收藏 举报 iis测试服务器microsoft脚本网络 如何建 ...

  3. IIS 无法安装URL重写模块的解决办法 UrlReWrite (.NET`SQL技术交流 群号206656202)

    下载和安装URL Rewrite IIS8默认是没有安装URL重写工具的,必须要自己下载安装. 如果IIS上默认有安装Web平台安装程序,我们可以使用平台自动安装URL Rewrite重写工具,打开I ...

  4. linux(centos、ubuntu)网卡配置文件不生效

    今天遇到个问题,服务器上ifcfg配置了eth0文件,但是通过ifcfg命令检查发现网卡IP配置并未生效. 然后通过如下配置修正: ubuntu: # vim /etc/default/grub在”G ...

  5. 安装python File "/usr/bin/pip", line 11, in <module> sys.exit(main()) File "/usr/lib/python3.4/site-packages/pip/__init__.py", line 215, in main locale.setlocale(locale.LC_ALL, '') File "/u

      $ uname -a Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u6 (2015-11-09) x86_64 GNU ...

  6. Steeltoe

    谈谈Circuit Breaker在.NET Core中的简单应用 http://www.cnblogs.com/catcher1994/p/8975192.html 前言 由于微服务的盛行,不少公司 ...

  7. Python之Dict和Set类型(入门5)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6407905.html 本文出自:[Edwin博客园] Python之Dict和Set类型 1. Python ...

  8. Angular4.0--创建类实例

    src/app/hero.ts文件: export class Hero { constructor( public id: number, public name: string) { } } sr ...

  9. 用js或JQuery模拟点击a标签的操作

    一.用js模拟点击a标签的操作. jsp代码: <a id="login" href="${pageContext.request.contextPath}/log ...

  10. boost tss.hpp源码分析

    tss.hpp定义了thread_specific_ptr,使用thread local storage 技术 1.在thread目录下的win32和pthread目录下thread_data.hpp ...