Cent OSにLet’s Encryptを設定する手順

本ページはプロモーションが含まれています。

Let’s Encryptを利用すると独自ドメインを使って無料でSSLに対応することができます。

Cent OS 7.4とApacheを使ってLet’s Encryptを導入する手順について簡単に解説してみようと思いますので、よろしければ参考にしてみてください。

まだ独自ドメインを取得していないという方もドメインの取得は簡単ですので、ぜひチャレンジしてみてください。

※サーバー付属のサブドメイン(.hmk-temp.comなど)ではLet’s Encryptは使えません。

Let’s Encryptを使わずSSLの自己証明書で試してみたいという場合はこちらを参考にしてみてください。

Let’s Encryptの準備と設定

mod_ssl

Apacheはインストール済みであるとして、SSLに必要となる「mod_ssl」をインストールするところから解説します。

# yum install mod_ssl
# systemctl restart httpd

※Apacheのインストールがまだという場合はApacheのインストールの記事も参考にしてみてください。

Certbot

SSLの設定に必要となる各種証明書の取得や設定を行うために「Certbot」というツールをインストールします。

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

証明書の取得

インストールしたCertbotを使って証明書を取得します。

※以下はexample.comというドメイン名と/var/www/htmlというディレクトリに設定する例です。

# certbot certonly --webroot -w /var/www/html -d example.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): yourname@example.com
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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: YもしくはN
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example.com
Using the webroot path /var/www/virtual/example.com for all unmatched domains.
Waiting for verification...
Cleaning up challenges
Resetting dropped connection: acme-v02.api.letsencrypt.org

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 20**-**-**. 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

サブドメインの証明書を取得したい場合は-dで証明書を取得したいサブドメインを指定します。

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

Apacheの設定

Apacheの設定は次のような感じで記述します。

<VirtualHost *:443>
  DocumentRoot /var/www/html
  ServerName example.com

  SSLEngine on
  SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
</VirtualHost>

設定できたらApacheを再起動してブラウザから動作を確認してみてください。

# systemctl restart httpd

証明書の更新

Let’s Encryptで取得できる証明書の有効期限は3か月となっています。有効期限の延長もcertbotコマンドでできますので忘れずに実行してください。

# certbot renew
# systemctl restart httpd

※CRONで自動化するという方法もあります。

SSLの自己証明書

Let’s Encryptを使うと無料で証明書を取得することができますが、独自ドメインではなく(サーバー付属の)サブドメインを使っていたり、なんらかの理由で証明書を取得できないという場合は次のような手順で自己証明書を導入できます。

※自己証明書の場合、SSLの暗号化通信は使えますが”サーバーの証明書”としての意味はなくなります。実際に使用される場合は、その点について理解した上で試してください。

自己証明書の作り方

次のような感じでコマンドを実行して秘密鍵server.keyと署名済み証明書server.pemを作成します。

※テスト用のダミー証明書として作成する場合は国名などの項目は空欄でも問題ありません。

# cd /etc/pki/tls/certs/
# make server.key
umask 77 ; \
/usr/bin/openssl genrsa -aes128 2048 > server.key
Generating RSA private key, 2048 bit long modulus
..........................................+++
.........................+++
e is 65537 (0x10001)
Enter pass phrase: ********
Verifying - Enter pass phrase:
# openssl rsa -in server.key -out server.key
Enter pass phrase for server.key: ********
writing RSA key
# make server.csr
umask 77 ; \
/usr/bin/openssl req -utf8 -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]: 国名
State or Province Name (full name) []: 都道府県名
Locality Name (eg, city) [Default City]: 市町村名
Organization Name (eg, company) [Default Company Ltd]: 組織名
Organizational Unit Name (eg, section) []: 部門名
Common Name (eg, your name or your server's hostname) []: サーバーのホスト名
Email Address []: メールアドレス

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: 空欄
An optional company name []: 空欄
# openssl x509 -in server.csr -out server.pem -req -signkey server.key -days 90(証明書の有効日数)
Signature ok
subject=/C=XX/L=Default City/O=Default Company Ltd
Getting Private key

Apacheの設定

Apacheの設定は次のような感じで記述します。

<VirtualHost *:443>
  DocumentRoot /var/www/html
  ServerName example.com

  SSLEngine on
  SSLCertificateFile /etc/pki/tls/certs/server.pem
  SSLCertificateKeyFile /etc/pki/tls/certs/server.key
</VirtualHost>

VPS活用ガイド‐月額780円からのVPSというサイトで公開していた記事です。誤字脱字などの修正を除いて当時のまま移転していますのでやや古い内容も含まれています。ご理解の上、参照して頂ければと思います。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です