引子
jupyter notebook(原名ipython)可是科学计算界的必备工具,友好的界面,方便的交互,支持Markdown,集中的极客们想要的一切特点,同时又制作的如此优雅和精美,真是难能可贵的又好看又好用的工具。
官网
并行计算支持
这货支持并行计算,而且很全面,像MPI,OpenMP等,支持部署在集群上,具体可以参考文档
多语言支持
这里的语言不是英语、汉语,而是不同的编程语言,包括在科学界很流行的julia、Matlab、C、C#等,超多支持,令我震惊了,具体可以看这个表。
桌面端的环境搭建
按照官网的说法,极其推荐 Anaconda,有需求的可以直接到 Anaconda官网下载可用的安装包,支持win、Linux、MacOS,真是除了手机都支持O(∩_∩)O~。
jupyter服务器搭建
网上搜了一下jupyter服务器的搭建,官方给出了很好的多用户服务端jupyterhub,但我的需求就是在VPS搭建好一个jupyter服务网页,完全自己用,所以搭建jupyterhub有些浪费资源,转而去寻找简单的jupyter服务搭建方案,别说,官网也写好文档给我们用了,但文档是用英文写的,看的不舒服,这里把要点写出来,供参考。
英文文档地址在此,英文好的小伙伴可以直接去实施。
安装 jupyter notebook
下载minicanda,并安装(miniconda就是一个精简版的Anaconda),下载地址
对于python3,就要对应下载Miniconda3,相似的是对于python2,就要下载Miniconda2
Shell
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
1
wgethttps://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
然后,安装Miniconda,Miniconda包含了一个完整的python3版本,如果加入系统的环境变量(
~/.bashrc 中加入export语句,详情可以谷歌bashrc),那么系统默认的python就是刚刚安装的miniconda中的python,这样做的好处是,直接在系统中使用pip安装需要的包之后,在网页上的jupyter也有了相应的包,很是方便。
bash Miniconda3-latest-Linux-x86_64.sh
1
bashMiniconda3-latest-Linux-x86_64.sh
之后就可以使用conda的包管理系统,安装需要的包
conda install ipython
conda install jupyter
1
2
condainstallipython
condainstalljupyter
对于python3,需要安装jupyter这个包,如果是Python2,需要安装ipython-notebook的包
至此,jupyter的安装已经完成。
配置 jupyter notebook
下面的命令会在
~/.jupyter 创建一个配置文件
jupyter_notebook_config.py
$ jupyter notebook --generate-config
1
$jupyternotebook--generate-config
可以配置的项目有很多,有时间的话,可以仔细阅读配置文件中的注释,写的很清楚。
这里要强调的是创建密码的方法,总不想让自己的jupyter服务器被其他人使用吧。
执行下面语句
python -c "import IPython;print(IPython.lib.passwd())"
1
python-c"import IPython;print(IPython.lib.passwd())"
然后将得到的sha1复制到配置文件
jupyter_notebook_config.py 中的相应位置
c.NotebookApp.password = u'sha1:2e2e65056049:3ea8b0d906e17c7d45158206c333d62b785dfcdb'
1
c.NotebookApp.password=u'sha1:2e2e65056049:3ea8b0d906e17c7d45158206c333d62b785dfcdb'
配置文件中,还有几处要修改
c.NotebookApp.ip = '127.0.0.1'
c.NotebookApp.allow_origin = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888
1
2
3
4
c.NotebookApp.ip='127.0.0.1'
c.NotebookApp.allow_origin='*'
c.NotebookApp.open_browser=False
c.NotebookApp.port=8888
如此这般之后,配置完成咯
运行
执行命令
jupyter notebook
1
jupyternotebook
就可以看到
[I 21:09:34.905 NotebookApp] Serving notebooks from local directory: /home/ipython/run_jupyter
[I 21:09:34.906 NotebookApp] 0 active kernels
[I 21:09:34.906 NotebookApp] The Jupyter Notebook is running at: http://127.0.0.1:8888/
[I 21:09:34.906 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
1
2
3
4
[I21:09:34.905NotebookApp]Servingnotebooksfromlocaldirectory:/home/ipython/run_jupyter
[I21:09:34.906NotebookApp]0activekernels
[I21:09:34.906NotebookApp]TheJupyterNotebookisrunningat:http://127.0.0.1:8888/
[I21:09:34.906NotebookApp]UseControl-Ctostopthisserverandshutdownallkernels(twicetoskipconfirmation).
这是尚不能打开网页,因为是配置在127.0.0.1上的,只有本机可以访问。
配置nginx
nginx是一个高效的服务器,著名的LNMP中的N,相信很多在VPS搭建过网站的小伙伴一定不陌生。
我假设你已经安装好了nginx,如果不会安装可以参考 lnmp.org
nginx创建一个虚拟主机vhost,然后配置文件参考下面
upstream notebook {
server localhost:8888;
}
server {
listen 80;
server_name xxx.xxxx.com;
rewrite ^/(.*) https://xxx.xxxx.com/$1 permanent;
}
server{
listen 443 ssl;
index index.html index.htm index.php default.html default.htm default.php;
server_name xxx.xxxx.com;
root /home/wwwroot/xxx.xxxx.com;
ssl_certificate /etc/letsencrypt/live/xxx.xxxx.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/xxx.xxxx.com/privkey.pem;
ssl_ciphers "EECDH CHACHA20:EECDH CHACHA20-draft:EECDH AES128:RSA AES128:EECDH AES256:RSA AES256:EECDH 3DES:RSA 3DES:!MD5";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
location / {
proxy_pass http://notebook;
proxy_set_header Host $host;
}
location ~ /api/kernels/ {
proxy_pass http://notebook;
proxy_set_header Host $host;
# websocket support
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
location ~ /terminals/ {
proxy_pass http://notebook;
proxy_set_header Host $host;
# websocket support
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
upstreamnotebook{
serverlocalhost:8888;
}
server{
listen80;
server_namexxx.xxxx.com;
rewrite^/(.*)https://xxx.xxxx.com/$1 permanent;
}
server{
listen443ssl;
indexindex.htmlindex.htmindex.phpdefault.htmldefault.htmdefault.php;
server_namexxx.xxxx.com;
root/home/wwwroot/xxx.xxxx.com;
ssl_certificate/etc/letsencrypt/live/xxx.xxxx.com/fullchain.pem;
ssl_certificate_key/etc/letsencrypt/live/xxx.xxxx.com/privkey.pem;
ssl_ciphers"EECDH CHACHA20:EECDH CHACHA20-draft:EECDH AES128:RSA AES128:EECDH AES256:RSA AES256:EECDH 3DES:RSA 3DES:!MD5";
ssl_protocolsTLSv1TLSv1.1TLSv1.2;
ssl_prefer_server_cipherson;
ssl_session_cacheshared:SSL:10m;
location/{
proxy_passhttp://notebook;
proxy_set_headerHost$host;
}
location~/api/kernels/{
proxy_passhttp://notebook;
proxy_set_headerHost$host;
# websocket support
proxy_http_version1.1;
proxy_set_headerUpgrade"websocket";
proxy_set_headerConnection"Upgrade";
proxy_read_timeout86400;
}
location~/terminals/{
proxy_passhttp://notebook;
proxy_set_headerHost$host;
# websocket support
proxy_http_version1.1;
proxy_set_headerUpgrade"websocket";
proxy_set_headerConnection"Upgrade";
proxy_read_timeout86400;
}
}
其中
fullchain.pem 与
privkey.pem 是你的网址的SSL证书,如果没有,可以参考Letsencrypt免费证书。
至此,大功告成,打开你的网址xxx.xxx.com是不是可以看到熟悉的jupyter了呢?如有疑问,欢迎留言讨论。O(∩_∩)O~