1. Amimoto サポート
  2. Amimoto AMI
  3. Amimoto AMI によくいただくご質問

Let's Encrypt は使えますか?

セルフホスティングでご利用いただける、AmazonLinux 2 の最新バージョンの Amimoto では、以下の手順で Let's Encrypt のインストールが可能です。

このドキュメントでは、example.com のドメインに対して Let's Encrypt を使った SSL デジタル証明書をインストール・設定を行う手順について解説いたします。
 

前提

  • ドメインが取得済みであること
  • DNS の各種ゾーン情報・レコードが設定されていること
  • http://example.com 等のドメインでサーバへのアクセスが可能なこと
  • WordPress のインストールが完了していること

作業内容

  1. SSL デジタル証明書のインストール
  2. SSL の設定
  3. 証明書を自動更新する設定 (オプション)

1. ドメインの SSL デジタル証明書をインストールする

サーバへログインします

$ ssh -i ssh-key.pem ec2-user@example.com

root ユーザとして作業します

$ sudo su -

example.com へ 証明書をインストールする場合

コマンド

# letsencrypt certonly -t -d example.com \
 -a webroot --webroot-path=/var/www/vhosts/example.com/ \
 --rsa-key-size 2048
 

注意: example.com の箇所は、ご利用のドメイン名と置換えて設定を行ってください。

Let's Encrypt からのメールを受取るために、メールアドレスを設定します:

Enter email address (used for urgent notices and lost key recovery) (Enter 'c'
to cancel): info+letsencrypt@example.com ※

Let's Encrypt の利用既約に同意します:

この画面で[a] キーを入力し [Enter]/[Return] を押してください。

Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree
in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: A

Let's Encrypt がサーバへインストールされました。

証明書及び証明書チェーンは以下のディレクトリに保管されています:

/etc/letsencrypt/live/example.com/fullchain.pem

また秘密鍵は以下のディレクトリに保管されています

/etc/letsencrypt/live/example.com/privkey.pem

2. SSL の設定

Let's Encript と SSL デジタル証明書のインストールまで完了しました。次は、証明書を利用するようサーバの設定ファイル (/etc/nginx/conf.d/example.com-ssl.conf) を変更します。既存の example.com.conf を複製しexample.com-ssl.conf とファイル名を変えて編集をします。

注意: example.com の箇所は、ご利用のドメイン名と置換えて設定を行ってください。

example.com 用の設定ファイルをコピーし編集します

コマンド

# cd /etc/nginx/conf.d
# cp example.com.conf example.com-ssl.conf
# vi example.com-ssl.conf
  

[i] キーを押してから編集を行ってください。

設定内容

server {
    listen      443 ssl http2;
    server_name example.com;
    root        /var/www/vhosts/example.com;
    index       index.html index.htm;
    charset     utf-8;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers AESGCM:HIGH:!aNULL:!MD5;
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 5m;
   
    access_log  /var/log/nginx/example.com-ssl.access.log  main;
    error_log   /var/log/nginx/example.com-ssl.error.log;

    include     /etc/nginx/drop;

    add_header X-Cache-Status $upstream_cache_status;
    expires $expires;

    set $mobile "";
    #include /etc/nginx/mobile-detect;

    include     /etc/nginx/wp-front;

    location ~* /(phpmyadmin|myadmin|pma) { access_log off; log_not_found off; return 404; }

    #
    # redirect server error pages to the static page /50x.html
    #
    error_page  502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

注意: example.com の箇所は、ご利用のドメイン名と置換えて設定を行ってください。

編集が終ったら [Esc] を押し、続けて [Shift] キーを押しながら [z] を2回押して保存してください。

設定反映のため nginx を restart します。

コマンド

# service monit stop && service nginx restart
# service monit start

加えて、すでに WordPress を独自ドメインで運用されていて HTTPS 接続を強制する場合は、/etc/nginx/conf.d/example.com.conf の内容に return 301 https://example.com$request_uri; の1行を追加してください。

コマンド

# vi example.com.conf

[i] キーを押してから編集を行ってください。

設定内容

server {
    listen      80;
    server_name example.com;
    root        /var/www/vhosts/example.com;
    index       index.html index.htm;
    charset     utf-8;

    access_log  /var/log/nginx/example.com.access.log  main;
    error_log   /var/log/nginx/example.com.error.log;

    include     /etc/nginx/drop;

    add_header X-Cache-Status $upstream_cache_status;
    expires $expires;

    set $mobile "";
    #include /etc/nginx/mobile-detect;

    location / {
        return 301 https://example.com$request_uri;
    }

    #
    # redirect server error pages to the static page /50x.html
    #
    error_page  502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

編集が終ったら [Esc] を押してください。その後、[Shift] キーを押しながら [z] を2回押して保存してください。
 その後、設定反映のため nginx を restart します。

# service monit stop && service nginx restart
# service monit start

Let's Encrypt と SSL デジタル証明書の設定、サーバ設定の変更は以上です。

3. 証明書を自動更新する設定 (オプション)

Let's Encrypt は発行後90日で証明書の有効期限が切れるため、自動で証明書の更新を行うように設定をするとよいでしょう。

※ 毎週月曜日 AM1:00に letsencrypt renew を実行する場合

コマンド

以下の2行をコピーして、[i] キーを押してから crontab -e で開いた画面へペーストしてください。

# crontab -e

編集が終ったら [Esc] を押してください。その後、[Shift] キーを押しながら [z] を2回押して保存してください。

# Renewing Lets Encrypt certificate
0 1 * * 1 /opt/letsencrypt/bin/letsencrypt renew && /sbin/service nginx restart > /dev/null 2>&1


証明書の自動更新設定は以上です。