Week 06(2020/10/29)

  • /etc/services: 可以查詢 port 主要是什麼服務

  • port 通訊埠 : 一個主機在選擇要接哪個服務的時候,是透過 port 來連接,要資源

    • http: 80
      • 明文進行傳輸
    • https : 443
      • 利用 TLS/SSL 加密後的密文傳輸
    • 1024 以下的 port 是預留的,分配給固定服務
    • 1024 以上為可以動態分配
    • 要有 root 權限才能修改
    • 規定常用服務是使用什麼 port ,是為了讓 client 端可以容易的找到服務
  • web server

    • 已經有很多常見的服務, apache 、 nginx 、 lighttpd
    • 回應 http 的 request(??
    • 不同的牌子的 web server 可以解決不同的問題
    • 常見的web server:
      • nginx
        • 可以同時承受很大的流量(Concurrent connection)
        • 也可以做轉發
      • lighttpd
        • 適合做檔案傳輸
        • 支援 cgi, fastcgi
      • Apache2
        • 歷史最悠久
        • 市占率最高
        • 支援模組,設定較困難/複雜
        • 占用硬體資源多
        • 設定最麻煩的
Nginx Lighttpd Apache2
適合做 Reverse proxy
可以同時承受很大的流量(Concurrent connection)
也可以做轉發
適合檔案傳輸
支援 cgi, fastcgi
歷史悠久
市占率最高
模組支援完整
軟體肥大占用硬體資源多
設定複雜
  • 為甚麼要提供不同的 server?
    • 因為他們被用來在不同的情況
  • virtual host

    指在同一機器上運行多個網站

    • name-based

      在同一機器、IP 位址,架設多個網域名稱的網站

    • IP-based

      在同一機器、不同的IP位址,架設多個網站

    • port-based

      port不同 ex:127.0.0.1:8080、127.0.0.1:8081

實作Nginx

  • 使用apt安裝Nginx:
    • sudo apt install nginx
  • 查看Nginx目前的狀況:
    • sudo service nginx status
    • 預設是開啟的

操作server的指令 (三個選項可以做選擇)

  • sudo service [服務名稱] [動作]:

    • ex, sudo service nginx stop
  • Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    • start 開啟服務
    • stop : 關閉服務
    • reload : 不停止服務,重載設定檔
    • restart : 停止服務重新開啟
    • status : 查看服務狀態
  • sudo systemctl [動作] [服務名稱]

    • 透過systemctl查看服務狀態
    • 先帶操作再帶服務的名字
    • ex, sudo systemctl status nginx.servicesudo service nginx status 結果一樣

    為什麼這兩個結果會是一致的,有沒有什麼辦法可以驗證? Jerry Wang

  • sudo /etc/init.d/nginx status

    • nginx 的操作
    • 裡面一能查看指令
  • ls -l /etc/init.d/{script_name}(操作)

    • 直接跟nginx說要查看指令
  • sudo systemctl {操作} {unit_name}

    • /lib/systemd/system
  • :w !sudo tee %: 更改伺服器讀取的index順序(錯ㄌ)

    • 解決臨時權限不足的問題
    • 安裝多個伺服器時,index會被覆蓋過去,故需去更改讀取順序
    • 指令小解釋
      • :w !: 執行外部命令
      • sudo tee: 提昇tee的權限並且運用tee的功能,也就是把後面的引數印出來或存某個檔案內
      • %: 是vim的暫存檔,存檔案路徑
  • 於網址輸入127.0.0.1: 連上Nginx服務

    • 透過 host name 連上 Server 的服務
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
  • vim /etc/hosts : 記錄這台機器上 host 對應domain name,host name 的相關資訊

    • 跑server時,優先查看這個檔案中IP跟hostname的對應
  • http:// {hostname} or {hostname}

    • 可以存取到同樣的資源
    • 已經自動轉成 ip
      • 根據/etc/hosts
      • 預設檔案內會有127.0.0.1 localhost
      • 預設檔案內會有127.0.1.1 <電腦名稱>
      • 因此打上 http://localhosthttp://<電腦名稱> 會自動跳轉
    • 網址上輸入 localhost 也可以出現一樣的頁面
    • 如果要用{hostname}進去,要在sudo vim /etc/hosts 新增127.0.0.1 {hostname}才可使用
  • 127.0.0.1 netmask 255.0.0.0: 127 之下的都是自己的

Nginx 相關設定

nginx 設定檔主要會放在 3 個地方

  • vim /etc/nginx/nginx.conf: 主設定檔

    • 包含了服務的相關設定(log 檔等等)
    • etc: Nginx(要載入的其他)設定檔
      • 在此目錄下操作時,記得加上sudo
    • 盡量不要寫設定在這裡,主要裝系統設定,讓它隨著系統更新而更新
    • 路徑&查看 : username@hostname:/etc/nginx$+cat nginx.conf
    • nginx讀nginx.conf,若有include再一一load進來
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
  • available & enabled 區隔開來以免在系統設定時互相影響

    • sites-available:設定檔的存放庫,設定檔都先放這,要運行時直接softlink到sites-enabled
      • 當成設定檔的存放庫,以便管理
    • sites-enabled: 實際讓服務運作的設定檔
      • 如上所述sites-enable/default 連結到 sites-available/default
      • server在運作時,需要reload,才會更新設定
      • 當成設定檔的存放庫,以便管理(郭助教說這是錯的)
  • sudo netstat -ntupl: 查看服務跑在哪個port上面(目前的網路狀況)

    • 目前的網路狀況,哪些port是被listen的
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
      可看到Nginx是run在80 port
    • netstat -ntul: 不會顯示特定服務被哪個port listen

Virtual host 設定

  • ls -l /etc/nginx/sites-available:查看sites-available檔案
  • vim /etc/nginx/sites-available/default
  • sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.backup: 先複製一份,避免被改壞了,還可以復原

要在 etc 下操作,都要 sudo

好像在 / (根目錄) 底下做操作都要 sudo 陳柏瑋

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

在etc下的檔案權限幾乎都是root,所以建議都用sudo執行 呂心慈

  • sudo nginx -t: 在enable的設定檔可知道有無問題

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • 練習:

    • 將 Nginx 預設聽的 port 從 80port 改到 8080port
    • 在這更改 sudo vim /etc/nginx/sites-available/default
  • sudo service nginx restart: 將更改過的設定更新至server

  • 當對服務的port做更改時,建議先停止server sudo service nginx stop,再開始 sudo service nginx start

Virtual Host 介紹

  • 利用 server 開頭來區分每一台 Virtual Host 的內容
  • root : 設定網頁的根目錄
  • index : 設定搜尋順序
  • location : 對usr輸入的路徑做何種操作
    • try_files:
      • 根據目錄,給予不一樣的操作
      • 按順序檢查文件是否存在
      • 查看root目錄下是否有相應的檔案名稱可提供給使用者
      • root中沒有的話,則去index中查看
      • root & index 皆沒有找到相應的檔案名稱,會回傳404

透過不同Domain name 導向不同 virtual host

連結到 http://nginx1.com:8080 的流量會被導向 網站根目錄在 /var/www/vhost/nginx1/ 的 virtual host

  • sudo vim /etc/hosts: 更改host的紀錄

    • 例子: 127.0.0.1 nginx1.com
  • sudo mkdir /var/www/vhost: 於 /var/www 下,建立vhost資料夾

    • sudo mkdir /var/www/vhost/nginx1: 在 vhost 下,再建立一個 nginx1 資料夾,用來存放nginx1的所有資源
    • ls -l /var/www/vhost/ : check 一下

回到根目錄

  • 修改 config 設定
  • sudo vim /etc/nginx/sites-available/nginx1
server { listen 8080; # port server_name nginx1.com; # domain name root /var/www/vhost/nginx1; #網頁根目錄位置 從這裡開始下去找 index test.html; #使用者的url沒帶參數時,直接找此檔案 location / { try_files $uri $uri/ =404; #沒有使用者要的檔案時,回傳404(`$uri`: 檔案名,`$uri/`:檔案目錄) } }

以上完成還沒被啟用

啟用 Nginx1

  • sudo ln -s /etc/nginx/sites-available/nginx1 /etc/nginx/sites-enabled/: 把nginx1連結(soft link)到enabled,讓enabled啟用nginx1

  • ls -l /etc/nginx/sites-enabled/ : check 一下

  • sudo service nginx reload:麻煩 nginx 在 enable 把裡面的設定檔去吃進來

  • sudo vim test.html: 寫一個HTML檔進行測試(寫在/var/www/vhost/nginx1/裡面)

    ​​​​<h1>nginx1</h1>
  • 在網址列輸入 http://nginx1.com:8080

Proxy Pass Request

新增host紀錄

  • 希望透過不同的 domain name 連結到不同網站
  • sudo vim /etc/hosts
    • 127.0.0.1 nginx2.proxy.com

修改 Config 設定

  • sudo vim /etc/nginx/sites-available/nginx2
server { listen 8080; # 聽 ss8080 port server_name nginx2.proxy.com; location / { proxy_pass https://www.google.com; # 導到google } }

如果是nginx要proxy到lsa.bluet.org結果出現502 bad gateway的error訊息的話可以給它指定外面的DNS server喔

server { listen 8085; server_name nginx2.proxy.com; location / { resolver 8.8.8.8; set $example lsa.bluet.org; proxy_pass http://$example; } }

apache proxy conf:

<VirtualHost *:8090> ServerName myapache.com ProxyPreserveHost On ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy> <Location /> ProxyPass http://xxxxxx.com:8091/ ProxyPassReverse http://xxxxxx.com:8091/ </Location> </VirtualHost>

記得把proxy module打開

  • sudo a2enmod proxy
  • sudo a2enmod proxy_http
  • sudo ln -s /etc/nginx/sites-available/nginx2 /etc/nginx/sites-enabled: softlink 到 sites-enabled
  • sudo nginx -t : 檢查有沒有錯
  • sudo service nginx reload : 把裡面的設定檔去吃進來
  • 在網址輸入 http://nginx2.proxy.com:8080

補充 tee

  • ps | tee lala : 把 ps 這個指令的輸出存到 lala 裡並顯示出來
    • tee : 將結果同時輸出到螢幕和檔案
  • ps | sudo tee /etc/lala
  • sudo rm /etc/lala : 記得刪掉喔,避免以後出錯

lighttpd

  • sudo apt install lighttpd: 先安裝 lighttpd

  • sudo vim /etc/lighttpd/lighttpd.conf : 查看 lighttpd.conf 這個設定檔

    • modules: 模組
      • lighttpd: 查看現在有哪些module可以用 (這個不能查看喔 by助教)
      • force-reload: 使用這個指令,讀入改變的設定
  • lighttpd-enable-mod: 查看現在有哪些module可以用

    • modules 的其中一種模組
    • 輸入ssl
  • sudo lighttpd-disable-mod 就 disable

  • cat /etc/passwd | grep www-data : 在 /etc/passwd 裡面找 www-data 這個關鍵字在哪裡

lighttpd.conf

  • url.access-deny 不能被訪問
  • static-file.exclude-extensions 不能被下載