そういうのがいいブログ

SIerで働く30代サラリーマンSEがインフラエンジニアに憧れてLinux・クラウド・AWSの勉強をするブログ

AlmaLinux 8 のApache2 環境にcertbotでLet's Encrypt SSLを利用する方法

※[PR]当ブログの記事の中にはプロモーションが含まれています。

・AlmaLinux 8にインストールしたApache2のサイトをHTTPS化する方法を知りたい。
・具体的な手順を分かりやすく教えてほしい。

こういった疑問に答えます。

本記事の内容

  1. AlmaLinux 8 のApache2 環境にcertbotでLet's Encrypt SSLを利用する方法

サトナカ (@souiunogaii)

この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。

こういった私が、解説していきます。

私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。

AlmaLinux 8 のApache2 環境にcertbotでLet's Encrypt SSLを利用する方法

事前準備① ホスト名からIPアドレスへの名前解決

[root@SV-ALTAIR ~]# host test01.sv-altair.planet.space.com
test01.sv-altair.planet.space.com has address XXX.XXX.XXX.XXX

事前準備② firewallの許可サービスへhttpsを追加

firewall-cmd --zone=domestic --add-service=https --permanent
firewall-cmd --reload
[root@SV-ALTAIR ~]# firewall-cmd --zone=domestic --add-service=https --permanent
success
[root@SV-ALTAIR ~]# firewall-cmd --reload
success
domestic (active)
  target: default
  icmp-block-inversion: no
  interfaces:
  sources: ipset:domestic
  services: http https
  ports:
  protocols:
  forward: no
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

souiunogaii.hatenablog.com

手順③ SSL通信用モジュールのインストール

dnf search mod_ssl
[root@SV-ALTAIR ~]# dnf search mod_ssl
メタデータの期限切れの最終確認: 1:22:34 前の 20230901193658秒 に実施しました。
====================================== 名前 完全一致: mod_ssl ======================================
mod_ssl.x86_64 : SSL/TLS module for the Apache HTTP Server
==================================== 名前 & 概要 一致: mod_ssl =====================================
certwatch-mod_ssl.x86_64 : SSL/TLS certificate expiry warnings for mod_ssl
dnf info mod_ssl
[root@SV-ALTAIR ~]# dnf info mod_ssl
メタデータの期限切れの最終確認: 1:22:54 前の 20230901193658秒 に実施しました。
利用可能なパッケージ
名前         : mod_ssl
エポック     : 1
バージョン   : 2.4.37
リリース     : 56.module_el8.8.0+3560+c8e5e57e.6
Arch         : x86_64
サイズ       : 139 k
ソース       : httpd-2.4.37-56.module_el8.8.0+3560+c8e5e57e.6.src.rpm
リポジトリー : appstream
概要         : SSL/TLS module for the Apache HTTP Server
URL          : https://httpd.apache.org/
ライセンス   : ASL 2.0
説明         : The mod_ssl module provides strong cryptography for the Apache Web
             : server via the Secure Sockets Layer (SSL) and Transport Layer
             : Security (TLS) protocols.
dnf install -y mod_ssl
[root@SV-ALTAIR ~]# dnf install -y mod_ssl
メタデータの期限切れの最終確認: 1:23:10 前の 20230901193658秒 に実施しました。
依存関係が解決しました。
====================================================================================================
 パッケージ    Arch         バージョン                                        リポジトリー    サイズ
====================================================================================================
インストール:
 mod_ssl       x86_64       1:2.4.37-56.module_el8.8.0+3560+c8e5e57e.6        appstream       139 k
依存関係のインストール:
 sscg          x86_64       3.0.0-7.el8                                       appstream        51 k

トランザクションの概要
====================================================================================================
インストール  2 パッケージ

ダウンロードサイズの合計: 190 k
インストール後のサイズ: 369 k
パッケージのダウンロード:
(1/2): sscg-3.0.0-7.el8.x86_64.rpm                                  651 kB/s |  51 kB     00:00
(2/2): mod_ssl-2.4.37-56.module_el8.8.0+3560+c8e5e57e.6.x86_64.rpm  1.1 MB/s | 139 kB     00:00
----------------------------------------------------------------------------------------------------
合計                                                                212 kB/s | 190 kB     00:00
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                            1/1
  インストール中   : sscg-3.0.0-7.el8.x86_64                                                    1/2
  インストール中   : mod_ssl-1:2.4.37-56.module_el8.8.0+3560+c8e5e57e.6.x86_64                  2/2
  scriptletの実行中: mod_ssl-1:2.4.37-56.module_el8.8.0+3560+c8e5e57e.6.x86_64                  2/2
  検証             : mod_ssl-1:2.4.37-56.module_el8.8.0+3560+c8e5e57e.6.x86_64                  1/2
  検証             : sscg-3.0.0-7.el8.x86_64                                                    2/2

