天天看點

ChatGPT學習筆記:Amazon Lightsail安裝 jupyter Notebook

作者:如何用ChatGPT賺100萬

#挑戰30天在頭條寫日記#

Amazon Lightsail 隻有終端CMD模式,使用體驗太差而且動不動斷連接配接,後面所有的指令操作又得重新開始,如果官方提供的jupyter卻要按月收費

為此我嘗試在Lightsail上安裝Notebook,自己親測有效

安裝Anaconda和jupyter:

  1. 登入到AWS Lightsail控制台。
  2. 建立一個新執行個體,選擇作業系統Ubuntu(要選擇最新22.04以上的版本的因為autogpt需要python3.10以上版本,其他的Ubuntu版本還需要更新python)。
  3. 在執行個體啟動後,打開終端并使用以下指令安裝Anaconda:
wget https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh 
bash Anaconda3-2021.05-Linux-x86_64.sh           
ChatGPT學習筆記:Amazon Lightsail安裝 jupyter Notebook
  1. 安裝Jupyter Notebook:
sudo apt update
sudo apt install -y python3-pip
sudo pip3 install jupyter           
  1. 配置Jupyter,執行以下代碼對Jupyter直接進行配置,注意直接在/home/ubuntu/目錄下生成, 該目錄也是預設目錄
ChatGPT學習筆記:Amazon Lightsail安裝 jupyter Notebook
jupyter notebook --generate-config           
  1. 對生成配置檔案進行修改
vim /home/ubuntu/.jupyter/jupyter_notebook_config.py           
  1. 在Vim中輸入“/+你想查找的内容即可實作快速定位” 新增以下配置

    例:/App.ip

c.NotebookApp.ip='*' # 就是設定所有ip皆可通路
c.NotebookApp.open_browser = False # 禁止自動打開浏覽器
c.NotebookApp.port =9000 #可指定一個端口
c.NotebookApp.notebook_dir = '/home/ubuntu' #設定主目錄           
  1. 啟動JupyterNotebook
nohup jupyter notebook  &  #nohup是用來防止退出shell關閉jupyter運作,&進行背景運作           

到了這一步大部分環境下是可以直接運作jupyter了但是Lightsail有限制外部的通路,使用伺服器的ip:8888并打不開Jupyter,安全組的設定也沒有問題,這裡使用nginx将Notebook的8888端口代理到外網的80端口上

nginx安裝與配置

安裝nginx,安裝之後通過sudo nginx -t來檢視是否安裝成功,并且檢視配置檔案路徑

>> 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的配置, 該檔案一般是隻讀的,需要使用sudo來擷取權限

sudo vim /etc/nginx/nginx.conf           

在http下加上這樣一段json, 如果有 #include /etc/nginx/sites-enabled/*; 最好注釋掉不然會有監聽端口沖突

server {
	listen 80 default_server;
	server_name _;
location / {
	proxy_pass http://127.0.0.1:9000/;
	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;
	}
}
# 如果有 #include /etc/nginx/sites-enabled/*;  最好注釋掉不然會有監聽端口沖突
#include /etc/nginx/sites-enabled/*;
           

當修改完配置檔案後,應該運作nginx的配置檔案檢查指令,來確定沒有其他錯誤:

sudo nginx -t           

如果檢查結果沒有錯誤,可以重新啟動nginx服務:

sudo systemctl restart nginx           

通路jupyter

首先通過下面的語句拿到對應的jupyter資訊

jupyter notebook list           

以上語句将顯示運作伺服器的URL及其令牌,您可以将其複制并粘貼到浏覽器中。例如:

Currently running servers:
http://localhost:8888/?token=c8de56fa... :: /Users/you/notebooks           

直接通路外網IP, 在Password or token填寫對應的token值就可以了

ChatGPT學習筆記:Amazon Lightsail安裝 jupyter Notebook

繼續閱讀