# 在 Oracle Cloud 上架設 Nginx 網站 ## 作者說 我不是什麼Nginx強者,反正我架起來了,就寫個筆記。 ## 前提 1. 你有Oracle Cloud 帳號了。 2. 本文視角是 ubuntu 用戶,如果你是,那恭喜。 # 開始 ## 創建 VCN 左上選單 Networking Virtual Cloud Networks ![image](https://hackmd.io/_uploads/r1Z2f_Y4kl.png) Start VCN Wizard ![image](https://hackmd.io/_uploads/HydTf_YNJl.png) Create VCN with Internet Connectivity 取個 VCN Name 確定這行是 public subent ``` Subnet name: public subnet-asd ``` 點擊新的VCN ![image](https://hackmd.io/_uploads/H1hy7dtEyx.png) ## 新增Security Lists 選擇 Security Lists ![image](https://hackmd.io/_uploads/SJ4GQ_KVJl.png) Create Security List 取個名字 Another Ingress Rule 依照圖填寫 ![image](https://hackmd.io/_uploads/HyQPXOFVkx.png) Create Security List 點擊subnets ![image](https://hackmd.io/_uploads/HJPo7dFEyl.png) 點擊subnet ![image](https://hackmd.io/_uploads/rktnXdt4Jg.png) 點擊Add Security List 選擇Security List 點擊Add Security List 點擊你的Security Lists ![image](https://hackmd.io/_uploads/SkSZNuYE1x.png) 確認長的像下圖 ![image](https://hackmd.io/_uploads/SyQ84utEye.png) ## 創建oracle instance 左上菜單 Compute Instances Create Instance 在Image and shape區塊選擇Edit 選擇Image為Ubuntu 20.04 在Primary VNIC information區塊選擇Edit 選擇你的VCN ![image](https://hackmd.io/_uploads/BJfbruFNkg.png) 在Add SSH keys區塊,添加你的.pub key。 對於ubuntu用戶,使用 ``` ssh-keygen -t rsa -b 2048 -f "/home/你的用戶名/.ssh/instance-ubuntu-1234" cat ~/你的用戶名/.ssh/instance-ubuntu-1234.pub 拷貝後貼到Paste public keys ``` 點擊 Create ## ssh 到 instance 找到以下區塊 ![image](https://hackmd.io/_uploads/HypFUdKNJl.png) 複製IP。 連到實例。 ``` ssh -i ~/.ssh/instance-ubuntu-1 ubuntu@你的IP ``` ## 在instance中操作 裝nginx ``` sudo apt update && sudo apt upgrade -y sudo apt install nginx -y sudo systemctl start nginx sudo systemctl enable nginx ``` ## 設定防火牆 ``` sudo ufw allow 'Nginx Full' sudo ufw reload ``` 嘗試 ``` sudo ufw status ``` 另一個防火牆 ([ref](https://stackoverflow.com/questions/62326988/cant-access-oracle-cloud-always-free-compute-http-port)) ``` sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 80 -j ACCEPT sudo netfilter-persistent save ``` 在瀏覽器輸入 ``` http://你的IP ``` 可能存在502或別的錯誤訊息,但若是錯誤是無法連上,則上述步驟有錯誤。 ## 配置 Nginx `sudo vim /etc/nginx/conf.d/default.conf` ``` upstream api { server localhost:5000; server localhost:5001; } server { listen 80; listen [::]:80; server_name SERVER_IP; root /home/ryan; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / { proxy_pass http://api/; } } ``` ## 一個監聽Port:5000的範例 安裝Flask ``` sudo apt update sudo apt install python3-pip -y pip3 install flask ``` `vim /home/ubuntu/app.py` ``` from flask import Flask app = Flask(__name__) @app.route("/") def home(): return "Hello, Nginx and Flask!" if __name__ == "__main__": app.run(host="127.0.0.1", port=5000) ``` 啟動應用 ``` python3 /home/ubuntu/app.py ``` 去瀏覽器中F5,應該能看到`Hello, Nginx and Flask!`。 ctrl+c中斷。 現在要配置後台運行 `sudo vim /etc/systemd/system/flask.service` ``` [Unit] Description=Flask App After=network.target [Service] User=ubuntu WorkingDirectory=/home/ubuntu ExecStart=/usr/bin/python3 /home/ubuntu/app.py Restart=always [Install] WantedBy=multi-user.target ``` ``` sudo systemctl daemon-reload sudo systemctl start flask.service sudo systemctl enable flask.service ``` 嘗試 ``` sudo systemctl status flask.service ``` 測試,連接到自己的Port 5000 ``` curl http://127.0.0.1:5000 ``` ## Nginx 一些基本信息 重置 Nginx ``` sudo nginx -t sudo systemctl restart nginx ``` 查看主配置 ``` /etc/nginx/nginx.conf /etc/nginx/conf.d/ ``` 查看 log 文件 ``` cat /var/log/nginx/xxx.log ``` 就我所知,Nginx的設定非常複雜和龐大。他的[documentation在這](https://nginx.org/en/docs/) ,如果你對flask感興趣。這是[documentation](https://flask.palletsprojects.com/en/stable/)。 這個教學文件大概就這樣。 你有一台自己的雲伺服器,公網IP。 在上頭跑應用,可以通過瀏覽器訪問應用。 並且在脫離ssh後服務可以繼續運行。 這步驟是學習html必備的一步,畢竟寫網頁就是要在遠端看到嘛。 [後記: 更多nginx 筆記](https://hackmd.io/t2E3rmjbRC6UFwa9oWnoGQ)