2025.03.17

Let’s EncryptでSSL対応を行う手順
こんにちは、Maromaroの松橋です。
今回は、Let’s EncryptでSSL対応を行う手順をまとめました。
Let’s Encryptは無料でSSL証明書を発行できるため、特にサーバ管理者やウェブ開発者にとって重宝されています。私自身も頻繁に利用していますが、CLIでの設定機会はあまりないため、この記事で詳しい対応手順をまとめたいと思います。
SSL対応は、AlmaLinux OS 9環境下で行いました。
以下、具体的な手順について詳述していきます。
Let’s EncryptでSSL対応手順
まず、certbotをインストールします。
$ sudo dnf install certbot -y
certbotがインストールされているか確認します。/usr/bin/certbot と出力されていればインストール済みです。
$ which certbot
/usr/bin/certbot
インストール後、次のコマンドで証明書を発行します。
$ sudo certbot certonly --manual --preferred-challenges dns -d example.com -d www.example.com
HTTPアクセスが可能な場合はこれで証明書が発行されます。
ドメイン切替前でHTTPアクセスが不可能な場合は、以下のようにDNSレコードで認証することができます。
$ sudo certbot certonly --manual --preferred-challenges dns -d example.com -d www.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name:
_acme-challenge.example.com.
with the following value:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
※ここでtxtレコードの設定を行い、反映されたらEnter
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name:
_acme-challenge.www.example.com.
with the following value:
YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
(This must be set up in addition to the previous challenges; do not remove,
replace, or undo the previous challenge tasks yet. Note that you might be
asked to create multiple distinct TXT records with the same name. This is
permitted by DNS standards.)
Before continuing, verify the TXT record has been deployed. Depending on the DNS
provider, this may take some time, from a few seconds to multiple minutes. You can
check if it has finished deploying with aid of online tools, such as the Google
Admin Toolbox: https://toolbox.googleapps.com/apps/dig/#TXT/_acme-challenge.www.example.com.
Look for one or more bolded line(s) below the line ';ANSWER'. It should show the
value(s) you've just added.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
※ここでtxtレコードの設定を行い、反映されたらEnter
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/example.com/privkey.pem
This certificate expires on 2025-04-11.
These files will be updated when the certificate renews.
NEXT STEPS:
- This certificate will not be renewed automatically. Autorenewal of --manual certificates requires the use of an authentication hook script (--manual-auth-hook) but one was not provided. To renew this certificate, repeat this same certbot command before the certificate's expiry date.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
「Press Enter to Continue」が出力された時のEnter押下は、txtレコードが反映されてから実施します。そのため、下記コマンドで反映されているかどうかを確認してからEnter押下しましょう。
$ nslookup -type=TXT _acme-challenge.example.com 8.8.8.8
または、下記の Google Admin ToolboxでドメインでもDNSレコードが変更されているかどうか確認できます。
https://toolbox.googleapps.com/apps/dig/#TXT/_acme-challenge.example.com.
発行された証明書の確認方法についてですが、証明書は /etc/letsencrypt/live/example.com/ ディレクトリに保存されます。これを確認することで、SSL証明書が正しく発行されたことを確かめることができます。
Apacheを使用している場合、SSL証明書と秘密鍵のパスは設定ファイルに指定する必要があります。具体的には、/etc/httpd/conf.d/ssl.conf または example.com.conf ファイル内で、SSLCertificateFile(証明書のパス)と SSLCertificateKeyFile(秘密鍵のパス)を設定します。
$ cd /etc/letsencrypt/live/
$ ls -l
total *
drwxr-xr-x 2 root root 93 Dec 30 15:49 example.com
$ sudo vi /etc/httpd/conf.d/ssl.conf
or
$ sudo touch www.example.com.conf
$ sudo vi www.example.com.conf
▼ ▼ ▼
<virtualhost :443="">
ServerName www.example.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
ErrorLog /var/log/httpd/ssl_error.log
CustomLog /var/log/httpd/ssl_access.log combined
</virtualhost>
Apache設定ファイルの構文チェックします。
$ sudo apachectl configtest
Syntax OK
Webサーバーの再起動
$ sudo systemctl restart httpd
ここでSSL接続ができるようになりました。
さいごに
CLIを使用することには多少の難易度がありますが、一度手順をしっかりと理解してしまえば、次回以降の設定も簡単になります。
以上、Maromaroの松橋でした。