インストール済み:
  mod_ssl-1:2.4.37-56.module_el8.8.0+3560+c8e5e57e.6.x86_64         sscg-3.0.0-7.el8.x86_64

完了しました!

確認

dnf list installed | grep mod_ssl
[root@SV-ALTAIR ~]# dnf list installed | grep mod_ssl
mod_ssl.x86_64                              1:2.4.37-56.module_el8.8.0+3560+c8e5e57e.6   @appstream 
httpd -M | grep ssl
[root@SV-ALTAIR ~]# httpd -M | grep ssl
AH00526: Syntax error on line 85 of /etc/httpd/conf.d/ssl.conf:
SSLCertificateFile: file '/etc/pki/tls/certs/localhost.crt' does not exist or is empty
上記のようなエラー「AH00526」が出たら、
一度、httpdを再起動すると解消する。
[root@SV-ALTAIR ~]# systemctl restart httpd
[root@SV-ALTAIR ~]# httpd -M | grep ssl
 ssl_module (shared)

手順④ snapd のインストール

snapcraft.io

dnf install -y snapd
[root@SV-ALTAIR ~]# dnf install -y snapd
メタデータの期限切れの最終確認: 1:54:15 前の 20230901193658秒 に実施しました。
依存関係が解決しました。
====================================================================================================
 パッケージ                            Arch            バージョン             リポジトリー    サイズ
====================================================================================================
インストール:
 snapd                                 x86_64          2.58.3-1.el8           epel             17 M
依存関係のインストール:
 checkpolicy                           x86_64          2.9-1.el8              baseos          347 k
 policycoreutils-python-utils          noarch          2.9-24.el8             baseos          253 k
 python3-audit                         x86_64          3.0.7-4.el8            baseos           86 k
 python3-libsemanage                   x86_64          2.9-9.el8_6            baseos          127 k
 python3-policycoreutils               noarch          2.9-24.el8             baseos          2.2 M
 python3-setools                       x86_64          4.3.0-3.el8            baseos          623 k
 snap-confine                          x86_64          2.58.3-1.el8           epel            3.5 M
 snapd-selinux                         noarch          2.58.3-1.el8           epel            553 k

トランザクションの概要
====================================================================================================
インストール  9 パッケージ

ダウンロードサイズの合計: 25 M
インストール後のサイズ: 69 M
パッケージのダウンロード:
(1/9): policycoreutils-python-utils-2.9-24.el8.noarch.rpm           738 kB/s | 253 kB     00:00
(2/9): python3-audit-3.0.7-4.el8.x86_64.rpm                         186 kB/s |  86 kB     00:00
(3/9): checkpolicy-2.9-1.el8.x86_64.rpm                             484 kB/s | 347 kB     00:00
(4/9): python3-libsemanage-2.9-9.el8_6.x86_64.rpm                   238 kB/s | 127 kB     00:00
(5/9): snap-confine-2.58.3-1.el8.x86_64.rpm                         7.7 MB/s | 3.5 MB     00:00
(6/9): snapd-2.58.3-1.el8.x86_64.rpm                                8.8 MB/s |  17 MB     00:01
(7/9): snapd-selinux-2.58.3-1.el8.noarch.rpm                        5.1 MB/s | 553 kB     00:00
(8/9): python3-setools-4.3.0-3.el8.x86_64.rpm                       131 kB/s | 623 kB     00:04
(9/9): python3-policycoreutils-2.9-24.el8.noarch.rpm                206 kB/s | 2.2 MB     00:11
----------------------------------------------------------------------------------------------------
合計                                                                1.9 MB/s |  25 MB     00:12
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                            1/1
  インストール中   : snap-confine-2.58.3-1.el8.x86_64                                           1/9
  インストール中   : python3-setools-4.3.0-3.el8.x86_64                                         2/9
  インストール中   : python3-libsemanage-2.9-9.el8_6.x86_64                                     3/9
  インストール中   : python3-audit-3.0.7-4.el8.x86_64                                           4/9
  インストール中   : checkpolicy-2.9-1.el8.x86_64                                               5/9
  インストール中   : python3-policycoreutils-2.9-24.el8.noarch                                  6/9
  インストール中   : policycoreutils-python-utils-2.9-24.el8.noarch                             7/9
  scriptletの実行中: snapd-selinux-2.58.3-1.el8.noarch                                          8/9
  インストール中   : snapd-selinux-2.58.3-1.el8.noarch                                          8/9
  scriptletの実行中: snapd-selinux-2.58.3-1.el8.noarch                                          8/9
  インストール中   : snapd-2.58.3-1.el8.x86_64                                                  9/9
  scriptletの実行中: snapd-2.58.3-1.el8.x86_64                                                  9/9
  scriptletの実行中: snapd-selinux-2.58.3-1.el8.noarch                                          9/9
  scriptletの実行中: snapd-2.58.3-1.el8.x86_64                                                  9/9
  検証             : checkpolicy-2.9-1.el8.x86_64                                               1/9
  検証             : policycoreutils-python-utils-2.9-24.el8.noarch                             2/9
  検証             : python3-audit-3.0.7-4.el8.x86_64                                           3/9
  検証             : python3-libsemanage-2.9-9.el8_6.x86_64                                     4/9
  検証             : python3-policycoreutils-2.9-24.el8.noarch                                  5/9
  検証             : python3-setools-4.3.0-3.el8.x86_64                                         6/9
  検証             : snap-confine-2.58.3-1.el8.x86_64                                           7/9
  検証             : snapd-2.58.3-1.el8.x86_64                                                  8/9
  検証             : snapd-selinux-2.58.3-1.el8.noarch                                          9/9

