在AWS EC2上實現遠程運行JupyterNotebook

在AWS EC2上實現遠程運行JupyterNotebook

  首先,在AWS的Console中啓動一個實例,詳細步驟略,關鍵步驟如1、2所示。

1、使用的AMI

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

c.NotebookApp.ip='*' # 就是設置所有ip皆可訪問
c.NotebookApp.password = u'sha:ce...剛纔複製的那個密文'
c.NotebookApp.open_browser = False # 禁止自動打開瀏覽器
c.NotebookApp.port =8888 #可指定一個端口

啓動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了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章