How to create staging environment?
In the previous article we discussed why it is a good idea to have a staging environment. Now it is time to get our hands dirty and create actual environments. Before we start, let’s sum up the requirements for staging environment.
1. Production-like Environment
Make sure your staging environment uses the very same versions of all software in stack. That means the same version of operating system, database, PHP, server app etc. Every app behaves differently in other versions and even under another OS. Your WordPress, plugins and theme version should be the same on staging as on production. This will let you test and diagnose the production bugs precisely and debug them.
Having the same caching rules and policy on all levels in both environments is important too. This can uncover for example problems with caching security wp nonces for forms or other caching-related issues.
2. Non-public Environment
Your staging environment is meant for internal purposes such as debugging, testing, showing changes to a client. You should avoid publishing your staging environment due to many reasons. One of them is duplicate content for Google, shooting your SEO-self in the foot. Best approach is to use Basic Auth username and password, which you can easily share inside your team or with your client, but it will create a non-penetrable membrane for bots and outer world.
Standard shared hosting
If you are on a budget and you use the “standard” hosting solution, creating a staging environment is a bit tricky, but not impossible. Many hosting providers allow you to create another site on the subdomain e.g.
staging.example.com or as a subfolder
example.com/staging. If there is no option in your dashboard to “create a new site”, you should follow these general steps:
- Investigate which type of install is allowed (subdomain vs. subfolder).
- Create a new (S)FTP user to access the root directory of your staging website. This usually is somethink like (
- Setup a new database user and a new database if possible. In the case when both production and staging sites share DB, you need to make sure that staging site uses the different $table_prefix.
- If you are using a subdomain install, point the subdomain to the IP of hosting server. Make sure to create SSL certificate for this subdomain inside hosting panel.
- Export database from your production site. In this step it is important to run a search-replace on all your tables to change URLs of production to the staging ones. Also change the prefixes of tables if applicable.
- Import database with changed URLs and prefixes.
- Copy files from your web root of production site.
wp-config.phpto match all settings (new URL, database credentials, wp salts, table prefix etc.).
- Copy all the files to the root of staging site.
- Edit the
.htpasswdto enable basic auth protection.
- Access your new staging site.
Since your new staging site is on the very same server as your production you won’t need any extra hacks about settings. Just make sure you have set the same memory and PHP timeout limits. Some hosting providers also allow to use other version of PHP/DB engine to be run on your staging site, which is great for testing before you upgrade your production site.
Professional Hosting or Managed Server
Many professional hosting providers or managed server providers allow you to easily create a staging environment out of a dashboard. Just search for “environments” or “staging/testing” inside your dashboard or consult with support. Staging environment is usually a part of your subscription fee so no extra charges should be applied.
Are you managing your server by yourself? Great! As a first improvement, you should start using configuration management tool such as Ansible or Puppet. Do it even if you manage just one server now. You will see how it makes your life easier.
Here at Zeni we use Trellis, which is a collection of Ansible playbooks for WordPress projects. It already comes with preset environments for development, staging and production along with automated deployment and provisioning. You can have your environments automated just in hours.
Do you need help with your staging environment or hosting solution? We provide professional hosting solutions with full automation of deployment and releases based on Ansible and Gitlab CI as well as consultations on how to make your projects easily managed. Reach us out via contact form and we will discuss with you the best option for your project.