主要是參考網路上的這一篇:http://www.jeffreybolle.com/blog/run-google-go-web-apps-behind-apache

作者給出了一個簡單的 Go Web 程式:

package main

import(
    "http"
    "log"
    "os"
)

const resp = `<html>
    <head>
        <title>Simple Web App</title>
    </head>
    <body>
        <h1>Simple Web App</h1>
        <p>Hello World!</p>
    </body>
</html>`

func handler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte(resp))
}

func main() {
    http.HandleFunc("/", handler)
    err := http.ListenAndServe(":8080", nil)

    if err != nil {
        log.Println(err)
        os.Exit(1)
    }
}

這個 Web 程式主要是監聽 8080 Port,用 go build 之後得到的執行檔可直接作為 Web Server 運行。然後在 Apache 上安裝幾個 Module 後重啟 Apache。

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod rewrite
sudo service apache2 restart

接下來在 /etc/apache2/sites-available/ 目錄下建立 VirtualHost 的設定檔案 (gowebsite):

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName www.example.com
    ServerAlias example.com

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all

        RewriteEngine on
        RewriteRule ^(.*)$ http://localhost:8080/$1 [P,L]
    </Directory>
</VirtualHost>

接著在 Apache 啟動這個設定檔,將此 VirtualHost 設為啟動:

sudo a2ensite gowebsite
sudo service apache2 restart

整個過程就是在 VirtualHost 設定檔中設定此站台資料透過 Proxy Module 來由 localhost:8080 取得,當然這邊也可以同時在很多不同的 Port 中執行很多的 VirtualHost 站台。

最後修改日期: 2022-06-04

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。