在AWS EC2上实现远程运行JupyterNotebook
首先,在AWS的Console中启动一个实例,详细步骤略,关键步骤如1、2所示。
1、使用的AMI
2、安全组设置
入站设置:
出站设置:
3、配置Jupyter
由于AMI中已提供Anaconda以及JupyterNotebook,对Jupyter直接进行配置
生成Jupyter的配置文件:
jupyter notebook --generate-config
运行Python终端
>>from notebook.auth import passwd
>>In [2]: passwd()
Enter password:
Verify password:
Out[2]:'sha1:ce23d945972*********63968a41f1140274’
修改配置文件jupyter_notebook_config.py
vim /home/ubuntu/.jupyter/jupyter_notebook_config.py
在Vim中输入“/+你想查找的内容即可实现快速定位”
例:/App.ip
c.NotebookApp.ip='*' # 就是设置所有ip皆可访问
c.NotebookApp.password = u'sha:ce...刚才复制的那个密文'
c.NotebookApp.open_browser = False # 禁止自动打开浏览器
c.NotebookApp.port =8888 #可指定一个端口
c.NotebookApp.notebook_dir = '/home/ubuntu' #设置主目录
启动JupyterNotebook
nohup jupyter notebook & #nohup是用来防止退出shell关闭jupyter运行,&进行后台运行
发现使用服务器的ip:8888并打不开Jupyter,安全组的设置也没有问题,这里使用nginx将Notebook的8888端口代理到外网的80端口上
4、nginx安装与配置
>> sudo apt install nginx
>> sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
设置代理需修改nginx的配置
>> vim /etc/nginx/nginx.conf
在http下加上这样一段json
server {
server_name your_domain; # 服务器域名
listen 80;
location / {
proxy_pass http://127.0.0.1:8888/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Scheme $scheme;
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;
proxy_read_timeout 120s;
proxy_next_upstream error;
}
}
再次运行JupyterNotebook,访问你的domain,即可在自己的浏览器上使用Jupyter了。