セキュリティ

無料SSL証明書Let's Encrypt導入まとめ!

SSL対応は必須!まだ対応してなければ、まず無料証明書で即対応。

あなたのホームページはSSL対応できてますか?Let’s Encryptは無料でいつでも実装できるので、もし出来ていないようなら今すぐ導入しましょう。

※Let's Encrypt証明書の有効期限は3か月です。自動更新の設定を済ませれば永久に使えます。しかも、サブドメインを全て網羅するワイルドカードの設定もできます!ただし、ワイルドカードでは証明書の自動更新が出来ず、手動で(3か月おきに!)しなくてはなりません。ここではワイルドカードの設定いたしません。後日機会があれば記事にまとめます。

まずWEBサーバにSSL対応させよう。

まず、WEBサーバ(apache)がSSLに対応している確認します。

# httpd -M

Apacheにインストール済のモジュールの一覧が沢山出てきます。その中に

ssl_module (shared)

が含まれていれば、次の項目へスキップして下さい。上記のモジュールが入っていなければmod_sslをインストール出来るか確認します。

$ yum search mod_ssl
読み込んだプラグイン:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.cat.net
* epel: www.ftp.ne.jp
* extras: mirrors.cat.net
* remi-php70: ftp.riken.jp
* remi-safe: ftp.riken.jp
* updates: mirrors.cat.net
============================= N/S matched: mod_ssl =============================
mod_ssl.x86_64 : SSL/TLS module for the Apache HTTP Server

Name and summary matches only, use "search all" for everything.

続いてapacheにモジュールをインストールします

$ sudo yum -y install mod_ssl

特にエラーがでなければインストール完了です。下記のコマンドで

$ yum list installed | grep mod_ssl
mod_ssl.x86_64 1:2.4.6-88.el7.centos @base

と出れば、インストールできた事を確認できます。

ファイヤウォールの設定

通常http通信はポート80番を使用しますが、SSLのhttps通信は443番を使用します。まず、443番ポートが通信可能な設定になっているか確認します。

$ sudo firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources:
  services: dhcpv6-client http https ssh

このようにserviceの項目にhttpsが含まれていれば大丈夫です。次のステップへスキップして下さい。httpsが含まれていなければこちらのコマンドを実行。

$ sudo firewall-cmd --add-port=443/tcp --zone=public --permanent
success

successと出たらOKです。改めて、上記のsudo firewall-cmd --list-allコマンドで確認してみましょう。

Let’s Encrypt証明書を導入する

$ sudo yum install certbot python2-certbot-apache

これでエラー出なければ次は、certbotコマンドで証明書を導入します。

$ sudo certbot --apache -d www.your-domein.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): info@your-domein.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
Starting new HTTPS connection (1): supporters.eff.org
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for www.your-domein.com
Cleaning up challenges
Unable to find a virtual host listening on port 80 which is currently needed for Certbot to prove to the CA that you control your domain. Please add a virtual host for port 80.

IMPORTANT NOTES:
- 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.

失敗しました。

バーチャルホストの設定を求められているので、バーチャルホストの設定を行います。

$ sudo vi /etc/httpd/conf.d/virtualhost.conf

と下記の内容のファイルを作成してください。※メールアドレスとドメインは修正して下さい。

NameVirtualHost *:80

<VirtualHost *:80>
ServerAdmin info@your-domain.com
DocumentRoot /var/www/html
ServerName www.your-domain.com
</VirtualHost>

改めて、チャレンジします。

$ sudo certbot --apache -d www.your-domein.com

これで成功しました。

SSL証明書の自動更新

この無料のSSL証明書(Let’s Encrypt)は、3ヶ月で有効期限を迎えてしまいます。3か月おきに証明書の更新するのは面倒なので自動で更新するようにします。まず、更新が問題なく更新できるかテストしてみます。

このような表示が出ればテストOKです。

$ sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/www.your-domain.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator apache, Installer apache
Starting new HTTPS connection (1): acme-staging-v02.api.letsencrypt.org
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for www.your-domain.com
Waiting for verification...
Cleaning up challenges
Resetting dropped connection: acme-staging-v02.api.letsencrypt.org

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed with reload of apache server; fullchain is
/etc/letsencrypt/live/www.your-domain.com/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/www.your-domain.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
- 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.

では、cronの設定を行い3か月毎に証明書の更新を実行するようにしましょう。

# sudo crontab -u root -e

毎月1日と15日のAm4:00に自動更新する設定例です。2回設定されているのは更新失敗した時に備えています。(ちなみに、証明書の期限が30日以上残っている場合は更新されません。)

00 04 01 * * certbot renew && systemctl restart httpd
00 04 15 * * certbot renew && systemctl restart httpd

これで自動更新の設定が終わりました。

 

SSL対応が終わった後の作業

SSL対応していなかった方は、これまでURLが http://~/ だったと思いますが、これからはURLが https://~/ に変わります。それによって様々な設定を新しいURLに変更する必要がある場合があります。

  • WordPressをサイトに利用している場合
    • WPの管理画面内にある「設定」>「一般」の中にある「ワードプレスアドレス」「サイトアドレス」を修正しましょう。
  • ページ内のmetaタグのog:image等にhttp~の記述が残っていないでしょうか?
    • 原則相対パスはNGなので記述をhttp://~/となってる場合は修正が必要です。
    • httpで外部ファイルを読み込んでいる個所は原則httpsに変えましょう。
    • ページ内でhttpで外部ファイルを読み込んでいる場合、訪問者のブラウザに暗号化されてません!といった警告が出るので、修正が必要です。
  • GoogleAnalyticsをご利用の場合は、変更すべき個所があります。
    • 「管理」>「ビュー」の中の「ビューの設定」の「ウェブサイトのURL」を修正
    • 「管理」>「プロパティ」の中の「プロパティ設定」の「デフォルトのURL」を修正
    • GoogleSearchConsoleの方はhttpsで新規登録が必要です。httpで既に登録済の場合は、サイトの所有者確認を自動で済ませてくれるようです。
    • その他のアクセス解析を使ってる場合も念のため確認しましょう。

 

 

 

 

 

 

-セキュリティ
-, ,

© 2026 WEBマスターの為のメモ