Header Ads Widget

Game with eBay Refurbished. ebay tracking pixel

Build a Modern PHP Project on Windows 11 Using Docker, Traefik, and SSL – Step by Step

Build a Modern PHP Project on Windows 11 Using Docker, Traefik, and SSL – Step by Step

This article walks you through creating a small PHP project using Docker on Windows 11, including all installation steps, common pitfalls, and how to make it accessible via a custom domain using Traefik with HTTPS.

🔧 Step 1: Install Tools via Chocolatey or Scoop

Open PowerShell as Administrator and install a package manager:

Set-ExecutionPolicy Bypass -Scope Process -Force;
iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex

Or with Scoop:

iwr -useb get.scoop.sh | iex

Then install git and Docker Desktop:

choco install git docker-desktop -y

🐳 Step 2: Set Up Docker Desktop

  • Open Docker Desktop after installation.
  • Enable WSL2 backend and restart if prompted.

📁 Step 3: Create Project Structure


E:\projects\hello-php
├── docker-compose.yml
├── traefik.yml
├── certs\hello.localhost.pem
├── certs\hello.localhost-key.pem
└── php\index.php
  

📜 docker-compose.yml

version: '3'

services:
  traefik:
    image: traefik:v2.11
    command:
      - "--providers.docker=true"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--api.dashboard=true"
      - "--configFile=/etc/traefik/traefik.yml"
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - ./traefik.yml:/etc/traefik/traefik.yml
      - ./certs:/certs
      - /var/run/docker.sock:/var/run/docker.sock

  php:
    image: php:8.2-apache
    volumes:
      - ./php:/var/www/html
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.php.rule=Host(`hello.localhost`)"
      - "traefik.http.routers.php.entrypoints=websecure"
      - "traefik.http.routers.php.tls=true"
      - "traefik.http.routers.php.tls.certificates[0].certFile=/certs/hello.localhost.pem"
      - "traefik.http.routers.php.tls.certificates[0].keyFile=/certs/hello.localhost-key.pem"
  

⚙️ traefik.yml

entryPoints:
  web:
    address: ":80"
  websecure:
    address: ":443"

providers:
  docker:
    exposedByDefault: false

api:
  dashboard: true
  

👩‍💻 php/index.php

<?php
  echo "Hello from Docker + Traefik + SSL!";
?>
  

🧪 Step 4: Hosts File

Edit your hosts file at C:\Windows\System32\drivers\etc\hosts:

127.0.0.1 hello.localhost

🚀 Step 5: Run the Project

cd E:\projects\hello-php
docker-compose up -d

🐞 Troubleshooting & Common Issues

  • ERR_CONNECTION_REFUSED: Check that the firewall allows port 80, 443, and 8080.
  • field not found, node: certificates: Ensure the certificate files are valid and paths are correct.
  • Dashboard not reachable: Ensure 8080 is exposed in docker-compose.yml and Traefik API is enabled.

🛡️ Firewall Rule Commands (Run in Admin PowerShell)

New-NetFirewallRule -DisplayName "Allow Traefik 80" -Direction Inbound -LocalPort 80 -Protocol TCP -Action Allow
New-NetFirewallRule -DisplayName "Allow Traefik 443" -Direction Inbound -LocalPort 443 -Protocol TCP -Action Allow
New-NetFirewallRule -DisplayName "Allow Traefik 8080" -Direction Inbound -LocalPort 8080 -Protocol TCP -Action Allow
  

📬 Final Thoughts

Once it's working, you can view your app at https://hello.localhost and the Traefik dashboard at http://localhost:8080. For more development tools and ideas, check out GPT日本語版.

Post a Comment

0 Comments