開発運用

Django Docker Nginx セキュリティ

Django アプリケーションを Docker コンテナで実行し、Nginx と Let’s Encrypt を使用してセキュリティを強化する方法について、詳細な手順を以下に示します。このプロセスにより、Django アプリケーションを本番環境にデプロイし、セキュリティを高めることができます。以下の手順を順番に進めていきます。

1. 前提条件

この手順を始める前に、以下のツールがインストールされている必要があります:

  • Docker
  • Docker Compose
  • Nginx
  • Certbot(Let’s Encrypt 用)

さらに、Django プロジェクトがすでに作成されている必要があります。Django プロジェクトの作成方法については、公式ドキュメントを参照してください。

2. プロジェクト構造

まず、以下のような基本的なプロジェクト構造を作成します。

cpp
myproject/ │ ├── Dockerfile ├── docker-compose.yml ├── nginx/ │ └── default.conf └── myproject/ └── settings.py

3. Dockerfile の作成

まず、Django アプリケーションを Docker コンテナ内で実行するために、Dockerfile を作成します。このファイルは、Django アプリケーションの依存関係をインストールし、コンテナ内でアプリケーションを実行するために必要な設定を行います。

Dockerfile
# Python 3.9 の公式イメージを使用 FROM python:3.9-slim # 必要なパッケージをインストール RUN apt-get update && apt-get install -y \ libpq-dev \ && rm -rf /var/lib/apt/lists/* # 作業ディレクトリを設定 WORKDIR /app # ローカルのファイルをコンテナにコピー COPY . /app/ # 仮想環境を作成し、依存関係をインストール RUN pip install --upgrade pip RUN pip install -r requirements.txt # マイグレーションを適用し、静的ファイルを収集 RUN python manage.py migrate RUN python manage.py collectstatic --noinput # アプリケーションを起動 CMD ["gunicorn", "myproject.wsgi:application", "--bind", "0.0.0.0:8000"]

4. docker-compose.yml の作成

次に、Docker Compose を使用して複数のコンテナを管理します。これには、Django アプリケーション、データベース(PostgreSQL など)、および Nginx コンテナが含まれます。

yaml
version: '3.8' services: db: image: postgres:13 environment: POSTGRES_DB: myproject POSTGRES_USER: user POSTGRES_PASSWORD: password volumes: - db_data:/var/lib/postgresql/data networks: - mynetwork web: build: . command: gunicorn myproject.wsgi:application --bind 0.0.0.0:8000 volumes: - .:/app expose: - "8000" depends_on: - db networks: - mynetwork nginx: image: nginx:latest volumes: - ./nginx/default.conf:/etc/nginx/conf.d/default.conf - .:/app ports: - "80:80" - "443:443" depends_on: - web networks: - mynetwork certbot: image: certbot/certbot volumes: - ./certbot/conf:/etc/letsencrypt - ./certbot/www:/var/www/certbot entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'" networks: - mynetwork volumes: db_data: networks: mynetwork: driver: bridge

5. Nginx 設定の作成

次に、Nginx の設定ファイル default.conf を作成します。このファイルは、Nginx が HTTP および HTTPS トラフィックを受け入れ、Django アプリケーションにプロキシする方法を定義します。

nginx
server { listen 80; server_name example.com www.example.com; # HTTP から HTTPS へのリダイレクト 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; include /etc/letsencrypt/options-ssl-nginx.conf; ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; location / { proxy_pass http://web:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }

6. Let’s Encrypt で SSL 証明書を取得

次に、Certbot を使用して Let’s Encrypt から SSL 証明書を取得します。以下のコマンドを実行して、証明書を取得します。

bash
docker-compose run --rm certbot certonly --webroot --webroot-path=/var/www/certbot -d example.com -d www.example.com

このコマンドにより、証明書が生成され、/etc/letsencrypt フォルダに保存されます。この証明書は、Nginx コンテナで使用されます。

7. サービスの起動

すべての設定が完了したら、以下のコマンドを実行してコンテナを起動します。

bash
docker-compose up --build

これにより、Django アプリケーション、PostgreSQL データベース、Nginx、Certbot のサービスが起動します。

8. ドメインの設定

最後に、example.com を実際のドメイン名に置き換え、DNS 設定でそのドメインがあなたのサーバーの IP アドレスを指すようにします。

9. 自動更新の設定

Let’s Encrypt の証明書は 90 日間で期限切れになります。そのため、Certbot による証明書の自動更新を設定します。docker-compose.yml に記載されている certbot サービスが定期的に証明書を更新しますが、サーバーの再起動時に Certbot の自動更新が有効になるように設定します。

10. 最後に

これで、Django アプリケーションが Docker コンテナ内で実行され、Nginx と Let’s Encrypt によってセキュリティが強化された状態で運用されるようになりました。これにより、本番環境でも安全にアプリケーションを運用できるようになります。

Back to top button