0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Xserver VPSでhttps化【SSL化】(Django)

Posted at

🚀 nginx + Django(Gunicorn)環境への SSL(HTTPS)導入手順

経緯

こちらの記事を参考に, xserverにてSSL化を行っていたのですが、すでにDjangoのアプリケーションを構築していたため、

Could not bind TCP port 80 because it is already in use by another process on
this system (such as a web server). Please stop the program in question and then
try again.

というエラーが出たので、備忘録として、記載しておきます

前提条件

  • Django アプリは Gunicorn を介して unix または 127.0.0.1:8000 などで提供中
  • nginx がリバースプロキシとして設定済
  • 独自ドメインがDNSで正しく定義されている

1. Certbot と nginx プラグインのインストール

sudo apt update
sudo apt install certbot python3-certbot-nginx

python3-certbot-nginx を入れることで、nginx 設定を自動解析し SSL 設定を丸っと自動化してくれます。

2. nginx 設定の事前確認

/etc/nginx/sites-available/your_project に以下のような server ブロックがあるか確認:

server {
    listen 80;
    server_name example.com www.example.com;

    location /static/ {
        alias /path/to/staticfiles/;
    }

    location / {
        proxy_pass http://unix:/path/to/gunicorn.sock;
        include proxy_params;
    }
}

念のために設定テストと再読み込み:

sudo nginx -t && sudo systemctl reload nginx

Certbot はこの server_name をもとに証明書発行対象を認識します 。

3. Certbot で証明書取得+HTTPS化+リダイレクト設定

sudo certbot --nginx -d example.com -d www.example.com
  • 証明書の取得
  • nginx 設定ファイルへの ssl_certificate と ssl_certificate_key の自動追加
  • HTTP → HTTPS のリダイレクト設定
  • nginx 再読み込み
    これだけの処理が自動完了します。

4. HTTPS 状態の確認

ブラウザで以下をチェック:

https://example.com
https://www.example.com

安全な鍵アイコンが出ていれば、設定OKです。

さらに SSL Labs([https://www.ssllabs.com/ssltest/](https://www.ssllabs.com/ssltest/))でスコアを確認するのもおすすめ 。

  1. 自動更新の有効化とテスト

Certbot は自動で systemd タイマー(または cron)をセットアップします。

動作確認:

タイマー確認

sudo systemctl list-timers | grep certbot

更新テスト

sudo certbot renew --dry-run

テストが通ったら、本番更新も問題なしです 。

  1. nginx 設定全体のイメージ
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location /static/ {
        alias /path/to/staticfiles/;
    }

    location / {
        proxy_pass http://unix:/path/to/gunicorn.sock;
        include proxy_params;
    }
}

— Certbot 実行後は、このように HTTPS ブロックが自動挿入され、HTTP→HTTPS リダイレクトも追加されます。

まとめ

  • Certbot導入 sudo apt install certbot python3-certbot-nginx
  • nginx確認 server_name が正しいこと、設定テスト済
  • SSL取得・HTTPS化 sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
  • 動作確認 鍵アイコン表示、SSL Labs テスト
  • 自動更新 タイマー設定済、sudo certbot renew --dry-run でテストOK

参考

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?