|
| 1 | +# TLS Configuration |
| 2 | + |
| 3 | +> **Note:** This configuration is only relevant when running devcontainers locally. If you're using GitHub Codespaces, HTTPS is handled automatically and you can skip this setup. |
| 4 | +
|
| 5 | +When working locally, it's important to consider the end-user experience. To mimic production as closely as possible for debugging purposes, you can setup a local TLS certificate. This devcontainer supports optional TLS (HTTPS) for local development. |
| 6 | + |
| 7 | +## Configuration Options |
| 8 | + |
| 9 | +Two nginx configurations are provided: |
| 10 | + |
| 11 | +| Directory | Description | |
| 12 | +| ---------------- | --------------------------------- | |
| 13 | +| `nginx/default/` | HTTP only (port 80) | |
| 14 | +| `nginx/tls/` | HTTP (port 80) + HTTPS (port 443) | |
| 15 | + |
| 16 | +## Enabling TLS |
| 17 | + |
| 18 | +To enable TLS, modify your `docker-compose.overrides.yml` to: |
| 19 | + |
| 20 | +1. Mount the TLS nginx config (as opposed to the default config) |
| 21 | +2. Mount your certificates directory |
| 22 | +3. Set the certificate filenames |
| 23 | + |
| 24 | +```yaml |
| 25 | +services: |
| 26 | + nginx: |
| 27 | + environment: |
| 28 | + - MAGENTO_TLS_CERT=magento2.test.pem |
| 29 | + - MAGENTO_TLS_KEY=magento2.test-key.pem |
| 30 | + volumes: |
| 31 | + - ./magento2-devcontainer/nginx/tls:/etc/nginx/templates |
| 32 | + - ./certs:/etc/nginx/certs |
| 33 | +``` |
| 34 | +
|
| 35 | +## Certificates |
| 36 | +
|
| 37 | +### Using Default Snakeoil Certificates |
| 38 | +
|
| 39 | +Default self-signed certificates are included in `magento2-devcontainer/certs/`: |
| 40 | + |
| 41 | +- `snakeoil.crt` |
| 42 | +- `snakeoil.key` |
| 43 | + |
| 44 | +These work out of the box but will trigger browser security warnings. |
| 45 | + |
| 46 | +```yaml |
| 47 | +services: |
| 48 | + nginx: |
| 49 | + environment: |
| 50 | + - MAGENTO_TLS_CERT=snakeoil.crt |
| 51 | + - MAGENTO_TLS_KEY=snakeoil.key |
| 52 | + volumes: |
| 53 | + - ./magento2-devcontainer/nginx/tls:/etc/nginx/templates |
| 54 | + - ./magento2-devcontainer/certs:/etc/nginx/certs |
| 55 | +``` |
| 56 | + |
| 57 | +### Using mkcert (Recommended) |
| 58 | + |
| 59 | +[mkcert](https://github.com/FiloSottile/mkcert) generates locally-trusted certificates that work without browser warnings. |
| 60 | + |
| 61 | +1. Install mkcert on your system: |
| 62 | + |
| 63 | + ```bash |
| 64 | + # macOS |
| 65 | + brew install mkcert |
| 66 | +
|
| 67 | + # Linux |
| 68 | + sudo apt install mkcert |
| 69 | + # or |
| 70 | + sudo pacman -S mkcert |
| 71 | +
|
| 72 | + # Windows |
| 73 | + choco install mkcert |
| 74 | + ``` |
| 75 | + |
| 76 | +2. Install the local CA (this registers mkcert's root certificate with your system): |
| 77 | + |
| 78 | + ```bash |
| 79 | + mkcert -install |
| 80 | + ``` |
| 81 | + |
| 82 | +3. Generate certificates for your domain and place them in the certs directory: |
| 83 | + |
| 84 | + ```bash |
| 85 | + YOUR_TEST_DOMAIN=magento2.test |
| 86 | + mkdir -p .devcontainer/magento2/certs |
| 87 | + cd .devcontainer/magento2/certs |
| 88 | + mkcert -key-file $YOUR_TEST_DOMAIN-key.pem -cert-file $YOUR_TEST_DOMAIN.pem $YOUR_TEST_DOMAIN |
| 89 | + ``` |
| 90 | + |
| 91 | +4. Update your `docker-compose.overrides.yml` with the cert filenames as shown above. |
| 92 | + |
| 93 | +At this point, you should have a working TLS cert covering the `$YOUR_TEST_DOMAIN` domain. |
| 94 | + |
| 95 | +## Environment Variables |
| 96 | + |
| 97 | +| Variable | Default | Description | |
| 98 | +| ------------------ | -------------- | -------------------------------------------- | |
| 99 | +| `MAGENTO_TLS_CERT` | `snakeoil.crt` | Certificate filename in `/etc/nginx/certs` | |
| 100 | +| `MAGENTO_TLS_KEY` | `snakeoil.key` | Private key filename in `/etc/nginx/certs` | |
| 101 | + |
| 102 | +## Troubleshooting |
| 103 | + |
| 104 | +### Browser still shows certificate warnings with mkcert |
| 105 | + |
| 106 | +Make sure you ran `mkcert -install` before generating the certificates. If you generated certs first, delete them and regenerate after running the install command. |
| 107 | + |
| 108 | +### Certificate not found errors |
| 109 | + |
| 110 | +Verify that: |
| 111 | + |
| 112 | +- The certs directory is mounted to `/etc/nginx/certs` |
| 113 | +- The `MAGENTO_TLS_CERT` and `MAGENTO_TLS_KEY` environment variables match the actual filenames in your certs directory |
0 commit comments