阿里云端部署Jupyter notebook环境


1、准备

  阿里云服务器

  在防火墙里面开放一个端口

2、下载环境

wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2021.04-Linux-x86_64.sh --no-check-certificate 

 下载到本地后,执行bash安装这个.sh文件,安装过程中会有一次阅读协议确认和一次是否修改安装路径的确认,向修改的话按提示即可,最后还提示了这个,直接yes

bash Anaconda3-2021.04-Linux-x86_64.sh
Do you wish the installer to initialize Anaconda3
by running conda init? [yes|no]

3、添加到环境变量

vim /etc/environment 

打开文件后,把刚才安装的路径添加进去,注意要按照下面的方式,要把前面的$PATH给添加上去,不然会找不到执行的环境,如果出现ls和其他的命令无效的情况,可以按照这个连接去改https://www.cnblogs.com/lrp184/p/12870221.html

export PATH=$PATH:/root/xiaofan/anaconda3/bin

查看是否安装成功

conda -V

4、配置anaconda环境

添加Anaconda的TUNA镜像

conda config --add channels 'https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/'

设置搜索时显示通道地址

conda config --set show_channel_urls yes

创建jupyter notebook运行环境,可以方便管理各类库,然后激活环境,如果想退出环境执行最下面的

conda create -n jupyter_notebook python=3
source activate jupyter_notebook
source deactivate#暂时不执行

5、安装jupyter notebook

conda install jupyter notebook

测试下,是否弹出下面代码,后面的allow是在提示没有root权限的时候,不过没有桌面,没有找到浏览器  

jupyter notebook --ip=127.0.0.1 --allow-root
[I 14:03:14.159 NotebookApp] The port 8888 is already in use, trying another port.
[I 14:03:14.166 NotebookApp] Serving notebooks from local directory: /usr/bin
[I 14:03:14.166 NotebookApp] 0 active kernels 

6、配置jupyter notebook远程访问

  默认jupyter notebook是不需要配置文件的,需要用一下命令生成该文件,主要配置下面这些:

  • 设置远程访问密码
  • 设置可访问ip,全局访问
  • 禁用服务器端启动浏览器
jupyter notebook --generate-config

  如果没有这个文件,可以使用上面的命令生成文件后,文件在该目录下,这个文件下存在各种配置

Writing default config to: /root/.jupyter/jupyter_notebook_config.py

  打开jupyter_notebook_config.py文件

vim /root/.jupyter/jupyter_notebook_config.py

   设置所有客户端ip都可以访问

c.NotebookApp.ip = '*'  

  打开ipython,然后调用passwd()函数输入一个密码然后生成密匙,把密匙复制出来,后面有用到

In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password: 
Verify password: 
Out[2]: 'argon2:$argon2id$v=19$m=10240,t=10,p=8$4aMzGxNJZofPmX7B4peG3Q$jqITS58AF1yMa4IQJx33qw'

  在刚才那个.py文件中加入上面生成的密匙

c.NotebookApp.ip = '*' #设置所有客户端ip都可以
c.NotebookApp.password = 'argon2:$argon2id$v=19$m=10240,t=10,p=8$4aMzGxNJZofPmX7B4peG3Q$jqITS58AF1yMa4IQJx33qw'
c.NotebookApp.open_browser = False  #设置不在服务端打开浏览器
c.NotebookApp.port = 7890  #可以设置端口
lsof -i tcp:7890  #查看这个端口所在进程

  先把服务起了,最后直接ip:port就可以

jupyter notebook --allow-root

出现了这个问题

[W 2021-12-24 16:05:41.483 LabApp] 'allow_root' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[W 2021-12-24 16:05:41.483 LabApp] 'ip' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[W 2021-12-24 16:05:41.483 LabApp] 'password' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[W 2021-12-24 16:05:41.483 LabApp] 'password' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[W 2021-12-24 16:05:41.483 LabApp] 'port' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[W 2021-12-24 16:05:41.483 LabApp] 'port' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.

把配置文件中的c.NotebookApp改成ServerApp,就不会出现这个警告了

# 将配置文件中使用到的.NotebookApp修改为.ServerApp,如:

# 将 c.NotebookApp.open_browser = False 修改为:

c.ServerApp.open_browser = False

参考:https://www.jianshu.com/p/fff4a61dee7a及https://www.jianshu.com/p/b6a57145cd18(解决的是后面那个警报的问题以及升级后密码无效的问题)

出现的问题:云服务端口在开放,但是无法正常访问

https://blog.csdn.net/kenjianqi1647/article/details/118446660这个解决了给了一个新的思路,但是遇见了新的问题

https://blog.csdn.net/qq_36274515/article/details/87643759?utm_medium=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromMachineLearnPai2~default-13.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromMachineLearnPai2~default-13.control