WordPress Staging Website


There are many methods to create a staging site with software plugin-ins, but I’m going with a more manual approach in this blog. I am keeping the staging site on the same server as my live site to keep the server environments the exact same, not to mention it makes backups a little easier.

Create a staging directory within your root web directory

mkdir /var/www/itsmetommy.com/public_html/staging

Copy all files from your root directory into your staging directory

cp -R /var/www/itsmetommy.com/public_html/ /var/www/itsmetommy.com/public_html/staging

Note: You will get the expected following message.

cp: cannot copy a directory, `staging', into itself, `/var/www/itsmetommy.com/public_html/staging'

Password protect the staging directory

Not only does password protecting your site keep users out, but it also removes your site from search results.

cd into your staging directory.

cd /var/www/itsmetommy.com/public_html/staging

Create .htacess

IMPORTANT: Add a mod_rewrite. Update the RewriteRule line if your sub-folder is not named staging.

  • .htaccess — the .htaccess (hypertext access) file is a directory-level configuration file supported by several web servers, that allows for decentralized management of web server configuration.
vi .htaccess
AuthType Basic 
AuthName "restricted area" 
AuthUserFile /your/root/directory/.htpasswd 
require valid-user

# BEGIN WordPress

RewriteEngine On
RewriteBase /staging/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /staging/index.php [L]

# END WordPress

Create .htpasswd

  • .htpasswd — .htpasswd is a flat-file used to store usernames and password for basic authentication on an Apache HTTP Server.

Replace tommy with your name.

htpasswd -c .htpasswd tommy

Create staging database

Here comes the good part.

You will need to create a new staging database, export your live database, import your exported database into your newly created staging database, then modify the staging database to reflect its new location. Did you get all that? Good. Now lets get to it.

Step 1 – Create a new staging database

mysqladmin -u root -p create itsmetommy_staging

Step 2 – Export your live database

mysqldump -u root -p itsmetommy > itsmetommy.sql

Step 3 – Import your exported database into your newly created staging database

mysql -u root -p itsmetommy_staging < itsmetommy.sql

Step 4 - Modify the staging database to reflect its new location

Login to mysql as root.

mysql -u root -p

Give user access to new staging database.

mysql> GRANT ALL PRIVILEGES ON itsmetommy_staging.* TO 'tommy'@'localhost';

Or if your website and database is on a separate server.

mysql> GRANT ALL PRIVILEGES ON itsmetommy_staging.* TO 'tommy'@'10.176.11.65';

Switch to your staging database.

mysql> use itsmetommy_staging;

View current home and siteurl.

mysql> SELECT * FROM wp_options WHERE option_name = 'home' OR option_name = 'siteurl';
+-----------+-------------+------------------------+----------+
| option_id | option_name | option_value | autoload |
+-----------+-------------+------------------------+----------+
| 37 | home | http://itsmetommy.com/ | yes |
| 1 | siteurl | http://itsmetommy.com | yes |
+-----------+-------------+------------------------+----------+
2 rows in set (0.00 sec)

Update home and siteurl.

mysql> UPDATE wp_options SET option_value = replace(option_value, 'http://itsmetommy.com', 'http://itsmetommy.com/staging') WHERE option_name = 'home' OR option_name = 'siteurl';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0

Confirm the new home and siteurl value.

mysql> SELECT * FROM wp_options WHERE option_name = 'home' OR option_name = 'siteurl';
+-----------+-------------+--------------------------------+----------+
| option_id | option_name | option_value | autoload |
+-----------+-------------+--------------------------------+----------+
| 37 | home | http://itsmetommy.com/staging/ | yes |
| 1 | siteurl | http://itsmetommy.com/staging | yes |
+-----------+-------------+--------------------------------+----------+
2 rows in set (0.00 sec)

We now want to fix the urls of posts and pages.

Update guid.

mysql> UPDATE wp_posts SET guid = replace(guid, 'http://www.itsmetommy.com','http://itsmetommy.com/staging');

If you have linked internally within blog posts or pages with absolute URLs, these links will point to wrong locations after you move the blog location.

Update post_contet.

mysql> UPDATE wp_posts SET post_content = replace(post_content, 'http://www.itsmetommy.com', 'http://itsmetommy.com/staging');

Update meta_value.

mysql> UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.itsmetommy.com','http://itsmetommy.com/staging');

Update your WordPress config file to reflect the new database

vi wp-config.php
define('DB_NAME', 'itsmetommy_staging');

Confirm that everything works by going to http://yourdomain.com/staging.

That's it! Give yourself some credit if you made it this far.