概要
nginxを利用した環境で、httpsに対応した開発環境を用意しようと思います。
オレオレ認証局を用いた構築手順など、様々あると思いますが、手っ取り早く環境を用意するために、今回はcertbot
を利用したいと思います。
certbotについては以下のサイトをご参考ください。
https://certbot.eff.org/about/
設定
まずはcertbot
を使えるようにインストールします。
$ sudo dnf install -y epel-release
$ sudo dnf install certbot python3-certbot-nginx
certbot
コマンドで鍵ファイルなどを作成し、自動設定する前に、事前にnginxのdefault.confファイルの設定を確認しておきます。
確認項目としてはserver_name
の値がlocalhostなどではなく、ドメイン名となっていることを確認します。
後述するcertbot
コマンドの実行時に、nginxの設定ファイルの中身を確認し、server_nameの定義が初期状態のままだったりするとうまく処理が完了できません。
server {
server_name tk2-999-99999.vs.sakura.ne.jp;
....
}
上記設定が確認できたら、certbotコマンドを実行して証明書などを作成し、nginxの設定情報も自動で行います。
$ sudo certbot --nginx
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): {emailアドレスを入力}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 ← Agreeしなきゃいけないので
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, 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: N ← 最初に設定したメールアドレス宛にお知らせメールバンバン送っていいか聞いてる
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/default.conf
IMPORTANT NOTES:
- Unable to install the certificate
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/*****/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/****/privkey.pem
Your cert will expire on 2020-12-31. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. To non-interactively renew *all* of
your certificates, run "certbot renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
certbot --nginx
のコマンドを実行すると、nginxのdefault.confファイルに自動的にpemファイルを読み込むような設定などが追記されます。
以下自動的に作成された設定情報です。
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/xxxxxxxxxxx.vs.sakura.ne.jp/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/xxxxxxxxxxx.vs.sakura.ne.jp/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
server {
if ($host = xxxxxxxxxxx.vs.sakura.ne.jp) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name xxxxxxxxxxx.vs.sakura.ne.jp;
return 404; # managed by Certbot
}
この状態でhttpsで接続すると、Welcome to nginx!のページが正常に表示される。
証明書をブラウザで確認するとこのようになっています。