在云服务器(阿里云,腾讯云等通用)上安装jupyter-notebook并远程访问

正常安装jupyter notebook之后,运行如下命令

jupyter notebook --generate-config

生成配置文件,在个人文件目录下的.jupyter文件夹下。

vim jupyter_notebook_config.py

设置可以进入jupyter notebook的ip地址,其中的‘*’代表所有的ip过来的请求都可以访问

c.NotebookApp.ip = '*'

进入python界面,并引入包配置jupyter notebook访问密码

from notebook.auth import passwd
passwd()
Enter password: 
Verify password: 
Out[2]: 'sha1:8361f5f08937:081cdf40730cb5548e2c213ddd36813a5313192f'

一、配置服务器上的jupyter notebook远程访问

将加密的密钥写在配置文件中

c.NotebookApp.ip = '*'
c.NotebookApp.password = 'sha1:8361f5f08937:081cdf40730cb5548e2c213ddd36813a5313192f'

设置不在服务端自动打开浏览器(因为服务器没有)

c.NotebookApp.ip = '*'
c.NotebookApp.password = 'sha1:8361f5f08937:081cdf40730cb5548e2c213ddd36813a5313192f'
c.NotebookApp.open_browser = False

设置特定的端口可以访问

c.NotebookApp.port = 8888

开启指定文件夹

c.NotebookApp.notebook_dir = 'XXXXXXXXXXXXXXXXXX'

服务器端后台启动jupyter notebook

nohup jupyter notebook --allow-root  >> logs.out 2>&1 &

 二、配置jupyter notebook自动补全代码

安装代码自动补全插件

pip install jupyter_contrib_nbextensions -i https://pypi.tuna.tsinghua.edu.cn/simple
jupyter nbextensions_configurator enable --user

重新打开jupyter notebook,在编辑中可以找到Nbextensions插件,点击进入后,把disable前面的对号取消

jupyter contrib nbextension install --user --skip-running-check 

然后出现了Hinterland,勾选之后就可以代码自动补全了。

三、设置jupyter notebook开机自动启动

sudo nano /etc/rc.local #在exit 0 前加入以下命令 nohup /root/anaconda3/bin/jupyter-notebook --allow-root --notebook-dir=~ > ~/temp/jupyterout.log 2>&1 & # 指定服务项 root身份 工作路径 重定向输出 #添加权限 chmod +x /etc/rc.d/rc.local chmod +x /etc/rc.local

在树莓派下

sudo vim /etc/systemd/system/jupyter-notebook@pi.service

在其中填写

[Unit]
Description=Jupyter Notebook
[Service]
Type=simple
ExecStart=/usr/local/bin/jupyter-notebook
User=%i
[Install]
WantedBy=multi-user.target

加载到系统中

sudo systemctl enable jupyter-notebook@pi.service

启动系统中的服务

sudo systemctl start jupyter-notebook@pi.service

停止开机自启动jupyter

sudo systemctl disable jupyter-notebook@pi.service 

在树莓派ubuntu中

 

1. Ubuntu下配置jupyter开机自启与后台运行

查看jupyter-notebook安装位置

whereis jupyter-notebook

推荐采用用户目录,最好不要采用root目录,root用户为 /usr/local/bin/jupyter-notebook 普通用户为/home/$USER/.local/bin/jupyter-notebook

#新建服务文件
sudo vim /lib/systemd/system/jupyter.service

加入以下代码

[Unit]
Description=jupyter notebook
After=network.target
[Service]
Type=simple
# 这里填用户名,下同
User=starfish
EnvironmentFile=/home/starfish/.local/bin/jupyter-notebook
ExecStart=/home/starfish/.local/bin/jupyter-notebook
ExecStop=/usr/bin/pkill /home/starfish/.local/bin/jupyter-notebook
KillMode=process
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target

保存后终端依次输入以下代码

sudo systemctl daemon-reload
sudo systemctl enable jupyter.service
sudo systemctl start jupyter.service

输入以下代码观察有无报错信息

systemctl status jupyter

配置成功的话jupyter-notebook已经添加进系统服务,同时满足开机自启与后台运行
进入http://localhost:8888或者http://127.0.0.1:8888验证

其他常用控制jupyter命令

#移除jupyter服务
sudo systemctl disable jupyter.service
#重启jupyter服务
sudo systemctl restart jupyter.service
#停止jupyter服务
sudo systemctl stop jupyter.service

注意 ubuntu如果开启防火墙的话须开启8888端口

sudo ufw status # 查看防火墙状态
sudo ufw allow 8888 # 开启8888端口

参考:Ubuntu与windows下配置安装jupyter-notebook以及其开机自启、后台运行与远程访问 - it610.com

 

################不好使############四、设置jupyter notebook访问virtualenv的其它虚拟环境

1、在虚拟环境中安装ipykernel:pip install ipykernel

2、设置  python -m ipykernel install --user --name 虚拟环境名 --display-name Jupyter中要显示的名字

3、在虚拟环境中打开Jupyter notebook:Jupyter notebook

posted @ 2021-07-30 18:27  angbors  阅读(602)  评论(0)    收藏  举报