インストール済み:
  checkpolicy-2.9-1.el8.x86_64                   policycoreutils-python-utils-2.9-24.el8.noarch
  python3-audit-3.0.7-4.el8.x86_64               python3-libsemanage-2.9-9.el8_6.x86_64
  python3-policycoreutils-2.9-24.el8.noarch      python3-setools-4.3.0-3.el8.x86_64
  snap-confine-2.58.3-1.el8.x86_64               snapd-2.58.3-1.el8.x86_64
  snapd-selinux-2.58.3-1.el8.noarch

完了しました!
systemctl start snapd.socket
systemctl status snapd.socket
[root@SV-ALTAIR ~]# systemctl start snapd.socket
[root@SV-ALTAIR ~]# systemctl status snapd.socket
● snapd.socket - Socket activation for snappy daemon
   Loaded: loaded (/usr/lib/systemd/system/snapd.socket; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2023-09-01 21:32:23 JST; 3min 26s ago
   Listen: /run/snapd.socket (Stream)
           /run/snapd-snap.socket (Stream)
    Tasks: 0 (limit: 24824)
   Memory: 4.0K
   CGroup: /system.slice/snapd.socket

 901 21:32:23 SV-ALTAIR systemd[1]: Starting Socket activation for snappy daemon.
 901 21:32:23 SV-ALTAIR systemd[1]: Listening on Socket activation for snappy daemon.
systemctl enable --now snapd.socket
[root@SV-ALTAIR ~]# systemctl enable --now snapd.socket
Created symlink /etc/systemd/system/sockets.target.wants/snapd.socket → /usr/lib/systemd/system/snapd.socket.
[root@SV-ALTAIR ~]# ln -s /var/lib/snapd/snap /snap
snap install core
[root@SV-ALTAIR ~]# snap install core
core 16-2.60.2 from Canonical? installed
snap refresh core
[root@SV-ALTAIR ~]# snap refresh core
snap "core" has no updates available

手順⑤ certbotのインストール

certbot.eff.org

snap install --classic certbot
[root@SV-ALTAIR ~]# snap install --classic certbot
2023-09-01T21:34:02+09:00 INFO Waiting for automatic snapd restart...
certbot 2.6.0 from Certbot Project (certbot-eff?) installed
ln -s /snap/bin/certbot /usr/bin/certbot
[root@SV-ALTAIR ~]# ln -s /snap/bin/certbot /usr/bin/certbot
certbot --version
[root@SV-ALTAIR ~]# certbot --version
certbot 2.6.0

手順⑥ SSL証明書の作成

証明書の作成時に、httpサービスを利用するので、一時的に、firewallのpublicゾーンにhttpを追加します。

[root@SV-ALTAIR ~]# firewall-cmd --zone=public --add-service=http
success
certbot --apache
[root@SV-ALTAIR ~]# certbot --apache
Saving debug log to /var/log/letsencrypt/letsencrypt.log

Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): webmaster@space.com  ←★★最初だけ入力が必要

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y  ←★★最初だけ入力が必要

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y  ←★★最初だけ入力が必要
Account registered.


Which names would you like to activate HTTPS for?
We recommend selecting either all domains, or all domains in a VirtualHost/server block.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: test01.sv-altair.planet.space.com
2: www.test01.sv-altair.planet.space.com
3: test02.sv-altair.planet.space.com
4: www.test02.sv-altair.planet.space.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 test01.sv-altair.planet.space.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/test01.sv-altair.planet.space.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/test01.sv-altair.planet.space.com/privkey.pem
This certificate expires on 2023-11-30.
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 test01.sv-altair.planet.space.com to /etc/httpd/conf.d/test01.sv-altair.planet.space.com-le-ssl.conf
Congratulations! You have successfully enabled HTTPS on https://test01.sv-altair.planet.space.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

