NCNU-OpenSource
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: 1101, lsa --- - Book mode https://hackmd.io/@ncnu-opensource/book # Week 09(2021/11/18) :::spoiler **想看目錄 點這裡** [TOC] ::: ## 介紹 ### 有哪些web server - 有很多伺服器軟體 - web server 的功能大同小異 - 但各有特色 - lighttpd - 適合做檔案傳輸,YouTube 以前就是用這個 - file server - Windows 唯一支援 lighttpd - 佔用的資源少 - 運作起來輕量 - 操作簡單 - 現在ubuntu使用的 - apache(遠古巨獸) - 歷史最悠久 - 以前市占率最高 - 2019 年開始被 nginx 超車 - 模組功能強大 - 因為外掛模組多,常常伴隨安全性弱點 - 占用系統資源多 - 巨肥 - 設定最麻煩 - 支援 Windows 的 webserver - nginx - 同時乘載很大的流量 - 連線數的乘載率高,且速度快 - 處理 C10K問題 - 若 1 秒 過來 10000 個 request,還是可以處理,不會丟掉 request ## lighttpd ### 安裝 `sudo apt install lighttpd` ### 啟動 - `sudo service lighttpd start` ### 查看狀態 - `sudo service lighttpd status` - 預設自動開啟 - 要確定 nginx 等其他開在 80 port 上的程式沒有在跑 - `sudo netstat -ntupl` - 看一下 run 在哪個 port > 如果原本的 80 port 被占用,就要換一個 port 才可以正常執行 - `-a` 詳列詳細項目,被隱藏的項目也會顯示出來 - 到瀏覽器輸入 `localhost:<port>` > ![](https://i.imgur.com/jJRy4bV.png =200x) 先切到 `/etc/lighttpd`目錄底下 - `cat lighttpd.conf` - 顯示預先載入的模組 - lighttpd 的設定檔 - 到`cat /etc/passwd`下看有沒有`www-data` 這個使用者 - www-data:Web server (Apache & Nginx) 預設的特定 user/group ![](https://i.imgur.com/7oKNWB0.png) - `cat /etc/lighttpd/lighttpd.conf` ![](https://i.imgur.com/j3fbkig.jpg) - server.port = 80 預設伺服器的 port 為 80 - 先切到 `cd /var/www/html` - `sudo vim lighttpd.html` > 如果沒有 sudo 是不會讓你寫的優 ```html= yayaya ``` :::danger **重要** - `sudo service lighttpd stop`先停止服務再改設定檔 ::: - `sudo vim /etc/lighttpd/lighttpd.conf` - 將 `lighttpd.html` 加在 index-file.names ![](https://i.imgur.com/C2IBkaA.png) - `sudo service lighttpd reload` 重載設定檔 - `sudo service lighttpd start` 開啟 - `sudo vim /etc/lighttpd/lighttpd.conf` ![](https://i.imgur.com/76YgZAA.png) - index-file.names: 預先跑的檔案 - url.access-deny : 過濾這些東西時,如果有人要求的話會不理他 - `.inc`檔會去儲存資料庫,通常資料庫都是機密的檔案 - 如果有人想要求下載或是讀取 `~`、`inc`都會被擋下,不讓使用者存取或下載 - static-file.exclude-extensions: 後面的這些檔案格式,不可以讓它變成靜態檔案(static file),避免讓陌生人可以隨意存取(因為靜態檔案可以下載、儲存) - `/etc/lighttpd/conf-available/99-unconfigured.conf`: 設定檔 - 把"lighttpd.html"加進去 ![](https://i.imgur.com/lFXXanc.png) - 開啟瀏覽器輸入 localhost:8080(如果已經將 port 改成 8080 的話) ![](https://i.imgur.com/0ELqHWq.png =400x) > 1. `sudo vim /etc/lighttpd/lighttpd.conf` 將 lighttpd.html 加入到 index-file.names > 2. `sudo vim /etc/lighttpd/conf-available/99-unconfigured.conf` 將 ighttpd.html 加到 index-file.names<br/> > ※ 要看一下 99-unconfigured.conf 有沒有 softlink 到 /etc/lighttpd/conf-enabled 底下,因為 web server 都是看 enable 檔案去執行 - > `ln -s /etc/lighttpd/conf-available/99-unconfigured.conf /etc/lighttpd/conf-enabled/` > 完成以上兩步驟就可以在瀏覽器看到自己新增的檔案了~ - `sudo lighty-enable-mod` - 將 lighttpd 設定檔 enable - 可以順便看到哪些模組還沒載入 > ![](https://i.imgur.com/4DMtfkw.png) - `sudo service lighttpd force-reload` 強迫重啟 - `sudo service lighttpd status `看狀態 - `sudo lighty-disable-mod` - 將 lighttpd 設定檔 disable > ![](https://i.imgur.com/05tYYmt.png) - `ls -al /etc/lighttpd/conf-enabled/` ![](https://i.imgur.com/N3kEpln.png) > 看到哪些檔案是link檔 - available是個倉庫,甚麼都能丟,直到利用`ls -al /etc/lighttpd/conf-enabled/`引入enable內,才是成功引用 - `/etc/lighttpd/lighttpd.conf` - `server.port` 80 改成 8080 - 連上 localhost:80 會連不上 - 連上 localhost:8080 就能連上 > ![](https://i.imgur.com/4IlYeVq.png =500x) - use `sudo netstat -ntupl` to check - 查看lighttpd 是聽哪一個port >![](https://i.imgur.com/3bQYJoW.png =500x) - 模組前面的數字代表 include 順序 ![](https://i.imgur.com/hacGXd0.png =400x) - 數字和 priority 有關係,數字越小執行順序越前面 - 後面的(數字較大)東西會把前面(數字較小)補充或是**洗掉** - 99-unconfigured.conf 就是會最後 include - += 後面新增東西, := 會把東西洗掉 (覆寫) ![](https://i.imgur.com/SLQRyyc.png) - **盡量不要改到主設定檔 (/etc/lighttpd/lighttpd.conf)** > 因為可能只是一時需求需要更改,之後要改回來有時候會忘記自當初變動了甚麼。所以有另外一個方法可以做到這件事情<br/> > 1. 先將要更改的設定檔放在 /etc/lighttpd/conf-availvable.conf (設定檔案儲存區) > 2. 再 softlink 到 /etc/lighttpd/conf-enabled.conf <br/> > > 就可以取消或增加 softlink 以達到變更設定的效果 - 更改 conf-availvable 裡的檔案是為了不要改到主要的config檔 - 需要新增的設定檔,最好放在available中,如果放在enable中,在要取消該設定時會需要將設定檔刪除 - `ls /etc/lighttpd/conf-available/` - 查看 lighttpd 中的所有 conf 檔 ## Apache ### 安裝 - `sudo apt install apache2` - 2: 版本 - 啟動: `sudo service apache2 start` - `sudo service apache2 status` - 查看狀態 - 一樣預設開啟 - 如果 failed 可以 `sudo netstat -ntupl` 檢查 80 port 是否被佔用(前面的 lighttpd, nginx 之類) - 打開網頁連接localhost就可以看到網頁 > ![](https://i.imgur.com/0rJ8fWO.png) - `sudo netstat -ntupl` - 預設會開在 80 port - 只有 `sudo` 才可以看到這些 pid - ![](https://i.imgur.com/o4cuyCX.png =400x) - 先切到 `cd /etc/apache2/`,去看 apache 的設定檔 - `sudo vim apache2.conf` - envvars ### 把 port 改成8081 - `sudo vim ports.conf` ![](https://i.imgur.com/F3P6PQl.png) - `sudo service apache2 reload` - `sudo netstat -ntupl` ## Nginx ### 安裝 - `sudo apt install nginx` ### 查看狀態 - `sudo service nginx status` ### 啟動 - `sudo service nginx start` ### 停止 - `sudo service nginx stop` ### 看設定檔內容 - `sudo vim /etc/nginx/nginx.conf` - worker_processes : 運作時可以有多少processes ![](https://i.imgur.com/74Extw2.png) > available 裡面是存放所有 `.conf` 檔 > > enable 是存放啟用的 `.conf` 檔 #### `/sites-available` - enable 的內容就是 link 到 available 裡的 conf 檔 - default_server, server_name 用途 - 會先比對 server_name 如果找到就可以連線,找不到去找 default server - root /var/www/html - 看網頁的根目錄在哪(預設路徑) - `enable` 會執行的設定檔 - `avaliable` 是倉庫,會存放設定檔。不會被執行的設定檔 - `sudo vim /etc/nginx/sites-available/nameBase` ```file= server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name <server name>; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. # 試試看找目錄底下的檔案路徑,找不到檔案路徑再找有沒有這個資料夾 try_files $uri $uri/ =404; } } ``` - `location` / - try_files : 找的順序 - 找是不是檔案 `$url` - 找 `/` 底下有沒有 `<file name/folder name>` - $uri/ - 找有沒有資料夾叫這個名字 - 404 - 最後再找不到只好去這裡了 - `~ \php$` - 用正規表示式找結尾 .php 的檔案 - `~` 符合 - 有符合的請求會去叫 fastcgi 處理 - `~ /\.ht{ deny all}` - deny all : 不提供內容 - index 不輸入路徑時預設去找的檔案 - [**Linux 指令符號說明及 Shell 特殊符號**](https://rosalie1211.blogspot.com/2019/03/linuxshell.html) ### virtual host - 一台或是多台 host 共享資源提供多個服務 - 可以同一機台上面運行多個網站 - 以 domain name, port, IP 等訊息做區隔 - 依據使用者的訊息提供不同的內容 - 怎麼設定呢? - 就跟設定剛剛的default config一樣 #### name-based - 同ip位置,依照server name去尋找對應的服務,如果找不到,則執行default server的服務 - 到 `/etc/hosts` 新增一個 Domain Name - `127.0.0.1 nginx1.com` - 到`/var/www/vhost/nginx1` 建立等等要連進去的網站 - `cd /var/www` - `sudo mkdir vhost` - `cd vhost` - `sudo mkdir nginx1` - `cd nginx1` - `sudo vim name.html` - 回到`/etc/nginx/sites-available` 設定相關檔案 - `sudo vim /etc/nginx/sites-available/nameBase` - ``` file= server { listen 81; server_name nginx1.com; root /var/www/vhost/nginx1; index name.html; location /{ try_files $uri $uri/ =404; } } ``` - `cd /etc/nginx/sites-available` - `sudo vim nginx1` > 模版可以從 sites-available 裡的 default 裡面抓 - listen新的port e.g. 80 (port) - index更改檔案名稱name.html - root /var/www/vhost/nginx1 - `server_name nginx1.com;` - 在 `/etc/nginx/sites-enabled` - `sudo ln -s /etc/nginx/sites-available/nginx1 ./` - `sudo nginx -t` 檢查有沒錯誤 - 看enable是不是正常的 - ![](https://i.imgur.com/1YIEJdD.png =500x) - `sudo service nginx reload` 讓機台拿到新的設定檔 - `sudo service nginx status` - 在瀏覽器打 nginx1.com - ![](https://i.imgur.com/ukAcRv2.png =500x) #### port-based - 同ip位置,不同port number,依照port number去尋找對應的服務,如果是8082,在8082上尋找對應的server name,如果找不到,則執行8082上default server的服務 - 一樣在`/var/www/vhost`目錄底下新增 - `sudo mkdir nginx2` - `sudo vim index.html` 新增一些文字 - `sudo vim nginx2` > 模版可以從 sites-available 裡的 default 裡面抓 - listen新的port e.g. 8082 (port) - server_name _; - index更改檔案名稱index.html - root /var/www/vhost/nginx2 - `sudo ln -s ./nginx2 ../sites-enabled/` - 這樣不行,因為第一個路徑是以第二個路徑出發。要改成以下兩種 - `sudo ln -s ../sites-available/nginx2 ../sites-enabled/` - 相對路徑版 - `sudo ln -s /etc/nginx/sites-available/nginx2 /etc/nginx/sites-enabled/` - 絕對路徑版 - 可以用 telnet 來測試 vhost 是不是有設對 - `telnet 127.0.0.1 80` - ```terminel= GET / HTTP /1.1 Host: asldkasklda.com ``` - ![](https://i.imgur.com/PSZud7i.png) - 找不到有 virtual host 的 server_name 跟有人請求的 Host: 一樣的話,會抓到 default server,並回傳 default server 的內容 - 如果 Host 打 nginx1.com 就會抓到我們前面設定的 virtual host :::info - 在瀏覽器裡只打 `http://example.com` 的話,他就會預設幫你抓 80 port - `https://example.com` 的話則是 443 port ::: #### address-based - 在 listen 特別指定別人請求的是哪個 IP - `listen 127.0.0.1:8081;` :::info #### 虛擬主機 (Virtual Host) 如果有abc 三個人都用同一台伺服器來架設網站,其他人要如何分別的連到三個人的網站 有三種方法 1. name-based - 用 server name去尋找對應的服務 2. port-based - 用 port number去尋找對應的服務 3. address-based (IP-based) - 用 ip 去尋找對應的服務 ::: ## Proxy - 舉例 : 問 BT 問題 - 學生 ABC 問 BT web server 問題,因為是 keke做的,所以BT問完顆顆後才回應ABC - 但顆顆不知道有那些學生有問問題,只知道BT問我問題 - proxy : 代理(伺服器) - 幫使用者代理連到目標網站,再把網站內容傳回來給使用者 - 所以只要問 proxy ,proxy 就會幫你問好問滿,再告訴你答案 ## Reverse Proxy - 舉例 : 學生A問BT買樹梅派的問題 - 有好多個助教,因為是ㄅ瑋負責的,所以BT問完ㄅ瑋後才回應A - 但A不知道是哪個助教解答問題,只知道BT回答問題 - reverse proxy: 反向代理 - 外網訪問內網,向 proxy 問內容,內網告訴 proxy ,proxy cache 後並告訴外網內容 - 不知道他(傳來的網站)從哪抓來的網站 - 幫server代理將網頁資料傳給client,但使用者不知道proxy server連到哪台主機抓資料 - 好處 - 適合做load balance: - reverse proxy 是可以決定給哪個server處理請求,看誰最閒或是輪流也可以 - BT看到大家都在問蔣媽問題,所以以後有人來問問題他就派一個人來分配以後大家問的問題要分給誰回答 **用nginx reverse proxy 到 apache(8081) 跟 lsa.kija.io** - 切到 `sudo vim /etc/hosts` - 新增一個 domain name & IP - ex: `127.0.0.1 nginx1.proxy.com` - 回到 `/etc/nginx/sites-available/nginx_proxy` 設定相關檔案 - `sudo vim /etc/nginx/sites-available/nginx_proxy` ``` file= server { listen 80; server_name nginx1.proxy.com; location /{ # 代理伺服器訪問 apache (port = 8081) proxy_pass http://localhost:8081; } } ``` - `sudo ln -s /etc/nginx/sites-available/nginx_proxy /etc/nginx/sites-enabled` - `sudo service nginx reload` - nginx1.proxy.com:80 會看到 apache - 也可以連到外部網站 ```file= server { listen 80; server_name nginx1.proxy.com; location / { proxy_pass http://lsa.kija.io; } } ``` - `sudo vim nginx-loadB` ```file= upstream lsa.lab{ # 有兩個 port 可以分配給使用者用? server localhost:8080; server localhost:8081; } server { listen 80; server_name nginx1.loadB.com; location / { # 代理伺服器訪問 apache (port = 8081) proxy_pass http://lsa.lab; } } ``` :::info ### service - `sudo service <service_name> <操作>` - 操作有這些 - `status` 查看狀態 - `start` 開啟服務 - `stop` 關閉服務 - `reload` 不停止服務,重載設定檔 - `force-reload` 強制重載,把目前的連線全部清除然後重載 - `restart` 重新開啟,會暫時停止服務 - `sudo /etc/init.d/<service_name> <操作>` - /etc/init.d/ :存放系統服務啟動時的設定檔 - service 其實是到這裡找設定檔 - `sudo systemctl <操作> <service_name>` - 會到 `/lib/systemd/system` 找對應的檔案 - difference between service & systemctl - systemctl 能做的事情比 service 多 - service 只能做簡單的操作 - start、restart、reload... - systemctl 提供更多操作指令 - is-failed, reload-or-restart... **BT補充** - 上面提到那些操作,都是要看他提供的 script 實際做了什麼事 - `try_files $uri $uri 46` :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully