AWS

【Let’s Encrypt】【EC2】Amazon Linux 2023で無料のSSL証明書の設定

Let’s Encrypt
o_wani

mazon Linux 2023)で無料のSSL証明書である「Let’s Encrypt」をインストールする手順を記載します。そんなに複雑なものではありません。

こちらのサイトを参考にしました。

cerbotコマンドのインストール

「Let’s Encrypt」を使用するためにcertbotコマンドをインストールします。

certbotコマンドは、Let’s Encrypt CA(認証局)から無料のSSL/TLS証明書を取得し、インストールし、更新するためのツールです。

a.python3、augeas-libs、pipのインストールします。

PythonとPythonパッケージのインストールと管理を行うためのツールをインストールします。

$ sudo dnf install -y python3 augeas-libs pip
$ sudo python3 -m venv /opt/certbot/
$ sudo /opt/certbot/bin/pip install --upgrade pip
Requirement already satisfied: pip in /opt/certbot/lib/python3.9/site-packages (21.3.1)
Collecting pip
  Downloading pip-23.3.1-py3-none-any.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 5.7 MB/s            
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.3.1
    Uninstalling pip-21.3.1:
      Successfully uninstalled pip-21.3.1
Successfully installed pip-23.3.1

b.certbotのインストールします。

certbotコマンドをインストールします。

$ sudo /opt/certbot/bin/pip install certbot
Collecting certbot
  Downloading certbot-2.7.3-py3-none-any.whl.metadata (8.4 kB)
Collecting acme>=2.7.3 (from certbot)
  Downloading acme-2.7.3-py3-none-any.whl.metadata (1.4 kB)
Collecting ConfigArgParse>=1.5.3 (from certbot)
  Downloading ConfigArgParse-1.7-py3-none-any.whl.metadata (23 kB)
Collecting configobj>=5.0.6 (from certbot)
  Downloading configobj-5.0.8-py2.py3-none-any.whl (36 kB)
Collecting cryptography>=3.2.1 (from certbot)
...
...
...

c./opt/certbot/bin/certbot というパスのファイルに対して /usr/bin/certbot という名前のシンボリックリンクを作成します。

これにより、ユーザーはcertbotコマンドを直接実行できるようになり、フルパスを指定する必要がなくなります。

$ sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot

証明書の発行

ドメインに対して、証明書を発行します。前提として

  • (WebサーバーApacheの場合)Apacheを停止する
  • EC2のIPアドレスと対象のドメインを紐づけておくこと
  • ドメイン名は今回の例では、owani.net
$ sudo certbot certonly --standalone

a.メールアドレスを入力してください。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): info@owani.net

b.Let’s Encryptの利用規約に同意しますか?

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

c.Let’s EncryptプロジェクトとCertbotの開発を支援している非営利団体にメールアドレスを共有しますか?(今回私は不要なのでN)

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

d.ドメインを入力してください。

Account registered.
Please enter the domain name(s) you would like on your certificate (comma and/or
space separated) (Enter 'c' to cancel):owani.net

以上です。

必要なミドルウエアインストールと証明書の定期更新

a.mod_sslのインストールをします。

ApacheにSSL(Secure Sockets Layer)およびTLS(Transport Layer Security)プロトコルのサポートを提供するモジュールです。

$ sudo dnf install mod_ssl

b.cronのインストールをします。

SSL証明書の更新を定期実行するためスケジュールに基づいて定期的にタスクを実行するためのジョブスケジューラです。

$ sudo dnf install cronie-noanacron

c.SSL証明書の更新を行うコマンドを設定します。

$ crontab -e

夜中の2時30分に更新チェックします。

30 2 * * * root certbot renew --post-hook "systemctl reload httpd" --no-self-upgrade

ApacheのSSL設定

ApacheにSSL証明書の設定を行います。

Listen 443 https

<VirtualHost *:443>
ServerName owani.net
DocumentRoot /var/www/html
SSLEngine on

<Files ~ "\.(cgi|shtml|phtml|php3?)$">
    SSLOptions +StdEnvVars
</Files>
<Directory "/var/www/cgi-bin">
    SSLOptions +StdEnvVars
</Directory>
<Directory "/var/www/html/">
    AllowOverride All
</Directory>

SSLCertificateFile /etc/letsencrypt/live/owani.net/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/owani.net/privkey.pem

</VirtualHost>

その後、Webサーバーを(再)起動します。

$ sudo systemctl restart httpd

以上で完了です。

STAFF
o_wani
o_wani
スタッフ
大学卒業後、15年間WEB業界で働く。現在はマネジメントに従事していますが、ChatGPTの登場に触発され、このブログを再開。AIをパートナーに、自分で手を動かして実装する楽しさと喜びを再発見中。時代が変わりつつある中でも、陳腐化しない情報発信も目指しています。
記事URLをコピーしました