Try   HackMD

高效能網頁伺服器

contributed by < YLowy >

事前準備

1. 下載映像檔

使用 Ubuntu 20.10 (Groovy Gorilla)

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 →

這邊我下載的是
Raspberry Pi Generic (64-bit ARM) preinstalled server image For modern Raspberry Pi boards (Pi 2, Pi 3 and Pi 4).

2. 將映像檔載入 Micro SD Card

下載燒錄軟體balenaEtcher

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 →

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 →

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 →

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 →

3. 以映像檔開機樹梅派

設定新密碼

原始預設密碼 :
login as: ubuntu

password: ubuntu

4. 遠端連線到此電腦

用 putty 連進此樹梅派

修改成為固定 IP 以方便連線

先在此電腦下載 net-tools 以方便先觀察目前 IP
由於目前與實驗室申請 固定 IP,故先對此進行修改,之後可以透過 putty 對此進行 SSH 操作

先以 ifconfig 確認是否有取得 DHCP 給定 IP

生成設定 config 檔案 50-cloud-init.yaml

sudo netplan generate

對於 50-cloud-init.yaml 寫入 IP 設定

ubuntu@ubuntu:~$ cd /etc/netplan/
ubuntu@ubuntu:/etc/netplan$ ls
50-cloud-init.yaml
ubuntu@ubuntu:/etc/netplan$ cat 50-cloud-init.yaml
# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        eth0:
            addresses: [192.168.79.95/24]
            gateway4: 192.168.79.253
            nameservers:
              addresses: [8.8.8.8,8.8.4.4]
            dhcp4: no
    version: 2

安裝 OpenSSH Server

更新 apt-get

sudo apt-get upgrade

sudo apt-get update
下載 openSSH 套件
sudo apt-get install -y openssh-server

以確認 SSH 是已經開啟狀態
sudo service ssh status


ubuntu@ubuntu:/etc/netplan$ sudo service ssh status
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2020-11-25 09:05:09 UTC; 2h 4min ago
       Docs: man:sshd(8)
             man:sshd_config(5)
    Process: 1799 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
   Main PID: 1811 (sshd)
      Tasks: 1 (limit: 4434)
     CGroup: /system.slice/ssh.service
             └─1811 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups

Nov 25 09:05:09 ubuntu systemd[1]: Starting OpenBSD Secure Shell server...
Nov 25 09:05:09 ubuntu sshd[1811]: Server listening on 0.0.0.0 port 22.
Nov 25 09:05:09 ubuntu sshd[1811]: Server listening on :: port 22.
Nov 25 09:05:09 ubuntu systemd[1]: Started OpenBSD Secure Shell server.
Nov 25 09:08:20 ubuntu sshd[1883]: Accepted password for ubuntu from 192.168.79.85 port 7044 ssh2
Nov 25 09:08:20 ubuntu sshd[1883]: pam_unix(sshd:session): session opened for user ubuntu by (uid=0)
Nov 25 11:03:33 ubuntu sshd[2097]: Accepted password for ubuntu from 192.168.79.85 port 8264 ssh2
Nov 25 11:03:33 ubuntu sshd[2097]: pam_unix(sshd:session): session opened for user ubuntu by (uid=0)

透過 PuTTY 嘗試連線

login as: ubuntu
ubuntu@192.168.79.95's password:
Welcome to Ubuntu 20.10 (GNU/Linux 5.8.0-1006-raspi aarch64)

以上可以成功連線至遠端 Raspberry Pi

Raspberry Pi 架設 Webserver

本篇以 clock 進行操作

Clone the repository

$ git clone git://github.com/lpereira/lwan
$ cd lwan

Create the build directory

$ mkdir build
$ cd build

Build Lwan

$ make

接下對於進行測試 src/samples/clock/clock

進入所建立的 clock 檔案目錄下,按照文件所述建立 clock.confwwwroot 資料夾

clock.conf

clock.conf 內容使用 lwan.conf 預設內容

# Timeout in seconds to keep a connection alive.
keep_alive_timeout = 15

# Set to true to not print any debugging messages. (Only effective in
# release builds.)
quiet = false

# Set SO_REUSEPORT=1 in the master socket.
reuse_port = false

# Value of "Expires" header. Default is 1 month and 1 week.
expires = 1M 1w

# Number of I/O threads. Default (0) is number of online CPUs.
threads = 0

# Disable HAProxy's PROXY protocol by default. Only enable if needed.
proxy_protocol = false

# Enable straitjacket by default. The `drop_capabilities` option is `true`
# by default.  Other options may require more privileges.
straitjacket

listener *:8080 {
    serve_files / {
            path = ./wwwroot

            # When requesting for file.ext, look for a smaller/newer file.ext.gz,
            # and serve that instead if `Accept-Encoding: gzip` is in the
            # request headers.
            serve precompressed files = true
    }
}

wwwroot 目錄

雖然這邊沒用到,但是套用在原先 lwan.conf 檔案所需求的目錄

mkdir wwwroot

run clock

執行在 sample 中的 clock 執行檔 ,使其可以不斷接受服務

ubuntu@ubuntu:~/lwan/lwan/build/src/samples/clock$ ./clock
Loading configuration file: clock.conf
Using 4 threads, maximum 262144 sockets per thread
Listening on http://0.0.0.0:8080
Ready to serve

對於所架設 IP 之掃描

可以觀察對於一開始遠端 SSH (22) port 以及後來 https (8080) port 的開啟

ylowy@YYCs-MacBook-Air ~ % nmap 192.168.79.95
Starting Nmap 7.91 ( https://nmap.org ) at 2020-11-26 16:41 CST
Nmap scan report for 192.168.79.95
Host is up (0.0028s latency).
Not shown: 998 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
8080/tcp open  http-proxy

Nmap done: 1 IP address (1 host up) scanned in 0.22 seconds

對於該 server 進行連線

在網頁瀏覽器中輸入 192.168.79.95:8080

下一篇 lwan 的 Hello World 簡介