You should never test a plugin update, a theme change, or a PHP upgrade on the site your visitors are using right now. That is exactly what a WordPress staging site is for: a private, working copy of your live site where you can break things safely, find out what actually breaks, and only then push the change to production. This guide shows you how to build one by hand on your own VPS, with WP-CLI doing the heavy lifting — no managed-host staging button and no plugin required.
If your host offers one-click staging, use it — it is the easiest path and it is already tested on that host’s servers. But if you run WordPress on your own VPS, there is no button. The manual method below takes about 30 minutes the first time, and once you have done it once, you can repeat it in five commands. You will also understand exactly how your site is wired together, which pays off the next time something goes wrong at 2am.
Pick your staging method first
There are three ways to get a WordPress staging site. They all end up in the same place — a copy of your site on a different address — but they differ in how much control you get and how much work you do.
| Method | Best for | Trade-off |
|---|---|---|
| Your host’s staging tool | Managed WordPress hosting | Easiest, but only if your host offers it |
| A staging plugin (e.g. WP Staging) | Hosts without a staging feature | Click-through setup, but it runs on your server’s resources |
| Manual copy with WP-CLI | Your own VPS, or full control | More steps, but nothing hidden and nothing to pay for |
This guide covers the third method. If you would rather use a plugin, the free version of WP Staging clones your site into a subdirectory from your dashboard — but on a VPS you already have everything the manual method needs, and it is worth learning.
What you need before you start
- SSH access to your VPS and a user that can write to your web root.
- WP-CLI installed and working on your live site (run
wp --infoto check). - A full backup of your live site — files and database — taken before you touch anything. If you do not have a backup routine yet, set one up first; it is the one thing that turns a disaster into an inconvenience.
- A subdomain you control, for example
staging.example.com, with a DNS record pointing at your server.
One rule governs the whole process: the staging copy must never be able to affect production. That means its own database, its own URLs, no indexing by search engines, and no real emails going out. Every step below exists to enforce one of those four separations.
Step 1: Create the staging subdomain and point DNS at it
Create a DNS A record for staging.example.com pointing at your server’s IP address, then wait for it to resolve — check with dig +short staging.example.com or simply ping. A subdomain is the right home for staging: it is clearly separate from production, it gets its own Nginx server block, and it can be deleted later without touching anything else.
Next, create the directory and a minimal Nginx server block for it. Adapt the PHP-FPM socket path to whatever your server uses:
sudo mkdir -p /var/www/staging.example.com
sudo nano /etc/nginx/sites-available/staging.example.com
server {
listen 80;
server_name staging.example.com;
root /var/www/staging.example.com;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
}
sudo ln -s /etc/nginx/sites-available/staging.example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d staging.example.com
That last command gives your staging copy its own HTTPS certificate. Staging over plain HTTP is a bad habit: browsers will warn you, some plugins behave differently without HTTPS, and you want the copy to behave like production as closely as possible.
Step 2: Copy the files, skipping the caches
Copy the live site’s files into the staging directory with rsync. Exclude cache directories — they are regenerated automatically and can be enormous — but keep wp-content/uploads, because your content’s images live there:
rsync -a \
--exclude='wp-content/cache' \
--exclude='wp-content/uploads/cache' \
/var/www/example.com/ /var/www/staging.example.com/
Make sure the staging files are owned by the same web user as production (typically www-data) so PHP-FPM can read and write them:
sudo chown -R www-data:www-data /var/www/staging.example.com
Step 3: Clone the database into its own database
Never point staging at your live database. Create a separate database and user, then export production and import it into the staging database with WP-CLI:
cd /var/www/example.com
wp db export ~/prod-$(date +%F).sql
mysql -u root -p -e "CREATE DATABASE example_staging; CREATE USER 'example_stg'@'localhost' IDENTIFIED BY 'a-strong-password'; GRANT ALL PRIVILEGES ON example_staging.* TO 'example_stg'@'localhost'; FLUSH PRIVILEGES;"
cd /var/www/staging.example.com
wp db import ~/prod-$(date +%F).sql
Using a separate database user for staging is not paranoia — it is the boundary that stops a mistake on staging (a dropped table, a bad search-replace) from ever touching production data.
Step 4: Point the staging wp-config.php at the new database
Edit the staging copy’s wp-config.php — not the live one — and update the database credentials to the staging database you just created:
cd /var/www/staging.example.com
wp config set DB_NAME example_staging
wp config set DB_USER example_stg
wp config set DB_PASSWORD 'a-strong-password'
While you are in there, add two more settings that mark this install as staging. The first tells WordPress — and every plugin that checks — what kind of environment this is. WordPress accepts four values, local, development, staging and production, and defaults to production when nothing is set:
wp config set WP_ENVIRONMENT_TYPE staging
wp config set WP_DEBUG true --raw
Enabling WP_DEBUG on staging is deliberate: this is where you want to see warnings. Keep it off on production. If you have not used the debug log before, it is worth learning to read it — it turns “something broke” into a file and line number.
One more thing worth doing while you are here: generate fresh authentication salts for the staging copy. Shared salts between live and staging mean a session cookie from one could be replayed against the other, so do not copy them verbatim. You can regenerate them with wp config shuffle-salts in the staging directory.
Step 5: Rewrite every URL with wp search-replace
Right now your staging database still thinks it lives at https://example.com. Every internal link, image URL and plugin setting points at production. Fixing this by hand in the database is a trap: WordPress stores plugin settings and widget data as serialized PHP strings, and a raw SQL REPLACE() changes the string without updating its length prefix, silently corrupting the data. wp search-replace handles serialized data correctly — it unserializes each value, replaces, and re-serializes with the correct lengths.
Always dry-run first. The dry run reports how many replacements would happen in each table without writing anything, so you can spot a wrong URL before it becomes permanent:
wp search-replace 'https://example.com' 'https://staging.example.com' \
--all-tables --precise --recurse-objects --skip-columns=guid --dry-run
Read the report. Zero replacements where you expected thousands means your search string is wrong; replacements in a table you did not expect means you should investigate before proceeding. When the report looks right, run the same command without --dry-run. The --skip-columns=guid flag is there on purpose: GUIDs are meant to be permanent identifiers for feed readers, and rewriting them can break feed subscriptions, so they are left alone.
Step 6: Lock the staging site down
An open staging copy is a duplicate of your entire site sitting on a guessable subdomain. Lock it down in three layers, because no single layer is enough on its own.
Keep it out of search results
Set the staging site to discourage indexing. This flips the blog_public option, which adds a noindex rule:
wp option update blog_public 0
You can also tick Settings > Reading > Discourage search engines from indexing this site in the staging dashboard. But understand its limit: noindex hides the site from search results, it does not hide it from people. Anyone with the link can still open it — which is why you need the next layer too.
Put a password in front of it
HTTP basic authentication at the Nginx level keeps everyone out except your team, and it keeps the site out of search engines entirely. Add this inside the staging server block:
location / {
auth_basic "Staging - authorised users only";
auth_basic_user_file /etc/nginx/.htpasswd-staging;
try_files $uri $uri/ /index.php?$args;
}
sudo htpasswd -c /etc/nginx/.htpasswd-staging yourname
sudo nginx -t && sudo systemctl reload nginx
Stop it sending real emails
This is the step most guides skip, and it is the one that causes real damage: a staging copy with a live database will happily send password resets, order confirmations and newsletter emails to your real users when you trigger something on staging. The cleanest fix is a must-use plugin that short-circuits mail entirely:
mkdir -p /var/www/staging.example.com/wp-content/mu-plugins
cat > /var/www/staging.example.com/wp-content/mu-plugins/disable-mail.php <<'PHP'
<?php
// Staging only: never send real email from this copy.
add_filter( 'pre_wp_mail', '__return_true' );
PHP
Returning a non-null value from the pre_wp_mail filter tells WordPress the mail was already handled, so nothing is actually sent. If you would rather inspect what staging would send, use a mail-logging plugin instead and route everything to a test inbox — but never leave staging able to reach real inboxes.
Step 7: Prove the clone actually works
Do not assume the clone is good because the homepage loads. Run through this checklist before you trust it:
- Log in to
https://staging.example.com/wp-adminwith your live credentials — the clone carries the same users. - Confirm the URLs are right:
wp option get siteurlandwp option get homeshould both return the staging address. - Open a post with images and check the image URLs point at staging, not production.
- Run
wp cron event run --due-now— scheduled work exercises code paths a page render never reaches, and it is worth knowing they work on staging. - Check the REST API responds:
curl -sS -o /dev/null -w '%{http_code}\n' https://staging.example.com/wp-json/should return 200. - Deliberately trigger something you plan to test — update a plugin, switch a theme — and confirm production is untouched.
Pushing changes back without breaking live
Getting changes from staging to production is where staging workflows go wrong, because files and the database behave differently. Theme files, plugin files and code changes are safe to copy: they are the same on both sides. The database is not. On any site with live activity — comments, orders, form submissions, new users — the live database has moved on since you cloned it. Overwriting it with the staging database would erase everything that happened in between.
The safe rules are simple. If your change is code, a theme setting stored in a file, or a plugin update, deploy the files (or repeat the update on live) and leave the database alone. If your change is content — a new page, a rewritten landing page — recreate it on live, or export just that content with WordPress’s built-in exporter and import it. Only push a whole staging database over live when the site has no activity that matters during the testing window, and only after a fresh backup of live. Host staging tools and migration plugins handle this push for you with file/database toggles; if you are doing it by hand, the toggle is your own discipline.
6 mistakes that ruin a WordPress staging site
- Sharing the live database. One bad query on staging becomes a production outage. Separate database, separate user, always.
- Skipping the search-replace. A staging site that still links to production will send your test clicks — and your test form submissions — to the live site.
- Using raw SQL REPLACE() on the database. It corrupts serialized data. Use
wp search-replaceor a plugin that understands serialization. - Leaving staging open to Google. A duplicate of your whole site will compete with your real pages in search results, and customers may land on the copy and try to order from it.
- Forgetting outgoing email. Staging sends real mail to real users unless you stop it. Check this before you run any test that triggers notifications.
- Testing on a stale clone. A staging copy from three months ago does not prove anything about today’s site. Refresh it before any serious test — re-run the export, import and search-replace steps — and delete it when you are done so it does not become an unmaintained, unpatched copy of your site sitting on a public subdomain.
When to refresh or delete staging
Refresh the clone before every significant test: export production, import into staging, re-run the search-replace, and re-apply the lockdown settings (a fresh import overwrites blog_public and your mu-plugin survives because it is a file, not a database row — but verify both). When testing is done, delete the staging database, remove the directory and the Nginx server block, and remove the DNS record. A forgotten staging site is a security liability: it runs your plugins and themes but nobody is updating it.
That is the whole workflow. It is a handful of commands, it costs nothing, and it turns “I hope this update doesn’t break the site” into “I already know it doesn’t.” Build the staging copy once, and testing updates stops being scary.
Further Reading & References
- wp search-replace — WP-CLI Command (Developer.WordPress.org)
- Editing wp-config.php — Advanced Administration Handbook (Developer.WordPress.org)
- How to Create a WordPress Staging Site (4 Ways) — How-To WP
- How to Set Up a Staging Site for Your WordPress Website — Nexcess Documentation
- WordPress Staging on Shared Hosting — DCHost
- Before You Switch PHP Versions on a WordPress Site — dev.to
- Create a Staging Site in WordPress — WordPress.com Support
- How to Set Up Automated VPS Backups — HowToShipIt (take the backup this guide assumes you already have)
- WordPress Debug.log: How to Enable It and Read What It Tells You — HowToShipIt
Facts verified 30 October 2026 against the official WordPress developer documentation and the sources linked above.



