Tips:该文章面向Mac和centos用户,如果是Windows用户,请先安装虚拟机。
概述
Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言。
Jupyter Notebook 的本质是一个 Web 应用程序,便于创建和共享文学化程序文档,支持实时代码,数学方程,可视化和 markdown。 用途包括:数据清理和转换,数值模拟,统计建模,机器学习等等。
小试牛刀
如果之前你还不了解jupyter,我建议先在本地来一个全方位的体验! (For Mac)
- 首先,保证你Mac应该已经安装了python(推荐python3,因为在不久的将来python2将被抛弃!)以及pip命令;
- 然后,安装jupyter:
pip install jupyter
(不同环境安装方法参考 安装jupyter ) ; - 完成上面的操作就已经安装好了!
- 最后,启动jupyter:
jupyter notebook
- 启动后会自动打开浏览器,进入首页:
在这里就可以尽情体验jupyter的所有功能了,详情参考 运行jupyter 。
书归正传
上面只是对jupyter的一个简单体验,但是为了方便在何时何地都能使用jupyter,在自己Mac上部署就不能完全满足咱们的需求了。接下来讲述如何在云主机(centos系统)上安装jupyter并通过nginx做代理提供服务。
先展示一下部署成果:
这是我自己的jupyter登录主页!其中,配置了用户认证、HTTPS。
开始部署
建议使用普通用户进行部署!
-
安装python3及pip
yum install -y python36 python36-pip
-
安装jupyter
pip install jupyter
-
生成jupyter配置文件
jupyter notebook --generate-config
会创建目录
~/.jupyter
并且该目录下会有一个配置文件jupyter_notebook_config.py
。 -
修改配置
c.JupyterApp.config_file = '~/.jupyter/jupyter_notebook_config.py' c.NotebookApp.allow_remote_access = True # 允许远程访问 c.NotebookApp.base_url = '/jupyter' # 设置jupyter的资源主页路径,即[jupyter主页] c.NotebookApp.enable_mathjax = True # 启用mathjax c.NotebookApp.ip = '127.0.0.1' # 设置了访问该jupyter应用的来源机器只能是本机 c.NotebookApp.notebook_dir = '/home/nginx/workspace/js_python' # jupyter工作目录,所有在jupyter创建的文件都会保存到这里 c.NotebookApp.open_browser = False # 禁止启动时自动开启浏览器 c.NotebookApp.password = 'sha1:a937e51de9a1:a567707768cd50d0ac1d40a4fb739510ddb3d8cb' # 用户认证密码 c.NotebookApp.port = 8888 # 监听端口
-
上面设置了
c.NotebookApp.password
,通过以下方式获取加密后的密码:In [1]: from notebook.auth import passwd In [2]: passwd() Enter password: Verify password: Out[2]: 'sha1:a937e51de9a1:a567707768cd50d0ac1d40a4fb739510ddb3d8cb'
-
配置完成后,启动:
jupyter notebook
-
为了方便管理,讲服务加入systemctl管理:
# /usr/lib/systemd/system/jupyter.service [Unit] Description=Jupyter Management After=network.target [Service] User=nginx Group=nginx WorkDirectory=/home/nginx/workspace/js_python ExecStart=/usr/local/bin/jupyter notebook Restart=on-failure RestartSec=10 [Install] WantedBy=multi-user.target
-
启动:
$ systemctl daemon-reload $ systemctl start jupyter $ systemctl enable jupyter
配置nginx代理
nginx的安装过程在此不做赘述,参考 编译安装nginx 。
nginx配置如下:
-
ssl.conf配置:
ssl_session_timeout 10m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; ssl_buffer_size 1400; ssl_session_tickets on; ssl_session_cache shared:SSL:10m; ssl on; ssl_certificate /usr/local/nginx/conf/www.xxxx.cn.crt; ssl_certificate_key /usr/local/nginx/conf/www.xxxx.cn.key; client_max_body_size 10m; charset utf-8;
-
site.conf
server { listen 80; server_name www.xxxx.cn; rewrite ^/(.*)$ https://www.xxxx.cn/$1 permanent; } server { listen 443; server_name www.xxxx.cn; include /usr/local/nginx/conf/servers/common/ssl/www_ssl_config.conf; location /jupyter { proxy_pass http://jupyter; 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; # websocket support proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_read_timeout 86400; proxy_redirect off; # 白名单 allow 121.xx.xx.xx; deny all; } } upstream jupyter { server 127.0.0.1:8888; }
配置完成后启动nginx,在浏览器访问www.xxxx.cn/jupyter
。
Completed!