見出し画像

Encryptを使用してNginxでhttps接続

https化をしていないと、使えないツールもあるため開発環境もSSL証明書を導入する。

今回の環境は
・Ubuntu 20.04
・nginx。

Let’s Encryptで証明書を発行し、https接続できるよう構成する。

Certbotのインストール

証明書の発行や自動更新を行ってくれる便利なツール

sudo apt install certbot python3-certbot-nginx

※ARM版は以下を実行

certbot certonly

nginxの設定ファイルを編集

sudo vi /etc/nginx/sites-available/default

server_nameにsslを発行するドメイン名を指定

server_name _;
server_name www.example.com;

設定が正しく記載されているかテスト

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

テストでエラーが出なければ、nginxサービスをリロード

sudo service nginx restart

SSL証明書の取得

sudo certbot --nginx -d [ドメイン名]

上記を実行してSSL証明書の発行を実行

sudo certbot --nginx -d www.example.com

メールアドレスの入力や、各種規約の確認

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [メールアドレスを入力]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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: Y
・・・・

Certbotの自動更新の検証

sudo systemctl status certbot.timer


更新処理

sudo certbot renew --dry-run

「Congratulations, all renewals succeeded.」と表示されれば問題なし

実際の更新は「--dry-run」を外して行う

sudo certbot renew

有効期限の確認

certbot certificates