How can we help you today?

How to Set Up a Basic Staging Environment for Magento 2

Created by: Rens Zuurbier

Modified on: Wed, 17 Jun, 2020 at 4:07 PM


This article is an extension to the article for Magento 1. For information about the capabilities of the staging environment, please read it first.

Your staging environment shares resources(disk, CPU, memory) with your production site. If you want to do things such as automated load tests, it is recommended to order a development plan instead, so your production site will not be affected.


How to Make a Copy of a Live Site

Setup your staging environment by following the steps.

It is strongly recommended to use separate databases for your staging environment, so using the staging environment will not interfere with the production data.

Step One: Copy Your Magento2 Directory to a Staging Content Directory

rsync -va --delete --delete-excluded \
--exclude /var/report/ \
--exclude /var/session/ \
--exclude /var/log/ \
--exclude \*.sql \
--exclude \*.zip \
/data/web/magento2/ /data/web/magento2_staging/

Step Two: Recreate Symlinks for the Staging Public Directory
cd ~
rm -rf /data/web/staging && ln -s /data/web/magento2_staging/pub /data/web/staging

Step Three: Create a Staging Database

mysql -e 'create database if not exists staging'

Step Four: Edit Your di.xml and env.php to Use MySQL and Redis Databases Separate From the Production Databases

sensible-editor /data/web/magento2_staging/app/etc/env.php

Now at least change  your MySQL database to staging and your Redis databases for cache and FPC to another one that is not in use by the production. (for example: 3 and 4)

Step Five: Dump the Production Database Into the Staging Database

>magerun2 --root-dir=/data/web/magento2 db:dump --strip="@stripped" --stdout | mysql staging

Step Six: Change the Base URL's of Your Staging Environment

Warning! Changing the base URL's before changing your MySQL database to your staging database will have impact on your production site.

Now to use the staging environment, we need to change the base URL's to use the ports for the staging environment. This way your site is accessible through the same URL's as the production shop but will be using different ports. We change the HTTP port to 8888 and the HTTPS port to 8443.

**Please note the we often see conflicts when the unsecure and the secure Base-URLs are not the same. So either set both Base-URLs to HTTP -> 8888 or both URLs to HTTPS -> 8443. For example:

| id | code    | unsecure_baseurl                       | secure_baseurl                         |
+----+---------+----------------------------------------+----------------------------------------+
| 1 | default | http://APPNAME.hypernode.io:8888/ | http://APPNAME.hypernode.io:8888/

OR:

| 1  | default | https://APPNAME.hypernode.io:8443/      | https:////APPNAME.hypernode.io:8443/

Use the following command to change the ports of your staging shop, but use your own domains. You can do this for a single storefront or for all of them.
This way, if your live shop is https://www.example.com/, your staging environment will be accessible on https://www.example.com:8443/

Change the Base URL's for a Single Storefront Using Magerun

cd /data/web/staging
export SHOPHOST="mynode.hypernode.io"
magerun2 --root-dir=/data/web/magento2_staging config:store:set web/unsecure/base_url http://$SHOPHOST:8888/
magerun2 --root-dir=/data/web/magento2_staging config:store:set web/secure/base_url https://$SHOPHOST:8443/
magerun2 --root-dir=/data/web/magento2_staging cache:flush

Change the Base URL's for All Storefronts Using a Script

If you have many storefronts, that all should be changed it's easier to use our script to change the base URL's of your staging environment.

To use this script:

wget https://gist.githubusercontent.com/hn-support/d7a6fdd89bd78ebd7a03982605743616/raw/77837096d4a325b3f863609cd8282531faf0fe4a/change_magento2_staging_baseurls.py
python change_magento2_staging_baseurls.py

Change the Base URLS' for All Your Storefronts Via MySQL

Another easy way to change the base URLs of all your stores is by running the following database queries:

A big thank you to our partner [Experius](https://www.experius.nl/) for providing these.

Manually Change the Base URL's of Your Storefronts

If you want to set the base URL's manually, check our documentation on changing your base URL's for Magento 1. Do you have a Magento 2 shop, please check this article on changing base URL's.

Step Seven: Change All References in Your Staging Directory

If your media directory is stored in /data/web/staging, make sure first not to overwrite this folder with a symlink

Keep in mind:

  • Re-deploy your static files (clean out pub/static, and deploy your static files]
  • Find symlinks: `find /data/web/magento2_staging -type l -exec ls -lsa {} \;`
  • Grep for /data/web/public in your staging directory: grep -R '/data/web/public' /data/web/staging
  • Grep for /data/web/magento2 in your staging directory: grep -R '/data/web/magento2' /data/web/{staging,magento2_staging}
  • If you use Git submodules keep in mind that you should change the hardcoded paths and symlinks from /data/web/public to /data/web/staging to avoid collisions between the staging and the production environment.
  • Fix symlinks in your .modman dir that are still pointing to /data/web/public
  • Regenerate your sitemap as it may still contain links to your live site.
  • Change all custom links and references that are in your staging installation pointing to the production install.

Now you should be able to reach your Magento 2 staging environment on http://mynode.hypernode.io:8888

For additional configuration and troubleshooting refer to the Magento 1 staging environment article

Cleanup and Refresh

If you created a staging environment before on the same machine, you might want to do a resync of the content and the database or remove all staging components

Cleanup

magerun2 --root-dir=/data/web/magento2_staging uninstall --installationFolder=/data/web/magento2_staging --force

This will remove your Magento installation and drops the database as well.

Refresh

To refresh your installation, run all steps again.
This will resync your files and dumps the production database into the staging.
All steps are the same, making it easy to recreate your environment.

R
Rens is the author of this solution article.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.