AWS Jupyter Notebook远程服务器搭建(20240904)
背景
aws 上需要为算法同学搭建jupyter notebook开发服务器,查到的一些安装流程有的比较老了,重新写一份记录一下
安装指令
ssh连接远端服务器,这里服务器镜像是aws linux,包管理软件是yum,如果是其他包管理软件,对应命令修改即可
# 配置python环境
sudo su
yum install python311
yum install python3-pip
pip install virtualenv
virtualenv /my-jupyter-env
source /my-jupyter-env/bin/activate
# 安装并创建jupyter配置文件
pip install jupyter notebook
jupyter notebook --generate-config
# 终端输入ipython,设置密码并获取out中的内容
In [1]: from jupyter_server.auth import passwd
In [2]: passwd()
Enter password: ******
Verify password: ******
Out [2]: 'sha1:...'
# 打开配置文件,文件末尾添加如下内容
vim ~/.jupyter/jupyter_notebook_config.py
c.NotebookApp.allow_remote_access = True #允许远程连接
c.NotebookApp.ip='*' # 设置所有ip皆可访问
c.NotebookApp.password = u'sha:..' #之前复制的密码,或者输入的字符
c.NotebookApp.notebook_dir = '/' # 启动的默认目录
c.NotebookApp.open_browser = False # 禁止自动打开浏览器
c.NotebookApp.port =8888 #任意指定一个端口
# 启动jupyter notebook
jupyter notebook --allow-root
# 访问jupyter notebook
http://(服务器地址):(配置文件中设定的端口)
开机自启动
创建和编辑 systemd 服务文件
创建服务文件:
sudo vim /etc/systemd/system/jupyter.service
添加以下内容:
[Unit] Description=Jupyter Notebook [Service] Type=simple ExecStart=/bin/bash -c 'cd / && source /my-jupyter-env/bin/activate && exec jupyter notebook --allow-root' User=root Restart=always [Install] WantedBy=multi-user.target
说明
ExecStart
:使用/bin/bash -c
来确保可以运行多条命令。cd /
: 切换工作目录到根目录。source /my-jupyter-env/bin/activate
: 激活虚拟环境。exec jupyter notebook --allow-root
: 启动 Jupyter Notebook 并允许以 root 用户运行。
User=root
:因为指定了--allow-root
,所以这里使用root
用户。
完成设置
重新加载 systemd 守护进程:
sudo systemctl daemon-reload
启用并启动 Jupyter 服务:
sudo systemctl enable jupyter.service sudo systemctl start jupyter.service
检查服务状态: 确认服务已经正常启动。
sudo systemctl status jupyter.service
这样配置后,每次系统重启时,Jupyter Notebook 就会自动激活虚拟环境并在根目录下启动。
License:
CC BY 4.0