How to Set Up a Staging Copy of WordPress on the Same VPS

A staging copy of WordPress is a private, working duplicate of your live site where you can test updates, try a new theme, or fix a broken plugin without risking the site your visitors see. When that copy lives on the same VPS as the live site, you reuse the server, PHP version, and Nginx setup you already run, so what you test behaves much like production. This guide covers the full setup: a subdomain, a separate folder, a separate database, and a search-and-replace pass that rewires every internal link to the staging address.

If you want the broader cloning workflow first, read our guide on how to clone a WordPress site for testing without touching the live one. The steps below follow the same idea, tuned for a permanent staging copy you refresh from live regularly.

Plan your staging copy of WordPress before you touch the live site

Pick a subdomain nobody will confuse with the live site, such as staging.example.com. It keeps every link, redirect, and cookie separate from production. A WordPress staging subdomain also makes the Nginx setup simple, because each site gets its own server block and document root.

Decide the four names up front: the subdomain, the folder, the database, and the database user. A clear pattern is staging.example.com, /var/www/staging.example.com, staging_wp, and staging_user.

Check that the server can carry two copies of the site. Run df -h and free -h first. Uploads and databases double in size, so clean up old backups and unused uploads before you start if disk space is tight.

Step 1: Copy the files to a staging folder

Copy the live files with rsync. It preserves permissions and timestamps, and you can run it again later to refresh the copy.

sudo rsync -a /var/www/example.com/ /var/www/staging.example.com/ --exclude 'wp-config.php'

Excluding wp-config.php matters. That file holds the live database credentials, and the staging copy needs its own. After the copy finishes, fix the ownership so the web server can read and write the new folder:

sudo chown -R www-data:www-data /var/www/staging.example.com
sudo find /var/www/staging.example.com -type d -exec chmod 755 {} ;
sudo find /var/www/staging.example.com -type f -exec chmod 644 {} ;

Step 2: Create a separate staging database and import a copy

Never let staging share the live database. One shared database means a test plugin update on staging can wipe live content, and a test order can land in the live store.

Dump the live database, create a fresh database and user for staging, then import the dump. For the full backup routine, see our guide on how to back up MySQL databases automatically with mysqldump and cron.

mysqldump -u root -p live_wp > /tmp/live-backup.sql
mysql -u root -p -e "CREATE DATABASE staging_wp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql -u root -p -e "CREATE USER 'staging_user'@'localhost' IDENTIFIED BY 'a-long-random-password';"
mysql -u root -p -e "GRANT ALL PRIVILEGES ON staging_wp.* TO 'staging_user'@'localhost'; FLUSH PRIVILEGES;"
mysql -u root -p staging_wp < /tmp/live-backup.sql
rm /tmp/live-backup.sql

Use a long random password for the staging user, and delete the dump file from /tmp as soon as the import finishes.

Step 3: Point the staging copy at its own database

Copy the live wp-config.php into the staging folder, then edit only the database lines:

define( 'DB_NAME', 'staging_wp' );
define( 'DB_USER', 'staging_user' );
define( 'DB_PASSWORD', 'a-long-random-password' );
define( 'DB_HOST', 'localhost' );

Generate fresh salts for the staging copy. Fresh salts log everyone out of staging, which is what you want, since the imported database still holds live login sessions. Fetch a new set from the WordPress salt generator and paste it over the old block.

Add these lines to mark the copy as staging and keep errors in a log:

define( 'WP_ENVIRONMENT_TYPE', 'staging' );
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );

Logging errors to wp-content/debug.log instead of showing them on screen gives you a record of what broke during a test.

Step 4: Replace the live URL with the staging URL

The imported database still points at the live domain in post content, options, menus, and widget settings. A WP-CLI search-replace pass rewires all of it in one command. If you have not used the tool before, read how to use WP-CLI to manage WordPress faster from the terminal first.

cd /var/www/staging.example.com
wp search-replace 'https://example.com' 'https://staging.example.com' --all-tables --precise --recurse-objects
wp cache flush
wp rewrite flush

Run the replacement before you open the staging site in a browser. If you log in first, WordPress may redirect you back to the live domain and set cookies for the wrong site.

Step 5: Add an Nginx server block and HTTPS

Create a server block for the subdomain. An Nginx staging server block looks almost identical to the live one, with the server name and root path changed. See our walkthrough of how to set up Nginx server blocks on Ubuntu 24.04.

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;
    }
}

Match the PHP-FPM socket to the version the live site uses. A staging copy on a different PHP version can hide real errors, or invent ones that never happen in production. Enable the site, test the config, and add HTTPS with Certbot:

sudo ln -s /etc/nginx/sites-available/staging.example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo certbot --nginx -d staging.example.com
sudo systemctl reload nginx

If you already run a wildcard certificate for the domain, Certbot can reuse it instead of issuing a new one. Confirm the staging site loads over HTTPS before you hand the address to anyone.

Lock the staging site down

A staging copy of WordPress should never show up in search results or send mail to real people.

  • Block search engines. In the staging dashboard, go to Settings, then Reading, and tick “Discourage search engines from indexing this site”. Add a noindex header in the Nginx server block as a second layer.
  • Add HTTP basic auth. A username and password prompt in front of the whole subdomain keeps casual visitors and bots out. Generate the password file with htpasswd, then add auth_basic and auth_basic_user_file to the staging server block and reload Nginx.
  • Stop outgoing mail. Staging holds a copy of your real users and customers. A test password reset or order email must never reach them, so point staging mail at a logging plugin or a dead mailbox until you have verified the mail path yourself.

For more on the subdomain side of this setup, see our guide to a WordPress staging site on a VPS subdomain.

Test your changes, then push them live

Update one plugin at a time, check the front page and the admin area after each update, and read wp-content/debug.log when something looks off. Test the exact flows your visitors use: log in, submit a form, and add a product to the cart.

Pushing changes back to live depends on what changed:

What changed How to push it live Watch out for
Plugin or theme files Copy the updated files from staging to live with rsync Keep the same plugin versions on both copies
Theme settings or menus Export the changed options, or redo the change on live A full database import loses live content added since the copy
Posts or pages Use the WordPress exporter, or recreate the edit on live Media attached to the post needs its files copied too
Database structure Run the same update on live and let it migrate its own tables Never import the whole staging database over live

The rule behind the table is simple. Files move freely from staging to live, but the database only moves in pieces. A full staging database import wipes every order, comment, and post published on live since you made the copy.

Common mistakes that break a staging copy

  • Sharing one database. Staging and live must each have their own database and user. Shared tables turn every staging test into a live change.
  • Skipping the search-replace. A staging site that still points at the live domain will redirect visitors and mix up cookies.
  • Leaving staging indexed. Search engines can index the copy and split your ranking between two identical sites. Basic auth plus noindex closes that door.
  • Letting staging send real email. Test password resets and order confirmations go to real addresses unless you redirect staging mail first.
  • Forgetting to refresh the copy. A staging copy from three months ago tests against old content and old plugin versions. Refresh the files and database from live before any big test, then run the search-replace pass again.

A staging copy of WordPress on the same VPS costs no extra server and takes under an hour to build. Keep it locked down, refresh it before big changes, and test the real visitor flows on it. The next plugin update will still carry some risk, but that risk will land on a copy you can rebuild, not on the site your visitors use.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.