Infrastructure

Docker and Nginx: reliable deployments step by step

Docker packages your application and Nginx serves it predictably. The step-by-step setup to deploy PHP and Laravel without surprises.

"It works on my machine" stops being an excuse once the production environment is described in code. Docker packages the application with its dependencies and Nginx serves it fast and predictably. This is the setup I use to deploy PHP and Laravel applications reliably, step by step.

1. Split the services

One container per responsibility: the application (PHP-FPM), the web server (Nginx), the database and, if needed, Redis and a queue worker. That way you can update, scale or restart each piece without touching the others, and each service keeps its own logs.

2. A reproducible application image

The image must always build the same way: a pinned PHP version, explicit extensions and dependencies installed from composer.lock. Copying the Composer files first takes advantage of layer caching and speeds up builds considerably:

FROM php:8.3-fpm-alpine AS app
RUN apk add --no-cache icu-dev libzip-dev \
 && docker-php-ext-install intl pdo_mysql zip opcache
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html
COPY composer.json composer.lock ./
RUN composer install --no-dev --no-scripts --prefer-dist --no-interaction
COPY . .
RUN composer dump-autoload --optimize \
 && chown -R www-data:www-data storage bootstrap/cache

3. Nginx in front of PHP-FPM

Nginx serves static files directly and only passes to PHP what needs to run. A base configuration for Laravel:

server {
    listen 80;
    server_name example.com;
    root /var/www/html/public;
    index index.php;

    client_max_body_size 20M;
    gzip on;
    gzip_types text/css application/javascript application/json image/svg+xml;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass app:9000;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* \.(css|js|webp|png|jpg|svg|woff2)$ {
        expires 30d;
        access_log off;
    }
}

4. Configuration and secrets outside the image

The image must not contain passwords or the .env file. Pass them as environment variables or files mounted on the server, and use the same image in staging and production, changing only the configuration.

5. Persistent data in volumes

Database, user uploads and storage live in volumes, not inside the container. A container must be disposable without losing anything. And volumes need automatic, tested backups: a backup that has never been restored is not a backup.

6. A deploy in a few commands

Deploying has to be a script, not a list of steps someone remembers. A simple example:

#!/usr/bin/env bash
set -euo pipefail
git pull --ff-only
docker compose build app
docker compose up -d --no-deps app nginx
docker compose exec -T app php artisan migrate --force
docker compose exec -T app php artisan optimize
docker compose exec -T app php artisan queue:restart

With backward-compatible migrations and container healthchecks, downtime during a deploy is minimal. If you need zero downtime, the next step is to bring up the new version in parallel and switch traffic once it is healthy.

7. Watch what happens in production

  • Centralised, rotated logs for every container.
  • Alerts for disk, memory and service outages.
  • Automated HTTPS with monitored certificate renewal.
  • Regular base image updates for security.

Common mistakes

  • Using latest as the image version.
  • Storing uploaded files inside the container.
  • Deploying by hand without a script.
  • Never testing a backup restore.

If you want to move your applications to containers or make your deploys more reliable, tell me how you work today and I will propose a step-by-step plan. You can also see my Laravel development services.

Want to apply these ideas to your project?

We can review the context and define the next step.