LoginSignup
5
5

More than 5 years have passed since last update.

WebサイトにSSLを導入する(CentOS7+nginx1.15+Let's Encrypt)

Last updated at Posted at 2018-08-08

概要

CentOS7+nginx1.15+Let's Encryptを利用し、WebサイトにSSLを導入する。

※Let's Encryptとは
SSLサーバー証明書(以下、証明書と記載)を無償で取得・更新できるサービス。

対象環境

OS:CentOS7.5
Web:nginx1.15

前提

  • SSL化したいドメインの名前解決ができること。
  • SSL化したいドメインでWebサーバにアクセスできること。
  • 80番&443番ポートが開放されており、外部からアクセスできること。
ポート開放
# firewall-cmd --zone=public --add-service=http --permanent
# firewall-cmd --zone=public --add-service=https --permanent
# firewall-cmd --reload

ポート開放確認
# firewall-cmd --list-all

リストの中に「http」「https」があれば、ポート開放されている。
services: ssh http https

手順

Let's Encryptをインストールする。

任意のディレクトリに移動。
# cd /usr/local

Let's Encryptをcloneする。
# git clone https://github.com/certbot/certbot

cloneしたディレクトリに移動する。
# cd letsencrypt

インストール
# ./letsencrypt --help

証明書を発行する。

nginxを停止する。
# systemctl stop nginx

証明書を発行する。
ドメイン名やメールアドレスの入力、規約の同意を求められるので入力する。
※スタンドアローンモードで証明証を発行する。
# ./certbot-auto certonly --standalone -t

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel): xxxxxxxxxxxxxxxx.com

~~

Obtaining a new certificate
Performing the following challenges:
http-01 challenge for xxxxxxxxxxxxxxxx.com
Waiting for verification...
Cleaning up challenges

以下のように「Congratulations!」と表示されれば証明書の発行は完了。
IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:

~~

nginxを設定する。

confファイルに設定を追加する。

/etc/nginx/conf.d/default.conf
server {
  ~~
  listen 443 ssl;
  ssl_certificate     /etc/letsencrypt/live/xxxxxxxxxxxxxxxx.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/xxxxxxxxxxxxxxxx.com/privkey.pem;
  ~~
}

動作確認

nginxを起動する。

# systemctl start nginx

ブラウザで表示を確認する。

SSL導入前
image.png

SSL導入後
image.png

証明書の自動更新

cronで自動更新の設定

Let's Encryptで取得した証明書は有効期限が90日のため、定期的に更新処理を実行する必要があります。
※cron設定の手順は割愛します。

# 毎月1日の1時に更新処理を実行する。
0 1 1 * * root systemctl stop nginx && cd /usr/local/certbot/ && ./certbot-auto renew && systemctl start nginx

まとめ

検索エンジンやら外部API連携やらでSSL(HTTPS)が必須になってきているので、このあたりの操作には慣れておきたいですね。

参考

Let's Encrypt 総合ポータル

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