開発したアプリなど一覧

Ubuntu 18.04 と Nginx で Let's Encrypt を利用して SSL 化する

アフィリエイトリンクを含む場合があります

このブログに限らず自分の運営している Web サイトはすべて Let's Encrypt を利用して SSL 化しているため、サーバー移行した際には SSL の設定も行う必要があった。新しいサーバーでも Let's Encrypt を利用して SSL の設定を行おう。

Let's Encrypt - Free SSL/TLS Certificates

まず Let's Encrypt を利用するためのコマンド certbot-auto をインストールする。これは certbot.eff.org よりダウンロードできる。

$ sudo wget https://dl.eff.org/certbot-auto -O /usr/sbin/certbot-auto $ sudo chmod a+x /usr/sbin/certbot-auto

Ubuntu なら apt でもインストールできるようだ。

certbot-auto コマンドが利用できるようになったら証明書を取得する。

$ sudo certbot-auto certonly -d example.com How would you like to authenticate with the ACME CA? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: Nginx Web Server plugin (nginx) 2: Spin up a temporary webserver (standalone) 3: Place files in webroot directory (webroot) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-3] then [enter] (press 'c' to cancel): 1 Plugins selected: Authenticator nginx, Installer None (省略)

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 (省略)

認証を行う際に httpd が必要となるが既に Nginx が動いていたので 1 を選択した。何もサーバーが立ち上がっていなければ standalone の 2 を選択する。

正常に認証できれば上記のように "Congratulations!" と言われると思う。SSL に必要な fullchain.pem と privkey.pem などのファイルが保存される。これらファイルを Nginx の設定ファイルより読み込もう。

$ sudo vim /etc/nginx/sites-available/example.com # http:// へのアクセスはすべて https:// へリダイレクトする server { listen 80; server_name example.com; return 301 https://$host$request_uri; }

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

root /home/ryomatsu/public_html/example.com; server_name example.com www.example.com; # 省略 }

最後に設定ファイルが正常か確認して Nginx を再起動しよう。

$ sudo nginx -t $ sudo systemctl restart nginx

これで https でアクセス可能だ。

Lets' Encrypt の証明書は3か月で切れるので都度 sudo certbot-auto renew コマンドを利用して更新しなければならない。手動でやるのは面倒なので crontab にでも登録しておこう。

Sponsored Link

コメント

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