LoginSignup
7
10

More than 3 years have passed since last update.

今更ながらCentOS7にLet's Encryptの設定をしてみた

Last updated at Posted at 2020-02-07

Let's Encryptで認証されたWebサーバを作ってみた

 まずはDocumentRootとServerNameを設定

インストールしたばかりのサーバの設定をする。
当然この前にFirewalldの設定をしておいて80番や443番を開けておく。

touch /etc/httpd/conf.d/hoge.example.com.conf
vi /etc/httpd/conf.d/hoge.example.com.conf
hoge.example.com.conf
<VirtualHost *:80>
  DocumentRoot /var/www/hoge.example.com
  ServerName hoge.example.com
</VirtualHost>

SSLも設定

<VirtualHost _default_:443>
DocumentRoot "/var/www/hoge.example.com"
ServerName hoge.example.com:443
・・・・

httpdのプロセス再起動

systemctl restart httpd.service

Let'sEncryptをインストール

yum install epel-release
yum install certbot python-certbot-apache

接続して設定する

certbot certonly --webroot -w /var/www/hoge.example.com/ -d hoge.example.com

途中で聞かれること

Enter email address          ← メールアドレスを入力
Prease read the Terms  ...      ← 同意してAgree
Would you be willing ...        ← メールで連絡をくれるらしい 任意

こんな感じで進んでいくと、

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/hoge.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/hoge.example.com/privkey.pem
   Your cert will expire on 2020-05-07. 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"
 - 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

と表示されるので、本当にできたかどうかを確認してみる。

注)この際、Let's Encryptのサイトからアクセスがあるのでサーバは設定しているURL(この場合hoge.example.com)がLet’s Encryptのサイトから80番や443番でアクセスできるように設定しておかなければいけない。

# ls -l /etc/letsencrypt/live/hoge.example.com/
合計 4
-rw-r--r-- 1 root root 692  2月  7 11:33 README
lrwxrwxrwx 1 root root  41  2月  7 11:33 cert.pem -> ../../archive/hoge.example.com/cert1.pem
lrwxrwxrwx 1 root root  42  2月  7 11:33 chain.pem -> ../../archive/hoge.example.com/chain1.pem
lrwxrwxrwx 1 root root  46  2月  7 11:33 fullchain.pem -> ../../archive/hoge.example.com/fullchain1.pem
lrwxrwxrwx 1 root root  44  2月  7 11:33 privkey.pem -> ../../archive/hoge.example.com/privkey1.pem

ssl.confに登録

/etc/httpd/conf.d/ssl.conf
...
SSLCertificateFile /etc/letsencrypt/live/hoge.example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hoge.example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/hoge.example.com/chain.pem
...

httpdのプロセス再起動

systemctl restart httpd.service

スマホなどを使って、httpsでアクセスすると、信用おけるサイトとなっていた。

証明書の自動更新

Let's Encryptは3ヶ月で証明書が切れる。
切れないように、cronで更新をする。
rootさんでcrontabを編集。

crontab -e
00 04 01 * * certbot renew && systemctl restart httpd

これで毎日更新される。一応httpdもリスタートするようにしてある。

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