A big plugin update is queued. Your production site is taking orders right now. If the update breaks checkout, your customers find the bug before you do.
A staging server removes that risk. It is a private copy of your site that runs on the same VPS. You break things there first. Then you push only the changes that work.
This guide covers the full build on Ubuntu, Nginx, and MariaDB. WordPress is the worked example, but most PHP sites follow the same steps. You’ll need SSH access and sudo rights. Plan on about an hour.
Table of Contents
What a VPS Staging Server Must Match
A staging server is a private, production-like copy of your site. You use it to test changes before release. Real visitors never see it.
The copy only helps if it behaves like production. Therefore, match these four things:
- PHP version and extensions
- Database engine and version
- Web server configuration
- Caching rules and server modules, such as Redis
Data, traffic, and credentials can differ. In fact, they should. Staging gets sanitized data, no public visitors, and its own passwords.
A mismatch gives you false confidence. For example, a plugin may pass PHP 8.3 in staging but fail PHP 8.1 in production. The test looked green. The release still broke.
Same VPS or a Separate VPS for Staging?
Pick your layout before you build anything. Each choice has trade-offs.
| Factor | Same VPS | Separate VPS |
|---|---|---|
| Cost | No extra bill | Adds a second monthly plan |
| Fidelity | Shares PHP, database, and OS with production | Must be built to match |
| Risk to production | Shares CPU and RAM | Fully isolated |
| Server-level testing | Poor, since you’d change the live stack | Strong, so that you can break anything |
| Best for | Theme, plugin, and content changes | OS, PHP, or web server upgrades |
Use the same VPS for theme, plugin, and content changes. It costs nothing extra and matches production exactly.
However, test PHP upgrades, OS updates, and web server changes on a second server. Those changes touch the shared stack. A mistake would hit production too.
This guide builds the same-server version.
Prerequisites: Access, Resources, and a Fresh Backup

First, gather what you need:
- SSH access with sudo rights
- A working production site on Nginx and PHP-FPM
- Access to your DNS records
- WP-CLI installed on the server
Next, check your free headroom. Staging ne eds spare disk space for a full copy of your files and database. It also needs spare RAM for a second PHP pool.
free -h
df -h
uptime
If memory is already tight, upgrade the plan before you continue. Otherwise, staging will slow down your live site.
Then take a full production backup. Do this even if you already run nightly backups.
mkdir -p ~/backups
sudo mysqldump --single-transaction example_prod | gzip > ~/backups/prod-$(date +%F).sql.gz
sudo tar -czf ~/backups/files-$(date +%F).tar.gz /var/www/example.com/public_html
Stop and read: Every command below uses staging paths. Read each path twice before you press Enter. One wrong path can overwrite production.
Step 1: Point a Staging Subdomain at Your VPS
Create an A record for staging.example.com. Point it at your VPS IP address. Set the TTL low, around 300 seconds. That way, a mistake spreads and heals quickly.
Then verify the record from your server:
dig +short staging.example.com
The output should match your VPS IP; if it doesn’t, wait a few minutes and try again.
If you use Cloudflare, start with the proxy switched off (grey cloud). Certificate validation is easier that way. You can turn the proxy on later.
Also watch your wildcard records. A record like *.example.com resolves every guessed name. Give Nginx a default server block that drops unmatched requests, so nobody reaches staging by accident.
Step 2: Create the Staging Directory, User, and Nginx Server Block

Start with a separate system user and folder. This keeps staging file permissions apart from production.
sudo adduser --system --group --home /var/www/staging.example.com stagingsite
sudo mkdir -p /var/www/staging.example.com/public_html
Next, add a dedicated PHP-FPM pool. This protects production. If a staging script goes wild, it can only burn through its own small worker limit.
Create /etc/php/8.3/fpm/pool.d/staging.conf. Match the PHP version to your production server.
[staging]
user = stagingsite
group = stagingsite
listen = /run/php/php8.3-fpm-staging.sock
listen.owner = www-data
listen.group = www-data
pm = ondemand
pm.max_children = 5
pm.process_idle_timeout = 20s
php_admin_value[memory_limit] = 256M
After that, create the Nginx server block at /etc/nginx/sites-available/staging.example.com:
server {
listen 80;
server_name staging.example.com;
root /var/www/staging.example.com/public_html;
index index.php;
add_header X-Robots-Tag "noindex, nofollow" always;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm-staging.sock;
}
location ~ /\.ht {
deny all;
}
}
Finally, enable the site and test the config before you reload.
sudo ln -s /etc/nginx/sites-available/staging.example.com /etc/nginx/sites-enabled/
sudo systemctl reload php8.3-fpm
sudo nginx -t && sudo systemctl reload nginx
If nginx -t reports an error, fix it first. Never reload on a failed test.

