之前有写过一篇如何在服务器搭建jupyter的文章,但是当时什么都不懂,有很多地方就是脱裤子放屁了,而且当时只能通过ip+端口的方式进行访问,并不能通过域名访问,这次主要就是想通过域名来访问jupyter服务。
访问jupyter的时候发现现在新出了个jupyter lab感觉比原来的好用,而且包含了notebook原来的功能,所以现在就搭建这个。(其实就是把jupyter notebook改成了jupyter lab)
准备
-
有nginx的服务器一台
-
域名
开始搭建
1.安装
略
2.生成配置文件
jupyter notebook --generate-config
这不可以看到生成的配置文件的地址,一般是在~/.jupyter/jupyter_notebook_config.py
里。
3.编辑配置文件
设置
c.ServerApp.allow_origin = '*' c.ServerApp.allow_remote_access = True
4.创建systemd任务
vim /etc/systemd/system/jupyter.service
向里面写入:([ ]中的内容根据自己的实际情况进行替换!!!)
[Unit] Description = jupyter lab server After = network.target syslog.target Wants = network.target [Service] Type = simple WorkingDirectory=[jupyter程序运行的根目录] ExecStart=[python的位置] -m jupyter lab --allow-root --port=8889 Restart=always [Install] WantedBy = multi-user.target
这里python的位置可以使用whereis python
来查看。
--allow-root
是root用户执行才需要添加的选项。
--port=8889
这个是程序开放的端口,爱开哪开哪,但是要用于配置后面的反向代理。
5.启动服务
systemctl start jupyter #启动服务 systemctl status jupyter #查看启动状态
这里看到running就说明运行成功了。
6.设置域名解析
解析到自己的服务器上。
7.配置nginx服务
[]处请自行替换为自己的
server{ listen 80; server_name [你的域名]; # 跨域配置 location / { proxy_pass http://127.0.0.1:[你程序运行的端口]/; # JUPYTER_PORT 为 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; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_redirect off; } access_log /www/wwwlogs/jupyter.log; error_log /www/wwwlogs/jupyter.error.log; }
jupyter notebook 做nginx反向代理 就不能run了? - 知乎 (zhihu.com)
保存配置从新载入nginx服务后就可以访问了。
总结
在nginx反向代理那里卡了很久,这里应该是跨域的问题,这一块还需要再补补。
个人博客:TnTeQAQ的blog
原地址:在线访问jupyter服务并添加域名 | TnTeQAQ的blog
有个问题想问问大家,csdn这个代码块添加了之后好像就只有登录后才能复制了,但是不添加代码块的话就没有代码高亮,看着会不会特别难受,如果有人看到这句话的话能不能在评论区说下你的看法。