那根據先前的進度,想說也來把用到的指令記錄一下
由於是用了一段時間才想到要記錄,所以有一些是努力回想才想起來的(也有可能有些忘了)
首先就在這邊列出安裝好Ubuntu22後大概執行過的指令
更新apt列表
sudo apt update
實際跑更新
sudo apt upgrade -y
底下這行則是預計使用的各種套件,當然可以自行選擇是否要安裝這些
sudo apt install build-essential curl wget git ufw unzip software-properties-common -y
也可以根據各個關鍵字去搜尋這些工具的實際用途
當然也有可能有一部分系統已經自動裝好了
sudo apt install openssh-server -y
sudo systemctl enable ssh
sudo systemctl start ssh
像是這個ssh連線,印象中在安裝系統的過程中就會問你要不要安裝,所以除非那時候選了不安裝,不然應該不用打這些
接下來是打開防火牆,並且在防火牆上開SSH的port
sudo ufw allow OpenSSH
sudo ufw enable
開防火牆的指定port,看自己需求,例如常見的80跟443
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
也可以禁止其它連線(應該預設就是禁止?)
sudo ufw default deny incoming
然後使用固定IP連線使用pppoe,所以需要安裝
sudo apt install pppoeconf -y
新增pppoe的連線設定,好像也可以直接新增設定檔
啟用pppoe連線
接下來是安裝伺服器主要的語言,這邊是使用python
sudo apt install python3 python3-pip python3-venv -y
首先安裝python主版本3,然後後面的venv是建立虛擬環境的意思
建立好虛擬環境之後打這個啟動,就會在輸入指令的地方看到(venv)就表示成功進到虛擬環境了
python3 -m venv venv
source venv/bin/activate
接下來是要記得是在虛擬環境中使用,當然如果你能確保所有專案都要使用這些套件,也可以全域安裝,我這邊是裝在虛擬環境之中
pip install flask gunicorn
這個則是選用,目前還在考慮是否使用
pip install flask-socketio psycopg2-binary
資料庫安裝這邊是選擇PostgreSQL
sudo apt install postgresql postgresql-contrib -y
以下是建立資料庫相關的指令,當然包含使用者或是密碼等等的,請替換成自己的
sudo -u postgres createuser youruser
sudo -u postgres createdb yourdb -O youruser
sudo -u postgres psql
# \password youruser
接下來安裝反向代理的Nginx
sudo apt install nginx -y
可以建立自己的設定檔,檔名可以自訂,印象中這邊好像要停用預設的設定檔
sudo nano /etc/nginx/sites-available/flask_project
這邊用nano進去後可以設定一下預設的內容
例如以下這樣
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
記得上面要打上網域
然後除了80還有443的部分(對應http跟https)
改完之後可以使用以下的指令來啟用跟重新啟動Nginx,其中-t是測試,有錯誤會直接顯示出來,算是偵錯的指令
sudo ln -s /etc/nginx/sites-available/flask_project /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
那https的部分包含了自己伺服器到Cloudflare的部分
這邊使用Let's Encrypt + Certbot來安裝SSL證書
首先是安裝
sudo apt install certbot python3-certbot-nginx -y
然後啟用,這邊有點不確定,印象中申請的時候好像要輸入一些資料
sudo certbot --nginx -d your_domain.com
接著是自動續約,好像是三個月自動續約(吧?)
sudo systemctl status certbot.timer
進入資料庫(postgres可以換成自己新增的使用者)
然後要退出資料庫的模式使用\q
其他的還有\l列出資料庫
\c mydb切換到某個資料庫
\dt 列出所有表
要執行python腳本的時候先切換到指定路徑後使用(要注意是否在虛擬環境中)
如果想要用Flask啟動web伺服器底下app要看腳本中如何設置
gunicorn -w 4 -b 0.0.0.0:8000 app:app
最後是跟系統或開關機相關
重新啟動
關機
sudo shutdown now
檢查目前網路開放的port
顯示目前連線的IP
可以檢查某個服務是否在運行中,例如nginx
sudo systemctl status nginx
那一般來說用sudo常常是需要打密碼的,不過會提示,所以看提示就知道要打密碼
目前大概整理這些,如果還有想到,應該會在加新的
如果有什麼疑問也可以直接發問,也許就會想到更多的指令,當然如果有錯也請指正,感謝~
留言
張貼留言