最後に、Congratulations! You have successfully と表示されたら成功です。

確認

[root@SV-ALTAIR test01.sv-altair.planet.space.com]# ls -l
合計 4
-rw-r--r-- 1 root root 692  91 22:01 README
lrwxrwxrwx 1 root root  59  91 22:01 cert.pem -> ../../archive/test01.sv-altair.planet.space.com/cert1.pem
lrwxrwxrwx 1 root root  60  91 22:01 chain.pem -> ../../archive/test01.sv-altair.planet.space.com/chain1.pem
lrwxrwxrwx 1 root root  64  91 22:01 fullchain.pem -> ../../archive/test01.sv-altair.planet.space.com/fullchain1.pem
lrwxrwxrwx 1 root root  62  91 22:01 privkey.pem -> ../../archive/test01.sv-altair.planet.space.com/privkey1.pem

手順⑦ バーチャルホスト用confの編集

vi  /etc/httpd/conf.d/test01.sv-altair.planet.space.com.conf
## VirtualHost01 test01.sv-altair.planet.space.com
<VirtualHost *:80>
ServerName test01.sv-altair.planet.space.com
ServerAlias www.test01.sv-altair.planet.space.com
ServerAdmin webmaster@test01.sv-altair.planet.space.com
DocumentRoot "/home/www/html/test01"
 
<Directory "/home/www/html/test01">
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Allow from all
</Directory>
 
ErrorLog logs/test01.sv-altair.planet.space.com-error_log
CustomLog logs/test01.sv-altair.planet.space.com-access_log combined

RewriteEngine on
RewriteCond %{SERVER_NAME} =test01.sv-altair.planet.space.com[OR]
RewriteCond %{SERVER_NAME} =www.test01.sv-altairplanet.space.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>

<VirtualHost *:443>
Protocols h2 http/1.1
SSLEngine on
ServerName test01.sv-altair.planet.space.com:443
ServerAlias www.test01.sv-altair.planet.space.com:443

SSLHonorCipherOrder on
SSLCipherSuite PROFILE=SYSTEM
SSLProxyCipherSuite PROFILE=SYSTEM

DocumentRoot "/home/www/html/test01"
SSLCertificateFile /etc/letsencrypt/live/test01.sv-altair.planet.space.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/test01.sv-altair.planet.space.com/privkey.pem

<Directory "/home/www/html/test01">
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Allow from all
</Directory>

ErrorLog logs/test01.sv-altair.planet.space.com-ssl-error_log
TransferLog logs/test01.sv-altair.planet.space.com-ssl-access_log
LogLevel warn
</VirtualHost>
[root@SV-ALTAIR ~]# httpd -t
Syntax OK

httpdを再起動して反映

[root@SV-ALTAIR ~]# systemctl restart httpd
[root@SV-ALTAIR ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/httpd.service.d
           mqphp-fpm.conf
   Active: active (running) since Fri 2023-09-01 22:07:57 JST; 3min 16s ago
     Docs: man:httpd.service(8)
  Process: 3760 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
 Main PID: 4096 (httpd)
   Status: "Total requests: 1; Idle/Busy workers 100/0;Requests/sec: 0.00529; Bytes served/sec:   5>
    Tasks: 278 (limit: 24824)
   Memory: 43.9M
   CGroup: /system.slice/httpd.service
           tq4096 /usr/sbin/httpd -DFOREGROUND
           tq4097 /usr/sbin/httpd -DFOREGROUND
           tq4098 /usr/sbin/httpd -DFOREGROUND
           tq4099 /usr/sbin/httpd -DFOREGROUND
           tq4100 /usr/sbin/httpd -DFOREGROUND
           mq4313 /usr/sbin/httpd -DFOREGROUND

 9月 01 22:07:57 SV-ALTAIR systemd[1]: httpd.service: Succeeded.
 9月 01 22:07:57 SV-ALTAIR systemd[1]: Stopped The Apache HTTP Server.
 9月 01 22:07:57 SV-ALTAIR systemd[1]: Starting The Apache HTTP Server...
 9月 01 22:07:57 SV-ALTAIR systemd[1]: Started The Apache HTTP Server.
 9月 01 22:07:57 SV-ALTAIR httpd[4096]: Server configured, listening on: port 443, port 80

HTTPSポートがLISTENであることを確認

ss -lt
[root@SV-ALTAIR ~]# ss -lt
State      Recv-Q     Send-Q         Local Address:Port           Peer Address:Port     Process
LISTEN     0          511                        *:http                      *:*
LISTEN     0          511                        *:https                     *:*

手順⑧ ブラウザで表示確認