環境構築からWEBアプリ開発・スマホアプリ開発まで。ときには動画制作やゲームも。

supilog
すぴろぐ

Let’s EncryptでSSL証明書発行(CentOS7/apache)

みんな大好き「Let’s Encrypt」を利用してSSL設定をしていきます。無料です。以前と違って、どのようなサイトを作る場合でも暗号化は必須なので、割とお世話になると思います。

前提

お持ちのドメインのDNSレコードにて、Aレコードが設定してあるものとします。(このアドレスにアクセスしたら、このIPアドレスに遷移するという設定です)事前に設定をしておいてください。

apacheの事前設定

まずSSLでない状態でアクセス可能な状態を作ります。

yum -y install httpd

apacheをインストールしていない場合は、インストールしましょう。

yum -y install mod_ssl

SSLに必要なため、mod_sslをインストールします。mod_sslをインストールすると、apacheの設定ファイル(ssl.conf)も作成されます。

/etc/httpd/conf.d/ssl.conf

以下のように設定します。今回は、「SSLProtocol」「SSLCipherSuite」を少しだけ修正したのと、VirtualHostのデフォルトは使わない設定にします。

Listen 443 https
SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog
SSLSessionCache         shmcb:/run/httpd/sslcache(512000)
SSLSessionCacheTimeout  300
SSLRandomSeed startup file:/dev/urandom  256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin
SSLProtocol -all TLSv1.2
SSLCipherSuite !SSLv3:!DSS:AESGCM+ECDHE:AESGCM+EDH:EDH+AES256+SHA256
SSLHonorCipherOrder on
SSLCompression off

/etc/httpd/conf.d/yourdomain.example.com.conf

新規ファイルを追加して設定を記述していきます。「yourdomain.example.com」の部分は、お持ちのドメインにしておくと、わかりやすいと思います。

DocumentRootLogディレクトリは、もちろんお好みで変更可能です。バーチャルホストが複数個あることを想定して、勝手に以下の配置にしました。

<VirtualHost *:80>
  ServerName yourdomain.example.com
  DocumentRoot /usr/local/apache/vhosts/yourdomain.example.com/htdocs
  ErrorLog /usr/local/apache/vhosts/yourdomain.example.com/logs/error_log
  CustomLog /usr/local/apache/vhosts/yourdomain.example.com/logs/access_log combined
</VirtualHost>

<Directory "/usr/local/apache/vhosts/yourdomain.example.com/htdocs">
  Require all granted
</Directory>

事前の記述内容はこれだけ。記載したドキュメントルートとログディレクトリは作成しておきます。

# ディレクトリ作成
mkdir -p /usr/local/apache/vhosts/yourdomain.example.com/htdocs
mkdir -p /usr/local/apache/vhosts/yourdomain.example.com/logs

# ドキュメントルートに確認用のHTMLを配置
echo 'Hello World !!' > /usr/local/apache/vhosts/yourdomain.example.com/htdocs/index.html

ここまで出来たら、apacheを再起動します。

systemctl restart httpd

ここで、ブラウザからアクセスを確認すると、「Hello World !!」と表示されると思います。もちろんまだこの状態では暗号化されていません。

http://yourdomain.example.com/

ここで表示がされない場合には、設定が足りない可能性があるので、調べてみましょう。

Let’s encryptの出番です

いよいよ証明書の作業に入ります。

certbotインストール

yum -y install certbot

証明書の取得

certbot certonly --webroot -w ドキュメントルート -d ドメイン名

「ドキュメントルート」と「ドメイン名」は書き換えて実行してください。今回の例ですと、以下のような感じです。

certbot certonly --webroot -w /usr/local/apache/vhosts/yourdomain.example.com/htdocs -d yourdomain.example.com
【初回のみ】Emailや規約の同意

コマンドを実行すると、初回のみEmailアドレスの入力や規約の同意が求められます。確認していきます。

↓初回はEmailアドレスを聞かれます。ご自身のメールアドレスを入力しましょう。証明書の期限が近づいた時に、連絡をもらえたりします。

Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): 

↓サービス規約読んで、同意してねと言われます。「Y」で次に行きましょう。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: 

↓こちらは、ニュースやキャンペーン情報を送りたいんだけど、メールアドレスを共有してもいいか。と聞かれているので、お好みで答えます。

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

↓Congratulationsの表示が出たら、無事に取得完了です。

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

取得できた証明書は、以下に配置されています。確認してみてください。

ls -la /etc/letsencrypt/live/yourdomain.example.com/
--------------------
-rw-r--r-- 1 root root  692 Feb 22 06:46 README
lrwxrwxrwx 1 root root   46 Feb 22 06:46 cert.pem -> ../../archive/yourdomain.example.com/cert1.pem
lrwxrwxrwx 1 root root   47 Feb 22 06:46 chain.pem -> ../../archive/yourdomain.example.com/chain1.pem
lrwxrwxrwx 1 root root   51 Feb 22 06:46 fullchain.pem -> ../../archive/yourdomain.example.com/fullchain1.pem
lrwxrwxrwx 1 root root   49 Feb 22 06:46 privkey.pem -> ../../archive/yourdomain.example.com/privkey1.pem
--------------------

apacheのSSL設定

取得まで完了したので、HTTPSでアクセスができるようにapacheの設定をします。

/etc/httpd/conf.d/yourdomain.example.com.conf 

<VirtualHost *:80>
  ServerName yourdomain.example.com
  DocumentRoot /usr/local/apache/vhosts/yourdomain.example.com/htdocs
  RewriteEngine on
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:443>
  ServerName yourdomain.example.com
  DocumentRoot /usr/local/apache/vhosts/yourdomain.example.com/htdocs
  ErrorLog /usr/local/apache/vhosts/yourdomain.example.com/logs/error_log
  CustomLog /usr/local/apache/vhosts/yourdomain.example.com/logs/access_log combined
  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/yourdomain.example.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.example.com/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/yourdomain.example.com/chain.pem
</VirtualHost>

<Directory "/usr/local/apache/vhosts/yourdomain.example.com/htdocs">
  Require all granted
</Directory>
設定の説明

HTTP(80)のアクセスについては、受け取ったあと、HTTPS(443)にリダイレクトしています。これにより、httpでアクセスしてもhttpsでアクセスしても、httpsになります。

「SSLCertificateFile」「SSLCertificateKeyFile」「SSLCertificateChainFile」には、さきほど取得したSSL証明書のパスが入ります。

systemctl restart httpd

設定が完了したので、再起動します。

ブラウザ確認

ブラウザより、お持ちのドメインにてアクセスしてみてください。設定が間違っていなければ、SSLアクセスが出来ているはずです。(ブラウザのアドレス欄に、鍵マークが付きます)

さいごに

SSL証明書発行の手順といいつつapacheの設定の話が多かったですね。初心者の方用にそのうちnginxでの手順も記載するかもしれません。

また、注意が1点必要で、「Let’s Encrypt」のSSL証明書は無料で便利ですが、証明書の期限が90日となっています。90日以内に更新作業をしないといけません。ご安心ください。自動更新ができますので、その手順も見てもらえたらと思います。