Using a basic staging environment with Magento 2

in Magento

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

Your staging environment shares resources (disk, CPU, mem) with your production site. If you want to do things such as automated load tests, it is recommended to order a development package 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 && mkdir /data/web/staging
ln -fs /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 change at least 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 and import into the staging database

DUMP=/data/web/magento2/$( magerun2 db:dump --print-only-filename  --root-dir=/data/web/magento2 db:dump --strip="@stripped"  2>/dev/null )
magerun2 db:import $DUMP --root-dir=/data/web/magento2_staging && rm $DUMP

This requires enough space to store a database dump on the filesystem.

Step six: Change the base URL’s of your staging environment

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.

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

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

This implies you use SSL for your Hypernode. If you don’t, order an SSL certificate or use Lets Encrypt

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 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:

  • 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
  • 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

To cleanup or uninstall your Magento installation use the following magerun2 command:

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.

2