在 Ubuntu 20.04 LTS 上建立單 IP 多網站

這節的文章將說明如何在單個IP上建立多個網站

首先我們先來更新一下系統

sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade

接下來我們需要安裝 Certbot 的套件

sudo add-apt-repository ppa:certbot/certbot

也把 Certbot 提供的 Apache 套件裝起來

sudo apt install python3-certbot-apache

接下來啟用UFW

sudo ufw enable

允許SSH通過UFW(兩種寫法挑一種就好)

sudo ufw allow ssh
sudo ufw allow 22

也允許Apache通過UFW

sudo ufw allow 'Apache Full'

可以輸入以下指令來查看UFW狀況

sudo ufw status

系統顯示的結果像是這樣就是正常的了

Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere                  
Apache Full                ALLOW       Anywhere                  
22 (v6)                    ALLOW       Anywhere (v6)             
Apache Full (v6)           ALLOW       Anywhere (v6)

接下來編輯 /etc/apache2/sites-available 底下的 000-default.conf 這個文件

vim /etc/apache2/sites-available/000-default.conf

新增幾個你要Apache回應的網站
ServerAdmin 指的是你的信箱
ServerName 指的是連入的網址
ServerAlias 指的是這個網站會跟ServerName的網站顯示的一樣
DocumentRoot 指的是網站檔案放置的位置

<virtualhost *:80>
    ServerAdmin hostmaster@example.com
    ServerName test.example.com
    ServerAlias test3.example.com
    DocumentRoot /var/www/test.example.com
</virtualhost>
<virtualhost *:80>
    ServerAdmin hostmaster@example.com
    ServerName test2.example.com
    DocumentRoot /var/www/test2.example.com
</virtualhost>

接下來重啟Apache,並在你的網域DNS填入紀錄,就可以看到你的網站內容了

接下來要開始設定SSL的部分
再來編輯 /etc/apache2/sites-available/default-ssl.conf 的文件
內容指是上面的80換成443

<virtualhost *:443>
        ServerName test.example.com
        ServerAlias test3.example.com
        DocumentRoot /var/www/test.example.com
</virtualhost>
<virtualhost *:443>
        ServerName test2.example.com
        DocumentRoot /var/www/test2.example.com
</virtualhost>

接下來就要呼叫 Certbot 來建立SSL憑證
此部分我是參考這裡的,有興趣可以到他的網站查看完整版的

certbot certonly --manual --agree-tos \
-d "*.example.com" \
-d example.com \
--email hostmaster@example.com \
--preferred-challenges dns \
--manual-public-ip-logging-ok \
--server https://acme-v02.api.letsencrypt.org/directory \

–manual:手動安裝憑證。 –agree-to:同意 ACME 用戶協議,不指定的話會在執行時問你。 -d 接上要申請的網域:萬用字元(*)是給子網域用,父網域要單獨申請。 –email:接收到期通知的 email。 –preferred-challenges dns:使用 DNS 方式申請(Wildcard 只能用此方式)。 –manual-public-ip-logging-ok:自動接受「詢問是否記錄 IP」,不指定的話會在執行時問你。 –server:使用 Let’s Encrypt ACME2 的伺服器 (必須使用 v2 伺服器)。

接下來依照提示,到你的網域DNS新增TXT紀錄

完成後,你的憑證應該會放在/etc/letsencrypt/live/example.com/這個路徑底下 請編輯 /etc/apache2/sites-available/default-ssl.conf 裡的檔案 將SSLCertificateFile跟SSLCertificateKeyFile的路徑換掉

SSLCertificateFile      /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile   /etc/letsencrypt/live/example.com/privkey.pem

接下來啟動 Apache 的 SSL 並重啟

sudo a2enmod ssl
sudo a2ensite default-ssl.conf
sudo systemctl restart apache2

這樣就算是完成了!

補充:如果要讓網站自動轉址到 HTTPs ,請參考下方

編輯 /etc/apache2/apache2.conf
將 AllowOverride None 改成 AllowOverride All

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

在你的網頁資料夾底下新增 .htaccess ,並加入一下內容

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
ErrorDocument 404 /404.html

讓Apache啟動重寫模組並重啟就完成了

sudo a2enmod rewrite
sudo systemctl restart apache2

今天的解說就到這邊啦!