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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Week 11 (2024/11/21) Web Server [toc] - Book mode https://hackmd.io/@ncnu-opensource/book :::success 課堂中有問題可以放這,看到會回答 ::: ## Samba * 下載 : `sudo apt install samba` * 設定檔案 : `sudo vim /etc/samba/smb.conf` - 在虛擬機上,使用檔案總管選擇Other Locations輸入: - `smb://<SAMBA 伺服器的IP地址> /shared` smb://10.107.29.57/shared 帳號 lsa 密碼 同上 - 在Windows上,使用檔案總管輸入: - `\\<SAMBA 伺服器的 IP 地址>\shared` - 看IP: `ip a` # Web Server 先安裝lighttpd `sudo apt install lighttpd` - 查看現在的port - netstat - `sudo netstat -ntpul` :::warning 有沒有 sudo 的差別? ::: `-a`:顯示出目前所有的網路連線狀態 `-t`:顯示tcp連線 `-u`:顯示udp連線 `-p`:顯示此連線的PID `-l`:顯示LISTEN的內容 `-n`:host跟port都以數字顯示 >看有-n跟沒有-n的差異 :::info 操作Web Server的指令 ::: ## 開啟Web Server - `sudo service <service名稱> <操作方式>` service 名稱: `lighttpd`、`apache2`、`nginx` 操作方式: `start`:開啟服務 `stop`:停止服務 `restart`:重新啟用 ## lighttpd :::info - index-file.names就是紀錄有哪些預設的頁面可以顯示 小Lab - `sudo service lighttpd stop` - `sudo vim /etc/lighttpd/conf-available/99-unconfigured.conf` - 在index-file.names最前面加一個test.html - 到document-root的路徑(/var/www/html)底下新增test.html - `sudo vim /var/www/html/test.html` - > 打你想打的文字 之後把lighttpd重新開啟 - `sudo service lighttpd restart` ::: :::info 啟動Module設定檔的方法 - 方法一:使用lighty-enabled & lighty-disables 來啟用和停用modules 啟用:`sudo lighttpd-enable-mod <module 名稱>` 停用:`sudo lighttpd-disable-mod <module 名稱>` - 只需要名稱,不需要編號或是.conf - 方法二:手動-使用Softlink方式啟用設定檔 - `sudo ln -s <要被動的modules檔案> <-enabled位置>` - 要記得restart ::: ::: info 啟用Mod Userdir Lab - 在後面加上username可以到各自的家目錄 - 輸入 `vim /etc/lighttpd/conf-available/10-userdir.conf` 查看Userdir的設定檔 - `sudo lighttpd-enable-mod userdir` 使用者設定 - 建立資料夾:`mkdir ~/public_html` - 給權限 - `chmod 755 /home/<username>` <font color="#f00">這個很重要</font> - `chmod 755 ~/public_html` - 建立檔案:`vim ~/public_html/index.html` 去看看: `127.0.0.1:80/~<username>` ::: :::warning #### 把servcie port更改為8080 1.修改 Lighttpd 設定檔 `sudo vim /etc/lighttpd/lighttpd.conf` 2.找到 server.port,改為8080: `server.port = 8080` 3.建立index.html並顯示你要的東東 `sudo vim /var/www/html/index.html` >填你要的東西 4.重啟lighttpd `sudo service lighttpd restart` 5.看有沒有run在8080 port `sudo netstat -ntupl` 6.在瀏覽器輸入: `http://<伺服器的IP地址>:8080` ::: ## Apache 安裝: `sudo apt install apache2` ### Apache2實作 1. 安裝: `sudo apt install apache2` 2. 查看狀態: `sudo service apache2 status` 3. 看一下run 在哪個port: `sudo netstat -ntupl` 4. 在瀏覽器url輸入 http://127.0.0.1:80 - 看一下Apache2設定檔資料夾裡面有啥 :::warning 進入 Apache2 的配置目錄 - `cd /etc/apache2/` 使用 Vim 編輯器打開 Apache 的主要配置檔案 - `sudo vim apache2.conf` >一般不用改這個檔案來改 port,這是確認設定的步驟 - `ls` 列出 sites-available 資料夾中的檔案(存放虛擬主機設定檔) - `ls sites-available/` >這裡的檔案(例如 000-default.conf)定義了每個網站的對應資料夾、域名、或 port 設定。 修改 ports.conf - `sudo vim ports.conf` 嘗試更改port至8081,從預設的 80 改為 8081 - 找到80: >Listen 80 - 修改為8081: >Listen 8081 重開 - `sudo service apache2 restart` - `ls` 如果要把index.html改成其他東東 一樣先 `cd /var/www/html` 接著改 `sudo vim index.html` 實做 ![image](https://hackmd.io/_uploads/HJvr7Ynfyg.png) ::: ## Nginx 1. 安裝: `sudo apt install nginx` 2. 查看狀態: `sudo service nginx status` 3. 看一下run在哪個port: `sudo netstat -ntupl` 4. 在瀏覽器url輸入: ![image](https://hackmd.io/_uploads/rycYUK2Mkl.png) - nginx.conf: 主設定檔 - `cd /etc/nginx/` - `sudo vim nginx.conf` - - `ls ./sites-available/default` - `sudo service nginx restart` :::info Location 設定 lab 修改檔案 - `sudo service nginx stop ` - `sudo vim /etc/nginx/sites-available/default`新增一個location - >location /hi { try_files $uri $uri/ =401; } - 至 `/var/www/html`底下新增一個`hi`資料夾,並在裡面新增一個`index.html` ![image](https://hackmd.io/_uploads/rkrFjFnG1x.png) - >在index.html裡面打上 `<h1>HI! Is me!</h1>` 重啟服務 `sudo service nginx restart` 開網頁看 - 127.0.0.1:80 -> 預設網頁 - 127.0.0.1:80/hi ->剛剛創好的location網頁 在hi後面亂打 ![image](https://hackmd.io/_uploads/BkNq2F2GJe.png) 在根目錄底下亂打 ![image](https://hackmd.io/_uploads/rJu33Y3f1l.png) ::: warning `sudo vim /etc/nginx/sites-available/default` 把Server底下的listen 80 改成 listen 8022並存檔 `:wq` `sudo service nginx restart` 重新啟動服務 - url 打`127.0.0.1:8022` ![image](https://hackmd.io/_uploads/rJdZTthGye.png) ::: ## Virutual Host 允許一台server提供多個網站或網頁應用程式,且每個網站可以有自己的domain name ### 基於ip的虛擬主機(IP-based Virtual Host) - 將不同domain依照請求給指定資料夾幾下的網址 - example1.com 和 example2.com 都運行在同一台伺服器 - example1.com -> www/aaa.com - example2.com -> www/bbb.com ### 基於port的虛擬主機(Port-based Virtual Host) - 將不同port依照請求給指定資料夾幾下的網址 - 127.0.0.1:8011 -> 分配給 www/aaa.com - 127.0.0.1:8012 -> 分配給 www/bbb.com :::info 1. 記得先`sudo service nginx stop` 記得先備份`sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default_backup` 最後一行註解拿掉並改成8022port `server那邊` ![image](https://hackmd.io/_uploads/Sk1jl5nz1g.png) 2. 新增example.com的資料夾 - `sudo mkdir /var/www/example.com` - `sudo vim /var/www/example.com/index.html` 3. 開啟nginx - `sudo service nginx start` 4. 到預設default server `telnet localhost 8022` 5. 輸入請求網頁方式 - 記得按兩下enter ### 回傳預設的網頁(Nginx的default網頁) ``` GET / HTTP/1.1 Host: asdf(亂打) ``` ### 到 example.com(顯示/var/www/example.com底下的index.html頁面) ``` GET / HTTP/1.1 Host: example.com ``` ::: ## Proxy 目的: - 可以隱藏client身分,保護client - 可以突破對網路的一些限制,連到一些原本被限制的網站 - 可以load balence、強化安全性(隱藏內部server)、SSL加密 阿巴阿巴太長了打不下去 ::: info ### Reverse Proxy LAB 暫停nginx - `sudo service nginx stop` Nginx 虛擬主機配置 - `sudo vim /etc/nginx/sites-available/nginx-proxy` ``` server { listen 80; server_name nginxproxy; location /proxygoogle/ { proxy_pass https://google.com/; } location /proxyapache2/ { proxy_pass http://127.0.0.1:8081/; } location / { proxy_pass http://127.0.0.1:8080/; #lighttpd } } ``` 手動啟用 - `sudo ln -s /etc/nginx/sites-available/nginx-proxy /etc/nginx/sites-enabled/` 新增主機紀錄 ``` sudo vim /etc/hosts ``` 新增 `127.0.0.1 nginxproxy` 在文件裡 `nginxproxy`是你自定義的主機名稱 重啟 Nginx - `sudo service nginx restart` 訪問目標 URL(代理Apache) - `http://nginxproxy/proxyapache2/` 訪問目標 URL(代理Google) - `http://nginxproxy/proxygoogle/` 直接訪問 nginxproxy 的根目錄 - `http://nginxproxy/` ![image](https://hackmd.io/_uploads/BkQvv53fyl.png) ::: ### nginx 模擬 load balance Lab 將服務分流到localhost:8080(lighttpd) 跟 yahoo.com (到 `http://lbnginx:8070` 一直重新整理頁面的話會看到 yahoo 跟 lighttpd 輪流切換) - `sudo service nginx stop ` - 至 `/etc/nginx/sites-available/`設定相關檔案 - `sudo vim /etc/nginx/sites-available/nginx-lb` 在裡面輸入 ```# 定義負載均衡的後端服務池 upstream backend_servers { server 127.0.0.1:8080; # Lighttpd server yahoo.com:80; # Yahoo } # Nginx 作為負載均衡器,監聽 8070 端口 server { listen 8070; # 定義 Nginx 的監聽端口 server_name lbnginx; # 定義伺服器名稱 # 根據負載均衡策略將請求轉發到後端 location / { proxy_pass http://backend_servers; # 將請求代理到 backend_servers 定義的服務池 proxy_set_header Host $host; # 保留原始請求的 Host 標頭 proxy_set_header X-Real-IP $remote_addr; # 傳遞客戶端的實際 IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` soft link 到 `nginx/sites-enabled/` sudo ln -s /etc/nginx/sites-available/nginx-lb /etc/nginx/sites-enabled/ 之後再到 /etc/hosts 裡面新增一個 host `sudo vim /etc/hosts` `127.0.0.1 lbnginx` - 確認設定檔沒有問題 - `sudo nginx -t` - 沒問題就 restart - `sudo service nginx restart`

    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