PHP on Previewops
Previewops builds whatever your Dockerfile produces and runs it as a container, so PHP needs no special support. What it does need is what every stack needs: read the injected PORT and listen on 0.0.0.0.
| Read the port | getenv("PORT") |
| Bind all interfaces | php -S 0.0.0.0:$PORT |
Dockerfile
FROM php:8.3-cli
WORKDIR /app
COPY . .
ENV PORT=8080
CMD ["sh", "-c", "php -S 0.0.0.0:$PORT -t public"]
ENV PORT=8080 is only a default. Previewops overrides it at deploy time, so the app must read the value rather than assume it.
Application code
A typical setup with the built-in server (previews) or PHP-FPM (production):
<?php
// public/index.php
$port = getenv('PORT') ?: '8080';
http_response_code(200);
echo 'ok';
PHP-specific pitfalls
php -Sis single-threaded. Fine for a preview environment, but do not carry this Dockerfile into production — use PHP-FPM behind nginx, or FrankenPHP, for real traffic.- The document root matters.
-t publicpoints the server at your front controller; without itphp -Sserves the repository root. - Laravel: run
php artisan config:cacheat build time and setAPP_KEYas an env secret. Do not run migrations from the start command.
Configuration
If your Dockerfile is not at the repository root, point at it from .previewops.yaml:
provider: cloud-run
dockerfile: ./docker/Dockerfile
port: 8080
ttl_hours: 24
See configuration.md for every available option.
Do not gate startup behind a migration
Language-agnostic, and the hardest failure here to diagnose:
# Avoid this
CMD ["sh", "-c", "run-migrations && start-server"]
If the migration cannot reach the database — a paused instance, an expired connection string, a cold-start timeout — it never exits, the server never starts, and the port never opens. The deploy itself already succeeded, so the preview shows as Live while nothing answers. Run migrations as a separate, visible step instead.
Related pages
- getting-started.md — your first deploy end to end
- configuration.md —
.previewops.yamlreference - databases.md — connecting a database to a preview
- ui-deploy.md — deploying from the dashboard