Apache に無料の SSL 証明書をインストールする

  1. 前提条件

    まず最初に Apache と SSL のモジュールがインストールされている必要があります。
    (パッケージ名「httpd」「mod_ssl」)

    インストールされていない場合はパッケージ「httpd」「mod_ssl」をインストールします。

    $ sudo dnf install httpd mod_ssl

    次に Apache が問題なくを起動することを確認します。

    $ sudo systemctl start httpd
    $ sudo systemctl status httpd
    httpd.service – The Apache HTTP Server

        Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
        Drop-In: /usr/lib/systemd/system/httpd.service.d
                        └─php-fpm.conf
            Active: active (running) since Wed 2021-12-22 12:41:24 JST; 10h ago
              Docs: man:httpd.service(8)
      Main PID: 204921 (httpd)
           Status: “Total requests: 1957; Idle/Busy workers 100/0;Requests/sec: 0.0538; Bytes served/sec: 1.7KB/sec”
            Tasks: 231 (limit: 115875)
       Memory: 42.4M
                CPU: 41.508s
        CGroup: /system.slice/httpd.service
                         ├─204921 /usr/sbin/httpd -DFOREGROUND
                         ├─229110 /usr/sbin/httpd -DFOREGROUND
                         ├─229111 /usr/sbin/httpd -DFOREGROUND
                         ├─229112 /usr/sbin/httpd -DFOREGROUND
                         ├─229113 /usr/sbin/httpd -DFOREGROUND
                         ├─229114 /usr/sbin/httpd -DFOREGROUND
                         └─229499 /usr/sbin/httpd -DFOREGROUND



  2. 「certbot」とプラグインの「python3-certbot-apache」をインストールします。

    $ sudo dnf install certbot python3-certbot-apache

    ※ CertBot は Let’s Encrypt のクライアントソフトであり、これを通して SSL の無料証明書を入手できます。
    ※ python3-certbot-apache は CertBot の Apache 用のプラグインです。nginx 用は python3-certbot-nginx になります。

  3. apache の SSL 設定ファイル「/etc/httpd/conf.d/ssl.conf」を編集します。

    「/etc/letsencrypt/options-ssl-apache.conf」を作成。(失敗しても問題なく作成されます)
    $ sudo certbot –apache

    気にせず次へ。


    $ sudo vi /etc/httpd/conf.d/ssl.conf
    ※ 仮にサーバーURLを「www.magic-object.mydns.jp」としておきます。
    1. ポート番号80番のパーチャルホストを追加しておきます。
      <VirtualHost *:80>
      ServerName www.magic-object.mydns.jp
      SSLEngine off
      </VirtualHost>
      
      ※ CertBot ではポート番号80番がパーチャルホストとして指定されている必要があります。
    2. ポート番号443番に CeatBot 用の設定を読み込ませます。
      <VirtualHost *:443>
      
      Include /etc/letsencrypt/options-ssl-apache.conf
      ServerName www.magic-object.mydns.jp:443
      


      〜以下略〜

      </VirtualHost>
      
      ※ 「/etc/letsencrypt/options-ssl-apache.conf」 は「python3-certbot-apache」パッケージをインストールした段階で存在します。

    3. Apache を再起動します。

      $ sudo systemctl restart httpd


  4.  CertBot を実行します。

    証明書を作成するとき、必ず外部(インターネット)からアクセスできる状態でなければいけません。

    「certbot –apache」を実行します。

    $ sudo certbot –apache

    Saving debug log to /var/log/letsencrypt/letsencrypt.log

    Which names would you like to activate HTTPS for?
    – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
    1: www.magic-object.mydns.jp
    – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
    Select the appropriate numbers separated by commas and/or spaces, or leave input
    blank to select all options shown (Enter ‘c’ to cancel): 1
    Requesting a certificate for www.magic-object.mydns.jp

    Successfully received certificate.
    Certificate is saved at: /etc/letsencrypt/live/www.magic-object.mydns.jp/fullchain.pem
    Key is saved at: /etc/letsencrypt/live/www.magic-object.mydns.jp/privkey.pem
    This certificate expires on 2022-04-13.
    These files will be updated when the certificate renews.
    Certbot has set up a scheduled task to automatically renew this certificate in the background.

    Deploying certificate
    Successfully deployed certificate for www.magic-object.mydns.jp to /etc/httpd/conf.d/ssl.conf
    Congratulations! You have successfully enabled HTTPS on https://www.magic-object.mydns.jp

    – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
    If you like Certbot, please consider supporting our work by:
    * Donating to ISRG / Let’s Encrypt: https://letsencrypt.org/donate
    * Donating to EFF: https://eff.org/donate-le
    – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

    ※ 「www.magic-object.mydns.jp」に対するSSL 証明書が作成され、「/etc/httpd/conf.d/ssl.conf」の内容が自動更新されています。

     
  5. apache の SSL 設定ファイル「/etc/httpd/conf.d/ssl.conf」の内容を確認します。

    $ sudo vi /etc/httpd/conf.d/ssl.conf
    ※ 仮にサーバーURLを「www.magic-object.mydns.jp」としておきます。
    1. ポート番号80番のパーチャルホスト確認。
      <VirtualHost *:80>
      ServerName www.magic-object.mydns.jp
      SSLEngine off
      RewriteEngine on
      RewriteCond %{SERVER_NAME} =www.magic-object.mydns.jp
      RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
      </VirtualHost>
      
      ※ ポート番号80番にアクセスした場合、「https://」にりダイレクトする項目が追加されている。

    2. ポート番号443番のパーチャルホスト確認。
      <VirtualHost *:443>
      

      〜以下略〜
      SSLCertificateFile /etc/letsencrypt/live/www.magic-object.mydns.jp/fullchain.pem
      SSLCertificateKeyFile /etc/letsencrypt/live/www.magic-object.mydns.jp/privkey.pem
      </VirtualHost>
      
      ※ 自動取得した SSL 証明書ファイルが追加されている。

    3. Apache を再起動します。

      $ sudo systemctl restart httpd


  6. 動作確認

  7. SSL 証明書の更新処理

    「/etc/cron.daily/certbot-renew.sh」ファイルの作成(新規)
    $ sudo vi /etc/cron.daily/certbot-renew.sh

    「/etc/cron.daily/certbot-renew.sh」ファイルの中身
    #!/bin/bash
    certbot renew
    

    「/etc/cron.daily/certbot-renew.sh」に実行権を付与
    $ sudo chmod +x /etc/cron.daily/certbot-renew.sh
     
  8. その他

    参考にしたサイトはコチラ

    他の Linux や UNIX、MacOSX を利用している場合は、コチラを参照してください。(公式)
    ※ 公式では、Snap を利用してインストールしています。

 

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です