LoginSignup
12
3

More than 1 year has passed since last update.

よくあるEC2サーバにLet's EncryptでSSLを使う方法 2021

Last updated at Posted at 2021-12-20

はじめに

株式会社ピー・アール・オー Advent Calendar 2021の 20日目です。

弊社のアプリブランド PRO.APPが12月で12周年を迎えました♪
イベントも開催中なので、よかったらご覧ください♪
本キャンペーンは終了しました。
PRO.APP誕生祭 プレゼントキャンペーン (2021/12/16〜2022/1/11)

EC2サーバにLet's EncryptでSSLを使いたい

テストでSSLの環境がほしい!となりました。
でもガッツリ使いたいわけじゃなく、ちょっとテストしたいだけ。
だからサクッと環境準備したい。

...のになぜかコピペのコマンドが、そのままだと動かないあるあるな問題。
...いっぱい情報もあるのにー。

日々情報は変わってしまいますが、2021/12に試した手順として、
自分の備忘録として、載せたいと思います。

前提条件

・EC2サーバ
http://example.comではページが見れていること
https://example.comにすると、見れないこと

※わかりやすく書くために以下としますが、実際とは違うドメイン名とメールアドレスです。
・webroot:/var/www/html
・ドメイン:example.com
・メールアドレス:hoge@example.com

やりたいこと

https://example.comでページを見る

1.Certbotを入れる

まずはLet's Encryptを簡単に入れられるということで、Certbotを入れます。

yumでインストールするだけなのに、さっそくのエラー。悲しい。

NG
[root]# yum install certbot
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
No package certbot available.
Error: Nothing to do

色々変わったみたいなので、certbot入れる前に、必要なのを入れてから
certbotを入れると、入りました。

OK
[root]# sudo yum -y install yum-utils
[root]# sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root]# sudo yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
[root]# sudo yum install certbot

次、インストールしたファイルの設定を変えたり、binに移動したりします。
バックアップも取っておきます。

OK
--- 最初の状態
[root ~]# cd /usr/bin/
-rw-r--r-- 1 root root         146 Dec  8 09:40 certbot-auto

--- 権限変更
[root bin]# sudo chmod 700 /usr/bin/certbot-auto

--- バックアップとって、/usr/local/binに移動
[root bin]# cp /usr/bin/certbot-auto /usr/bin/certbot-auto.bak
[root bin]# vi /usr/bin/certbot-auto
[root bin]# sudo mv /usr/bin/certbot-auto /usr/local/bin

--- 移動してファイル確認
[root@ip-172-30-0-123 bin]# cd /usr/local/bin
[root@ip-172-30-0-123 bin]# ls -la
total 4
-rwx------ 1 root root 146 Dec  8 09:40 certbot-auto

2.証明書をインストールする

ググって出てきたコマンドをコピペしたら失敗。

NG
[root html]# sudo certbot certonly --webroot -w /var/www/html -d example.com --email hoge@example.com --preferred-chain "ISRG Root X1" --debug
usage: 
  certbot [SUBCOMMAND] [options] [-d DOMAIN] [-d DOMAIN] ...

Certbot can obtain and install HTTPS/TLS/SSL certificates.  By default,
it will attempt to use a webserver both for obtaining and installing the
certificate. 
certbot: error: unrecognized arguments: --preferred-chain ISRG Root X1

2021/09月でDST Root CA X3が廃止、ISRG Root X1を代わりに使うって情報も見たけど、エラー。
ISRG Root X1は使えるんじゃないの?と思いつつ、試しに--preferred-chainを外してみたら、できた!

OK
[root]# sudo certbot certonly --webroot -w /var/www/html -d example.com --email hoge@example.com --debug
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: a

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing 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
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example.com
Using the webroot path /var/www/html for all unmatched domains.
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/example.com/privkey.pem
   Your cert will expire on 2022-03-08. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - 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

↑で指定された場所にファイルができたことを確認

OK
[root html]# cd /etc/letsencrypt/live/example.com/
[root example.com]# ls -la
total 4
lrwxrwxrwx 1 root root  46 Dec  8 10:14 cert.pem -> ../../archive/example.com/cert1.pem
lrwxrwxrwx 1 root root  47 Dec  8 10:14 chain.pem -> ../../archive/example.com/chain1.pem
lrwxrwxrwx 1 root root  51 Dec  8 10:14 fullchain.pem -> ../../archive/example.com/fullchain1.pem
lrwxrwxrwx 1 root root  49 Dec  8 10:14 privkey.pem -> ../../archive/example.com/privkey1.pem
-rw-r--r-- 1 root root 692 Dec  8 10:14 README

3.ssl.confファイル修正

OK
[root ~]# cd /etc/httpd/conf.d
[root conf.d]# vi ssl.conf

以下の3箇所それぞれ探して、コメント外して修正。

ssl.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem

SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

SSLCertificateChainFile /etc/letsencrypt/live/example.com/fullchain.pem

ssl.confファイル直したらsyntaxチェックする

OK
[root conf.d]# service httpd configtest
Syntax OK

4.apache再起動

apache再起動

OK
[root conf.d]# service httpd restart
Redirecting to /bin/systemctl restart httpd.service
[root conf.d]#

5.httpsで見れるか確認

見れました!∩(´∀`❤)∩

 https://example.com

12
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
3