Ubuntu 20.04
Sponsored Link

Apache2 : SSL/TLS の設定2020/05/07

 
SSL/TLS の設定を有効化して、HTTPS による暗号化通信ができるように設定します。
[1]
[2] SSL/TLS の設定を有効化します。
root@www:~#
vi /etc/apache2/sites-available/default-ssl.conf
# 3行目:管理者アドレス変更

ServerAdmin
webmaster@srv.world
# 32,33行目:[1] で取得した証明書に変更

SSLCertificateFile      /etc/letsencrypt/live/www.srv.world/cert.pem
SSLCertificateKeyFile   /etc/letsencrypt/live/www.srv.world/privkey.pem

# 42行目:コメント解除して [1] で取得したチェインファイルに変更

SSLCertificateChainFile /etc/letsencrypt/live/www.srv.world/chain.pem

root@www:~#
a2ensite default-ssl

Enabling site default-ssl.
To activate the new configuration, you need to run:
  systemctl reload apache2

root@www:~#
a2enmod ssl

Considering dependency setenvif for ssl:
Module setenvif already enabled
Considering dependency mime for ssl:
Module mime already enabled
Considering dependency socache_shmcb for ssl:
Enabling module socache_shmcb.
Enabling module ssl.
See /usr/share/doc/apache2/README.Debian.gz on how to configure SSL and create self-signed certificates.
To activate the new configuration, you need to run:
  systemctl restart apache2

root@www:~#
systemctl restart apache2

[3] HTTP 通信を HTTPS へリダイレクトして Always on SSL/TLS とする場合は、それぞれのサイト設定に RewriteRule を記述します。
例えば、こちらの例のように設定したバーチャルホスト設定では、以下のように記述します。
[httpd.conf] (include 含む) に設定しない場合は、各ドキュメントルートの直下に [.htaccess] を配置して設定します。
root@www:~#
vi /etc/apache2/sites-available/virtual.host.conf
<VirtualHost *:80>
    DocumentRoot /var/www/virtual.host
    ServerName www.virtual.host
    ServerAdmin webmaster@virtual.host
    ErrorLog /var/log/apache2/virtual.host.error.log
    CustomLog /var/log/apache2/virtual.host.access.log combined
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

root@www:~#
a2enmod rewrite

Enabling module rewrite.
To activate the new configuration, you need to run:
  systemctl restart apache2

root@www:~#
systemctl restart apache2

[4] クライアント PC から Web ブラウザでテストページに HTTPS でアクセスして、正常にページが表示されれば OK です。
関連コンテンツ