How to Set Up a Staging Site That Mirrors Production Exactly
A staging site that mirrors production is a copy of your live environment where you can deploy and test without risking real traffic. Mirroring means matching the things that actually change program behaviour: the web server and its configuration, the PHP version and loaded extensions, the database engine and schema, and the environment variables your code reads. If any of those differ, your staging tests tell you less than you think.
Why a staging site that mirrors production must match the runtime, not just the code
Most deployment bugs are not logic bugs. They are differences between environments: a missing PHP extension, a different default timezone, a web server that rewrites URLs differently, or a database that sorts strings in another collation. Copying the codebase and the database gets you partway there. The runtime is where the surprises live, so capture it from the live host rather than guessing.
Start by recording exactly what production runs. On the live server, dump the PHP configuration and the loaded modules, and note the web server version and the database server version:
php -v
php -m
php -i | grep -E 'memory_limit|upload_max_filesize|post_max_size|date.timezone'
nginx -v 2>&1 || httpd -v
mysql --version || psql --version
The output of php -m is a plain list of extension names, one per line. Save it. That list is your checklist for the staging box. The php -i lines give you the directives that most often differ between hosts and break uploads or date handling quietly.
Match the PHP version and extensions exactly
Pin the same minor version, not just the major one. A patch release can change deprecation warnings into errors depending on your error reporting settings, and a minor release can change behaviour in ways that only show up at runtime. Install the matching version through your distribution's package manager or a version manager, then install the same extension set.
Compare the two lists mechanically rather than by eye. On staging, run the same php -m and diff it against the saved production output:
php -m | sort > /tmp/staging-ext.txt
sort /tmp/prod-ext.txt -o /tmp/prod-ext.txt
diff /tmp/prod-ext.txt /tmp/staging-ext.txt
An empty diff means the extension sets match. Anything printed is a gap to close. Pay attention to extensions that are easy to forget because they are compiled in by default on one host and packaged separately on another, such as the database drivers, the image libraries, and the internationalisation extension.
Then copy the ini directives that matter. Rather than editing the global php.ini by hand, put the production values in a dedicated file so the difference is explicit and reviewable. For example, in a file under the PHP configuration directory for your version, such as /etc/php/8.x/fpm/conf.d/99-staging.ini:
memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
date.timezone = UTC
display_errors = Off
log_errors = On
Match display_errors and log_errors to production too. It is tempting to leave errors on in staging for convenience, but then you never test how the application behaves when they are hidden, which is exactly the condition your users see.
Mirror the web server and its request handling
The web server decides how requests reach your application, and small differences break things. If production runs nginx with PHP over FastCGI, staging should too, not Apache with mod_php. The two differ in how they set PATH_INFO, how they handle Authorization headers, and how they apply rewrite rules.
Copy the relevant server configuration verbatim where you can: the server block or virtual host, the rewrite rules, the location blocks, and any custom headers. Then confirm that the headers your application emits on staging match production. Use curl against both and compare:
curl -sI https://staging.example.test/ | sort > /tmp/staging-headers.txt
curl -sI https://example.test/ | sort > /tmp/prod-headers.txt
diff /tmp/prod-headers.txt /tmp/staging-headers.txt
Ignore headers that legitimately differ, such as Date, Set-Cookie values and server timing headers, and focus on the ones that affect behaviour: content type, cache control, security headers, and anything your application sets. If production sends a strict transport security header and staging does not, you will not notice a misconfiguration until it is live.
Also check how the server passes the client IP and the protocol. Behind a proxy, the application often reads X-Forwarded-For and X-Forwarded-Proto. If staging terminates TLS differently, your framework may generate http links or misread the client address. Configure staging to send the same headers your production proxy sends, and set the framework's trusted proxy list the same way.
Match the database engine, schema and data shape
Use the same database engine and the same major version. A schema that relies on a feature present in one version and absent in another will fail on deploy, and collation differences change sort order and case sensitivity in ways that are hard to spot in a test suite. Dump the schema from production and load it into staging rather than letting your migration tool build a fresh database, so the starting state is identical.
Take a recent copy of production data, but scrub anything sensitive before it lands on staging. Real data exposes edge cases that generated fixtures miss: long strings, odd encodings, nulls where you assumed values. The scrubbing step is not optional, and it should happen as part of the copy, not after. If your database supports it, dump with a consistent snapshot so the copy is not torn mid write.
Finally, match the configuration the application reads. Environment variables, secret names and feature flags should have the same keys as production, even if the values point at staging services. A missing key that defaults silently in one environment and throws in another is a classic deploy surprise. Keep a template of the required keys and check staging against it before every deploy, so the check is repeatable rather than remembered.
Next, write the whole comparison down as a script you can run against staging after each rebuild. The commands above are a reasonable starting point: version checks, an extension diff, a header diff and a schema comparison. Run it before you trust a staging result, and treat a clean run as the precondition for deploying. When the script passes, a bug that appears in staging is a real bug, and that is the whole point of mirroring production.
