0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【備忘録】nginx で https://で開通するまでの流れ

0
Posted at

この記事は完全に個人的な環境での備忘録です。
記載内容の動作を保証するものではありません。
また、サーバの構築、 nginxのインストール等は済んでいる前提で進めます。
あらかじめご了承お願いします。
まず、前提ですが、

- 対象ドメインがサーバーのグローバルIPを向いていること
  (Aレコード / AAAAレコード)
- 80番ポートが外部から到達可能であること
- nginx が起動していること
- certbot がインストール済みであること
- /var/www/my-project/index.html を公開することを前提に進めていきます。

サーバーにログインしたら etc/nginx/nginx.confを開きます。
http{}の内側にとりあえず以下のスクリプトを追加します。
(my-projectmy.project.dmain は適宜変更)

nginx.conf
    server {
        listen 80;
        server_name my.project.dmain;

        location /.well-known/acme-challenge/ {
            root /var/www/my-project;
            try_files $uri =404;
        }

        location / {
            return 301 https://$host$request_uri;
        }
    }
#     server {
#     listen 443 ssl http2;
#     server_name my.project.dmain;

#     ssl_certificate     /etc/letsencrypt/live/my.project.dmain/fullchain.pem;
#     ssl_certificate_key /etc/letsencrypt/live/my.project.dmain/privkey.pem;

#     root  /var/www/my-project;
#     index index.html;

#     location / {
#         try_files $uri /index.html;
#     }
# }

nginx を再起動

sudo systemctl restart nginx

起動できているか確認

sudo systemctl status nginx

この時点で チャレンジ用のhttpリクエストがアクセスできるようになります。
次に、 チャレンジファイルの置き場を作ります。

sudo mkdir -p /var/www/my-project/.well-known/acme-challenge

証明書発行

sudo certbot certonly --webroot -w /var/www/my-project -d my.project.dmain

以下のような文言が出れば成功

...
Successfully received certificate.
...

先程追加したnginx.confのコメントアウトを外す

     server {
     listen 443 ssl http2;
     server_name my.project.dmain;

     ssl_certificate     /etc/letsencrypt/live/my.project.dmain/fullchain.pem;
     ssl_certificate_key /etc/letsencrypt/live/my.project.dmain/privkey.pem;

     root  /var/www/my-project;
     index index.html;

     location / {
         try_files $uri /index.html;
     }
 }

nginxを再起動

sudo systemctl restart nginx

sudo systemctl status nginx

最後に念の為にssl 自動更新ができるかのテスト

sudo certbot renew --dry-run
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?