Step 3: Clone Your Production Files and Database to Staging
Now copy the files. The trailing slashes matter here, and the direction matters even more. The source is production. The destination is staging.
sudo rsync -a \
--exclude 'wp-content/cache' \
--exclude 'wp-content/uploads/backup*' \
/var/www/example.com/public_html/ \
/var/www/staging.example.com/public_html/
sudo chown -R stagingsite:stagingsite /var/www/staging.example.com/public_html
If your uploads folder is huge, skip it and symlink or sync a sample. Staging rarely needs every image.
Next, export the production database and create a new one for staging. Give staging its own user. That user should have access only to the staging database.
sudo mysqldump --single-transaction example_prod > /tmp/prod.sql
sudo mysql -e "CREATE DATABASE example_staging CHARACTER SET utf8mb4;"
sudo mysql -e "CREATE USER 'staging_user'@'localhost' IDENTIFIED BY 'use-a-long-random-password';"
sudo mysql -e "GRANT ALL PRIVILEGES ON example_staging.* TO 'staging_user'@'localhost';"
sudo mysql example_staging < /tmp/prod.sql
sudo rm /tmp/prod.sql
Delete the dump file right away. It holds real customer data.
Last, confirm the table prefix and character set match production. A prefix mismatch breaks user roles in WordPress.
Step 4: Configure the Staging Site (wp-config and URL Rewrites)
![sudo -u stagingsite wp option update blogname "[STAGING] Example"
sudo -u stagingsite wp option update blog_public 0
sudo -u stagingsite wp cache flush
sudo -u stagingsite wp rewrite flush](https://truehost.co.za/wp-content/uploads/staging-check-site-options.webp)
Right now, your staging copy still points at the production database. Fix that first, using WP-CLI as the staging user.
cd /var/www/staging.example.com/public_html
sudo -u stagingsite wp config set DB_NAME example_staging
sudo -u stagingsite wp config set DB_USER staging_user
sudo -u stagingsite wp config set DB_PASSWORD 'use-a-long-random-password'
sudo -u stagingsite wp config set WP_ENVIRONMENT_TYPE staging
sudo -u stagingsite wp config set WP_DEBUG true --raw
Then swap the site URLs. Run a dry run first. It shows how many replacements WordPress will make.
sudo -u stagingsite wp search-replace 'https://example.com' 'https://staging.example.com' --all-tables --dry-run
sudo -u stagingsite wp search-replace 'https://example.com' 'https://staging.example.com' --all-tables
After that, label the site so nobody edits the wrong copy. Also discourage search engines at the WordPress level.
sudo -u stagingsite wp option update blogname "[STAGING] Example"
sudo -u stagingsite wp option update blog_public 0
sudo -u stagingsite wp cache flush
sudo -u stagingsite wp rewrite flush
Editing the wrong copy is the most common staging mistake. A visible label costs nothing and prevents it.
Step 5: Lock Staging Down With SSL, Basic Auth, and noindex
An open staging site is a liability. Search engines may index it, and strangers may poke at unfinished code. Lock it down in three layers.
First, issue a free certificate. Each subdomain needs its own, unless you already hold a wildcard certificate.
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d staging.example.com
Second, add HTTP basic auth. Create a password file:
sudo apt install apache2-utils
sudo htpasswd -c /etc/nginx/.htpasswd-staging teamuser
Then add two lines inside the server block for port 443:
auth_basic "Staging";
auth_basic_user_file /etc/nginx/.htpasswd-staging;
Reload Nginx once more. Third, the X-Robots-Tag header from Step 2 and the WordPress setting from Step 4 act as a backup. Auth is the real lock. The noindex signals cover you if someone removes auth later.
Finally, test from outside the server:
curl -I https://staging.example.com
You should see 401 Unauthorized. If you see 200, auth isn’t active yet. Fix that before you do anything else.
Step 6: Disable Payments, Email, and Outbound Integrations

Your staging copy still holds live API keys. As a result, it can charge cards, send emails, and push data into your CRM. Switch all of that off.
Payments. Put every gateway in test mode. Check the settings page of each one, since some store keys in more than one place.
Email. Block outgoing mail with a small must-use plugin:
sudo -u stagingsite mkdir -p wp-content/mu-plugins
echo "<?php add_filter( 'pre_wp_mail', '__return_false' );" | sudo -u stagingsite tee wp-content/mu-plugins/block-mail.php
WordPress now treats every email as failed, and nothing leaves the server. If you want to read the messages instead, install Mailpit as a local mail sink.
Customer data. Replace real emails and clear session tables. Adjust the ID so you keep your own admin login.
sudo -u stagingsite wp db query "UPDATE wp_users SET user_email = CONCAT('user', ID, '@example.invalid') WHERE ID > 1;"
sudo -u stagingsite wp db query "TRUNCATE TABLE wp_woocommerce_sessions;"
The second query only applies if you run WooCommerce. Likewise, check plugin tables for billing emails and phone numbers.
Cron and webhooks. Scheduled tasks can fire live actions, such as publishing posts or syncing orders. Turn WP-Cron off:
sudo -u stagingsite wp config set DISABLE_WP_CRON true --raw
When a test requires a scheduled task, run it manually with wp cron event run --due-now. Also deactivate any plugin that syncs with shipping, accounting, or CRM tools.
Step 7: Test Your Changes, Then Push Staging to Production
Now the staging server earns its keep. Apply your update there and run through a short checklist:
- Log in as an admin and as a regular user.
- Submit every form and confirm the notification path.
- Run a test checkout with a test card.
- Clear caches and reload the key pages.
- Check the PHP error log for new warnings.
Once everything passes, choose how to deploy. You have two paths.
Deploy code only. Copy the changed theme or plugin folders to production. This is the safest path. Production data stays untouched.
Migrate the database too. Use this only for changes that live in the database, like a site rebuild. However, production continues to collect orders and comments after you clone. Pushing a staging database over it erases that new data.
Here is the code-only sequence for one plugin. It backs up first, then enters maintenance mode.
STAGE=/var/www/staging.example.com/public_html
PROD=/var/www/example.com/public_html
# Back up first
sudo mysqldump --single-transaction example_prod > ~/backups/pre-deploy-$(date +%F-%H%M).sql
sudo tar -czf ~/backups/plugin-$(date +%F-%H%M).tar.gz -C $PROD/wp-content/plugins my-plugin
# Deploy
sudo -u www-data wp --path=$PROD maintenance-mode activate
sudo rsync -a $STAGE/wp-content/plugins/my-plugin/ $PROD/wp-content/plugins/my-plugin/
sudo chown -R www-data:www-data $PROD/wp-content/plugins/my-plugin
sudo -u www-data wp --path=$PROD cache flush
sudo -u www-data wp --path=$PROD maintenance-mode deactivate
Then run a quick smoke test on the live site. Load the home page, log in, and place a test order if you can.
Never rsync the whole staging tree over production. That would copy wp-config.php and your staging database credentials. The site would break at once.
If something goes wrong, roll back right away:
sudo tar -xzf ~/backups/plugin-DATE.tar.gz -C $PROD/wp-content/plugins
sudo mysql example_prod < ~/backups/pre-deploy-DATE.sql
Restore the database only if your deploy changed data. A restore discards orders placed since the backup. For that reason, keep the deploy window short.
Keeping Your Staging Server Healthy
A neglected staging server drifts away from production. Then it stops catching bugs. Build a few habits to prevent that.
- Refresh on a schedule. Re-run Steps 3, 4, and 6 before any major test cycle.
- Cap resources. Keep
pm.max_childrenlow and run heavy tests off-peak. - Match versions. Compare
php -vandmysql --versionon both sides after every upgrade. - Rotate credentials. Change the basic auth password when team members leave.
Before each test session, run this safety checklist:
Basic auth is on and returns 401
The noindex header is present
Payments run in test mode
Outgoing mail is blocked or sent to a sink
WP-Cron is off, and webhooks are disabled
Customer data is sanitized
The site title shows a STAGING label
FAQs
How often should I refresh staging from production?
Refresh before every major test cycle. Busy shops may refresh weekly. Quieter sites can refresh monthly. Stale data hides bugs that fresh orders or posts would trigger. After each refresh, repeat the sanitizing steps.
Will running staging slow down my live site?
It can, since both sites share CPU, RAM, and disk. The dedicated pool, with its worker cap, limits the damage. Also run heavy tests off-peak. If staging continues to compete with production, move it to its own VPS.
Can I use this setup for Laravel or Node apps?
Yes, the pattern carries over. You still need a subdomain, a server block, a separate database, auth, and SSL. However, a .env file replaces wp-config.php. A process manager, such as PM2 or systemd, runs a Node app, and Nginx forwards traffic via proxy_pass.
What should I do if staging gets indexed by Google?
Act quickly. First, add auth and the noindex header if either is missing. Next, verify the staging subdomain in Google Search Console and submit a removal request. Finally, check that no public page links to staging.
How do I automate staging deploys?
Script the steps once they feel routine. A Git post-receive hook or a CI job can run the rsync and WP-CLI commands. Keep the backup and smoke-test steps inside the script.
Get Started With Staging Servers
You now have a private copy of your site that can’t hurt production. Use it for every update, even the small ones. Small updates cause surprising breakage too.
Keep one habit above all others. Never deploy without a fresh backup and a tested rollback. Staging catches most problems, but a rollback covers the rest.
A staging server also needs room to breathe. Two PHP pools and two databases consume real memory and disk space. If your current plan feels tight, move to a VPS with spare capacity for both sites.
Ready to run production and staging side by side? View Truehost VPS plans and pick one with spare RAM and disk space for both.
Web Hosting
Windows HostingBuilt for Windows apps and websites – stability, speed and flexibility
Reseller HostingLaunch a hosting business without technical skills or expensive infrastructure
Affiliate ProgramRefer customers and earn commissions from sales across our platform
Domain SearchFind and secure a domain name in seconds with our quick lookup tool
CO ZA Domains
All DomainsExplore domain names from over 324 TLDs globally – all in one place
Free Whois Lookup Tool South Africa
VPS
SSLs



