【Ubuntu】Let’s EncryptのSSL証明書をインストール

letsencrypt サーバー

Let’s Encryptとは、SSL証明書を無料で利用できる仕組みです。

VPSサーバーはもちろん、これまではSSL証明書取得に料金がかかっていた、エックスサーバー
や、ヘテムル、ロリポップ、サクラインターネットといった共用サーバーでもSSL証明書が無料で利用できるようになりました。

当ブログでも、Let’s Encryptを利用してSSL接続を行っています。
この記事では、Let’s EncryptをUbuntuサーバーに導入する手順を解説します。

Let’s Encrypt用のツールをaptでインストール

Ubuntuではaptで簡単にインストールできます。

$apt install letsencrypt

SSL/TLS サーバー証明書の取得

※途中で80番ポートを利用するため、nginxを停止しておく
$service nginx stop

※証明書を発行
$letsencrypt certonly --standalone -d example.com

証明書がインストールされたディレクトリ

SSL証明書は/etc/letsencrypt/live/example.com/以下にインストールされます。

Nginxの設定

NginxをSSLに対応させるには以下のように設定する必要があります。

server {
  ...
  listen 443 ssl;
  ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
  ...
}

また、httpアクセスを強制的にhttpsにリダイレクトするには以下の設定が必要です。
※wwwあり、なしどちらの場合でも強制的に、wwwなしにリダイレクト

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

移行先のサーバーでSSL証明書を取得する場合

新規でSSL証明書を取得する場合は問題ありませんが、すでにSSL証明書を取得・運用しているサーバーを移転する際、先程の方式ではドメインとサーバーが一致していないと失敗します。

そこで、certbot というパッケージをaptでインストールすれば、DNS認証を行えばドメインが利用できないサーバーからでもSSLを取得する事ができます。

#apt install software-properties-common
#add-apt-repository ppa:certbot/certbot
#apt update
#apt install python-certbot-nginx

certbot で証明書を取得します。

# certbot certonly --manual --preferred-challenges dns -d example.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Starting new HTTPS connection (1): acme-v01.api.letsencrypt.org
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for example.com

-------------------------------------------------------------------------------
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
-------------------------------------------------------------------------------
(Y)es/(N)o: y 

Yを押して継続します。

-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.example.com with the following value:

xxxxxxxxxxxxxxxxxxxxxx //このパスフレーズをコピーする

Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------
Press Enter to Continue  

ムームードメインなど、ドメインを登録しているレジストラにて、_acme-challengeというサブドメインでアクセスできるようにし、TXTレコードで先程のパスフレーズを設定、浸透したらEnterで続行。

Waiting for verification...
Resetting dropped connection: acme-v01.api.letsencrypt.org
Resetting dropped connection: acme-v01.api.letsencrypt.org
Cleaning up challenges

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

コメント

タイトルとURLをコピーしました