LoginSignup
12
11

More than 5 years have passed since last update.

Let's Encrypt CertBotで無料のSSL証明書を自動で取得

Posted at

環境

$ cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

準備

予めサーバの80番と443番ポートを開けておきます。AWS等なら管理コンソールやCLI等を使います。
※先にやっておかないと取得時に失敗します。

あとは当たり前ですが、SSL化したいドメインを取ってAレコードを差し向けておきます。
今回の例では www.example.co.jp を使います。

CertBotでSSL証明書を取得

$ git clone https://github.com/certbot/certbot
$ cd certbot/
$ ./certbot-auto certonly --standalone -d www.example.co.jp

すると /etc/letsencrypt 下に色々と配置されます。

/etc/letsencrypt/live に実際の証明書へのシンボリックリンクが貼られており、実際のwebサーバの設定ではこちらを参照することになります。

$ sudo ls -la /etc/letsencrypt/live/www.example.co.jp
合計 0
drwxr-xr-x 2 root root 75 10月  2 23:35 .
drwx------ 3 root root 39 10月  2 23:35 ..
lrwxrwxrwx 1 root root 50 10月  2 23:35 cert.pem -> ../../archive//cert1.pem
lrwxrwxrwx 1 root root 51 10月  2 23:35 chain.pem -> ../../archive/www.example.co.jp/chain1.pem
lrwxrwxrwx 1 root root 55 10月  2 23:35 fullchain.pem -> ../../archive/www.example.co.jp/fullchain1.pem
lrwxrwxrwx 1 root root 53 10月  2 23:35 privkey.pem -> ../../archive/www.example.co.jp/privkey1.pem

ローカルの3000番で動いているAppサーバをSSL化

Railsの開発中、ログイン機能を作るために開発用のSSLを用意したかったので、下記設定でnginxからPuma(Webrickでも同様)へとproxyしました。

/etc/nginx/conf.d/rails.conf
server {
  listen 443;
  ssl on;
  server_name www.example.co.jp;
  ssl_certificate     /etc/letsencrypt/live/www.example.co.jp/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/www.example.co.jp/privkey.pem;

  location / {
    proxy_pass http://localhost:3000;
  }
}

nginxを起動し、 https://www.example.co.jp/ でアクセスできたら成功です。

無料で、数分でSSL証明書が取得できました。

良い時代になりました。

参考

12
11
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
12
11