2024年1月5日 星期五

Centos 7 上的 apache 升級 SSL + Cert

 一、前言:

        現在的 browser 都對沒有使用 SSL 與憑證的 web 提出不安全連線的警告, 為了不要讓我的智慧門牌一直出現這問題, 打算對 Centos 上的 apache2 web daemon 升級.


二、 Steps

1. 安裝 mod_ssl 模組,更改設定檔與重新啟動

    基本上參考 [1] 中的方法, 依自己的設定做修改即可, 摘要如下:

     (1) 安裝 SSL module
           sudo yum install mod_ssl

    (2) 產生給 TLS/SSL 用的憑證
          
            請注意, 這裏產生的是給https 通訊協定的 TLS/SSL 用的私有加密憑證, 跟你去外面申請的公開憑證不同, 這個即使安裝了, 你在連線時是使用 https 了, 但仍然會說你不安全, 因為伙沒有去申請一個公開憑證去證明你網站的身份.

    (3) SSL module 安裝好了之後, 在 /etc/ssl 下會有一個 certs 資料夾. 你需要自己再建一個 private 資料夾.
          sudo mkdir /etc/ssl/private

    (4) 修改 private 權限
          sudo chmod 700 /etc/ssl/private

    (5) 使用 OpenSSL 產生自己簽署的 key 與certificate pair 
           
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt|
           執行過程會要你回答一些問題:
            <1> 國碼: 我寫 TW
            <2> 城市名: 我寫 New Taipei City
            <3> 地名: 我寫 Xin Zuang
            <4> 公司或組織名: 我寫 FJCU
            <5> 單位: 我寫 HEC
            <6> 伺服器 domain: 我寫 macgyver.fjcu.org (沒有 domain 就寫 static public IP)
            <7> 連絡人 email; 這就留你的 email 即可

        (6) 設定私有憑證
              <1> 移到你 apache 設定資料夾 /etc/httpd/conf.d
                        cd /etc/httpd/conf.d
              <2> 產生編輯一個你 domain (或IP) 的設定檔, 在此我用我的domain ,  在此編輯器你用 vi 或 nano 都可以.
                        sudo vi /etc/httpd/conf.d/macgyver.fjcu.org.conf
                      將下列資料寫到該檔案中 (ServerName 用自己的 domain)
           

              <3> 產生如果對方使用 http 連線時能自動轉到 https 
                        sudo vi /etc/httpd/conf.d/non-ssl.conf
                        將下列檔案寫到檔案內 (ServerName用自己的 domain)
                        

          (7) 修改 apache 啟動 service
                <1> 先檢查語法有沒有問題
                        sudo apachectl configtest
                <2> 重新啟動 apache service
                        sudo systemctl restart httpd.service
                <3> 防火牆要打開 443 port  (看你用那套防火牆)
                        <i> 用 iptables 的
                                sudo iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT
                                sudo iptables -I INPUT -p tcp -m tcp --dport 443 -j ACCEPT
                        <ii> 用 firewalld 的
                                sudo firewall-cmd --add-service=http
                                sudo firewall-cmd --add-service=https
                                sudo firewall-cmd --runtime-to-permanent
         (8) 測試連線
                因為你是使用私有憑證, 所以仍有憑證問題, 但可以用 https 連線了. 公有憑證等第2點來談.

2. 使用免費憑證

    你在安裝 OpenSSL 之後所安裝的是私人憑證(可想像是私人信用), 在網路上沒有人要相信你, 所以你必須找到一個可以發公開憑證的單位給你發憑證去認可你. 但憑什麼要免費幫你做公開認可呢? 所以多數出來幫你擔保的都要收費. 

    在此我們介紹一個免費的發公開憑證的服務, 但一次效期 3 個月, 期限到再去請. 相關文件可先去 Let's Encrypt[2] 網站上先閱讀文件, 在此直接以 Centos 7 的 apache 做範例. 我選用的是 certbot [3].

    (1) 選擇使用的 web daemon 與 OS
            https://certbot.eff.org/instructions

    (2) 安裝 snap[4], 我以Centos 為例
            依據你的 Centos 版本先選擇安裝 EPEL (我是 Centos 7)
            <1> 
sudo yum install epel-release

      <2> 透過 EPEL repo 安裝 snapd
            
sudo yum install snapd

      <3> 透過 systemd enable snapd socket
            
sudo systemctl enable --now snapd.socket

      <4> 產生 snapd library 連結
            
sudo ln -s /var/lib/snapd/snap /snap

  (3) 移除你系統先前安裝過的 certbot
        
sudo yum remove certbot

  (4) 安裝 certbot
        sudo snap install --classic certbot

  (5) 做指令連結
        sudo ln -s /snap/bin/certbot /usr/bin/certbot

  (6) 取得公開憑證
        sudo certbot certonly --apache

  (7) 測試是否可自動取得新憑證
        sudo certbot renew --dry-run

  (8) 將上述寫到你的 crontab 中

3. 完工



=============================

References

1. https://www.digitalocean.com/community/tutorials/how-to-create-an-ssl-certificate-on-apache-for-centos-7 

2. https://letsencrypt.org/zh-tw/docs/

3. https://certbot.eff.org/

4. https://snapcraft.io/docs/installing-snapd