Column -コラム記事-
Nginxに Let’s Encrypt で無料SSLを導入してみた
手っ取り早くSSLがほしい!ってことありませんか?
そんなときはLet’s Encrypt。5分でSSL実装できます。
Let’s Encryptとは
Let’s Encrypt は、認証局(CA)として「SSL/TLSサーバ証明書」を無料で発行するとともに、証明書の発行・インストール・更新のプロセスを自動化することにより、TLS や HTTPS(TLSプロトコルによって提供されるセキュアな接続の上でのHTTP通信)を普及させることを目的としているプロジェクトです。
2016年4月12日 に正式サービスが開始されました。
*ただし、証明書の有効期間は3ヶ月。3ヶ月毎に更新作業を行う必要がある*
それでは早速使ってみましょう。
certbot のインストール
$ cd /usr/local $ git clone https://github.com/certbot/certbot $ cd certbot $ ./certbot-auto --help
上記を順番に実行
Usage: certbot-auto [OPTIONS] A self-updating wrapper script for the Certbot ACME client. When run, updates to both this script and certbot will be downloaded and installed. After ensuring you have the latest versions installed, certbot will be invoked with all arguments you have provided. Help for certbot itself cannot be provided until it is installed. --debug attempt experimental installation -h, --help print this help -n, --non-interactive, --noninteractive run without asking for user input --no-self-upgrade do not download updates --os-packages-only install OS dependencies and exit -v, --verbose provide more output
ヘルプが表示されればインストールOK
証明書の取得
取得したいドメイン
`app-sample.jp`
$ ./certbot-auto certonly -t
初回実行時には不足パッケージのインストールを促される。
yesと答えてインストールする。
不足パッケージのインストールが終わると、次の様にメールアドレスを聞いてくるので入力する。
Creating virtual environment... Installing Python packages... Installation succeeded. Enter email address (used for urgent notices and lost key recovery) (Enter 'c' to cancel): xxxx@hogehoge.com
メールアドレスの次は、ライセンスに Agreeするかを聞いてくる。
------------------------------------------------------------------------------- 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
上の様にAを入力してAgreeすると続いて次の様に認証方法を訪ねてくる。
How would you like to authenticate with the ACME CA? ------------------------------------------------------------------------------- 1: Apache Web Server plugin - Beta (apache) 2: Nginx Web Server plugin - Alpha (nginx) 3: Spin up a temporary webserver (standalone) 4: Place files in webroot directory (webroot) ------------------------------------------------------------------------------- Select the appropriate number [1-4] then [enter] (press 'c' to cancel):
使用しているwebServerを指定する。今回はnginxなので、2
Select the appropriate number [1-4] then [enter] (press 'c' to cancel):2 Plugins selected: Authenticator nginx, Installer None Please enter in your domain name(s) (comma and/or space separated) (Enter 'c' to cancel):
ドメインを入力します。
`app-sample.jp` Plugins selected: Authenticator nginx, Installer None Please enter in your domain name(s) (comma and/or space separated) (Enter 'c' to cancel): app-sample.jp
Obtaining a new certificate Performing the following challenges: http-01 challenge for app-sample.jp Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/app-sample.jp/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/app-sample.jp/privkey.pem Your cert will expire on 2018-06-06. To obtain a new or tweaked version of this certificate in the future, simply run certbot-auto again. To non-interactively renew *all* of your certificates, run "certbot-auto 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
これで証明書が発行された。
cert-botにより取得した証明書一式は `/etc/letsencrypt/` に保存されている。
設定で使用するディレクトリは、`/etc/letsencrypt/live`にあるドメインファイル以下だ。
Nginxの設定
あとはNginxに取得した証明書を設定するだけ。
server { ... listen 443 ssl; ssl_certificate /etc/letsencrypt/live/app-sample.jp/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/app-sample.jp/privkey.pem; ... }
confファイルに記述したら`reload`でアクセスできるようになります。
簡単ですね。
証明書の更新方法
更新方法も簡単
./certbot-auto renew
これだけで更新されます。
3ヶ月ごとに更新する必要があるので、クーロンか、なにかを組む必要がありそうですね。