Ubuntu20+Apache2で構築したWEBサイトをLet’s EncryptでSSL化

Ubuntu
スポンサーリンク


以前、Nginxで構築したWEBサイトをLets Encryptを使用してSSL化しました。

Nginxで構築したWEBサイトをLets EncryptでSSL化
WEBサイトを作るなら常時SSL化は当たり前ということで、「Let's Encrypt」と「certbot」を使用して無料でSSL化します。 「Let's Encrypt」は無料でSSL証明書を発行してくれるサービスですが、証明書の有効期...

今回はApche2で構築したサイトを同じくLets Encryptを使用してSSL化します。

スポンサーリンク

サーバ環境

Ubuntu

$ cat /etc/issue
Ubuntu 20.04.5 LTS \n \l

Apache

$ apachectl -v
Server version: Apache/2.4.41 (Ubuntu)
Server built:   2022-06-14T13:30:55

Cerbotインストール&設定

CerbotはLet’s Encryptの無料SSL証明書を発行、WEBサーバへの設定を自動で行ってくれます。
Let’s Encryptの無料SSL証明書は有効期限が3ヶ月なので自動ですべて設定してくるのは有り難いです。

$ sudo apt-get install certbot python3-certbot-apache

Certbotを実行してSSL証明書の発行と設定を行います。

$ sudo certbot --apache

いくつか質問されます。
Cerbotは本当に痒い所に手が届くのでHTTPでアクセスした場合にHTTPSにリダイレクトする設定を追加してくれます。
リダイレクトさせたい場合は「2」を入力してEnterキーを入力します。

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):2

SSL証明書が発行されているか確認します。
この後のApache設定で必要になります。

$ sudo cd /etc/letsencrypt/live/example.com

Cerbotが動いているか確認します。

$ sudo systemctl status certbot.timer

mod_sslを有効化

ApacheでSSLの設定を使用にするために、mod_sslモジュールを有効化します。

$ sudo a2enmod ssl

Apache2の設定

修正する2つのファイル
/etc/apache2/sites-available/000-default.conf
/etc/apache2/sites-available/default-ssl.conf
$ sudo vi /etc/apache2/sites-available/000-default.conf

ServerNameを設定します。

先ほどのCerbotの実行でHTTPからHTTPSへリダイレクトの設定をした場合は、その記述があることを確認します。
「リダイレクトしない」にした場合でも、ここで手動でHTTPでアクセスされた場合にHTTPSにリダイレクトする設定をすればOKです。
※RewriteEngine、RewriteCond、RewriteRuleの3行

<VirtualHost *:80>
    :
    :
    ServerName WEBサイトのURL(example.com)
    :
    :
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
$ sudo vi /etc/apache2/sites-available/default-ssl.conf

ServerName、DocumentRootとSSL証明書の設定します。
それぞれの環境に合わせてパスを記述してください。

<IfModule mod_ssl.c>
        <VirtualHost _default_:443>
                :
                ServerName WEBサイトのURL(example.com)
                DocumentRoot = /var/www/html
                :
                :
                SSLCertificateFile      /etc/letsencrypt/live/example.com/cert.pem
                SSLCertificateKeyFile   /etc/letsencrypt/live/example.com/privkey.pem
                SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
                :
                :
                :
        </VirtualHost>
</IfModule>

設定を有効化します。

$ sudo a2ensite default-ssl

Apache再起動

設定を反映さえるためにApacheを再起動します。

$ sudo systemctl restart apache2

ブラウザでhttpsでWEBサイトがアクセスできるかとHTTPでアクセスした時にHTTPSにリダイレクトされるかを確認して完了です。