OshiHa.com

ConoHaでAlpine Linux 3.8

Nginx の SSL設定(https接続)

2018/09/01  2018/09/09

◆◇◆ 【衝撃価格】VPS512MBプラン!月額630円/1時間1円【ConoHa】 ◆◇◆

Let’s Encrypt で無料の SSL/TLSサーバ証明書を発行する。
無料であるが Let's Encrypt の中間証明書は、大手認証局 (CA) のルート証明書によってクロス署名されているので、多くの主要ブラウザで信頼済みとして扱われる。

なお、証明書の有効期限は 90日なので、90日以内に更新作業を再度実施する必要がありる。

事前に独自ドメインを取得・登録設定しておくこと。

1. certbot のインストール

alpine:~$ sudo apk add certbot

2. バージョンの確認

alpine:~$ certbot --version
certbot 0.26.1

3. SSL証明書を取得

事前にリポジトリの追加で、edge(testing)リポジトリ以外を有効にしておくこと。
リポジトリの追加方法は こちら

alpine:~$ sudo rc-service nginx stop
alpine:~$ sudo certbot certonly --standalone -d oshiha.com -d www.oshiha.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):*******@gmail.com	← メールアドレスを入力

-------------------------------------------------------------------------------
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		← [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: N			← [N] を入力してメールアドレスを共有しない

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

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/oshiha.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/oshiha.com/privkey.pem
   Your cert will expire on 2018-11-30. 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"
 - 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.
 - 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

証明書と秘密鍵はそれぞれ以下の場所に配置される。
証明書: /etc/letsencrypt/live/[Domain name]/fullchain.pem
秘密鍵: /etc/letsencrypt/live/[Domain name]/privkey.pem

4. Nginx の設定

HTTPS の Port は 443 なので、http(80)を https(443)にリダイレクトさせる。
www 有りアドレスも www 無しアドレスにリダイレクトさせる。
そして、serverディレクティブに証明書と秘密鍵のパスを指定する。
併せて webサイトで使用する ssi を有効化する。

alpine:~$ sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.org
alpine:~$ sudo vi /etc/nginx/conf.d/default.conf
server {
    listen 80;
    server_name oshiha.com;
    return 301 https://oshiha.com$request_uri;
}

server {
    listen 80;
    listen 443;
    server_name www.oshiha.com;
    return 301 https://oshiha.com$request_uri;
}

server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    server_name oshiha.com;

    ssl_certificate     /etc/letsencrypt/live/oshiha.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/oshiha.com/privkey.pem;

    root /var/www/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;

        ssi  on;
        ssi_last_modified on;
    }
}

5. Nginx の再起動

alpine:~$ sudo rc-service nginx restart

6. SSL証明書の更新

証明書の更新

alpine:~$ sudo certbot renew

証明書の更新は、証明書の有効期限が30日を切らないと更新できないので、「次の証明書はまだ更新される予定ではありません。」とコメントがかえってる。

Saving debug log to /var/log/letsencrypt/letsencrypt.log

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/oshiha.com.conf
-------------------------------------------------------------------------------
Cert not yet due for renewal

-------------------------------------------------------------------------------

The following certs are not due for renewal yet:
  /etc/letsencrypt/live/oshiha.com/fullchain.pem (skipped)
No renewals were attempted.
-------------------------------------------------------------------------------

証明書が更新できるかの確認

Nginx起動中だと更新がうまくいかないので、nginxを停止させてからおこなう。

alpine:~$ sudo rc-service nginx stop
alpine:~$ sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/oshiha.com.conf
-------------------------------------------------------------------------------
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator standalone, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for oshiha.com
http-01 challenge for www.oshiha.com
Waiting for verification...
Cleaning up challenges

-------------------------------------------------------------------------------
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/oshiha.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/oshiha.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.
alpine:~$ sudo rc-service nginx start

強制的に更新

今回は強制的に更新してみる。ただし、1週間に20回までとなっているので更新回数に注意。

alpine:~$ sudo rc-service nginx stop
alpine:~$ sudo certbot renew --force-renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/oshiha.com.conf
-------------------------------------------------------------------------------
Plugins selected: Authenticator standalone, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for oshiha.com
http-01 challenge for www.oshiha.com
Waiting for verification...
Cleaning up challenges

-------------------------------------------------------------------------------
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/oshiha.com/fullchain.pem
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/oshiha.com/fullchain.pem (success)
-------------------------------------------------------------------------------
alpine:~$ sudo rc-service nginx start

7. cron で証明書の自動更新

Let’s Encryptの証明書の有効期限は90日なので、更新作業をおこなう。
ただし、有効期限が30日未満のときしか更新されない。
そのため、cron で毎月1日の午前3時に SSL証明書を自動更新する。

alpine:~$ sudo crontab -e
# 最終行に追加
0	3	1	*	*	rc-service nginx stop; certbot renew; rc-service nginx start

◆◇◆ 【衝撃価格】VPS512MBプラン!月額630円/1時間1円【ConoHa】 ◆◇◆

ConoHa VPSの攻略
Alpine Linux 3.8

TOP