下载安装脚本并执行
wget https://repo.anaconda.com/archive/Anaconda3-2023.03-0-Linux-x86_64.sh
chmod +x Anaconda3-2023.03-0-Linux-x86_64.sh
bash Anaconda3-2023.03-0-Linux-x86_64.sh
配置环境变量
cat > /etc/profile.d/anaconda.sh << EOF
export PATH=$PATH:/data/anaconda3/bin
EOF
取消自动激活base虚拟环境
conda config --set auto_activate_base false
创建虚拟环境
conda create -n prod python=3.10
激活虚拟环境并prod中安装jupyter notebook
conda activate prod
conda deactivate #退出虚拟环境
conda install jupyter notebook -y
生成notebook密码(prod环境中执行)
python
from notebook.auth import passwd
passwd()
# 随后会要求2次输入密码,该密码用于jupyter客户端登录
# 输入后会生成一个密钥(自行保存一下,稍后在jupyter配置文件中要用)
exit()
密码
123456
加密密码
argon2:$argon2id$v=19$m=10240,t=10,p=8$ZgzNA/BWAbSUboRqabZRQY5R/T4qwoLP7Y1e9liQ
生成配置文件(prod环境中执行)
jupyter notebook --generate-config --allow-root
修改配置文件
cat /root/.jupyter/jupyter_notebook_config.py | grep -Ev '^#|^$'
c = get_config() #noqa
c.NotebookApp.allow_origin = '*' # 允许跨域
c.NotebookApp.allow_remote_access = True # 允许远程访问
c.NotebookApp.ip = '0.0.0.0' # 监听IP
c.NotebookApp.notebook_dir = '/data/notebook' # notebook存储目录
c.NotebookApp.open_browser = False # 禁止启动打开浏览器
c.NotebookApp.password = 'argon2:$argon2id$v=19$m=10240,t=10,p=8$ZgzNA/BWAbSUboRqabZRQY5R/T4qwoLP7Y1e9liQ' # 登录密码设置
c.NotebookApp.port = 9999 # 端口设置
设置开机自启
cat > /usr/lib/systemd/system/jupyter.service << EOF
[Unit]
#服务描述
Description=python jupyter Service
#指定了在systemd在执行完那些target之后再启动该服务
After=network.target
[Service]
#定义Service的运行类型,一般是forking(后台运行)
Type=simple
ExecStart=/data/anaconda3/envs/prod/bin/jupyter notebook --allow-root --ip 0.0.0.0 --no-browser
PrivateTmp=True
[Install]
WantedBy=multi-user.target
EOF
启动服务
systemctl daemon-reload
systemctl start jupyter
访问测试
http://ip地址:9999