How to Set Up Staging Environment in Magento 2
Magento developers may be tempted to launch their online store, set up the initial configuration, and get going!
However, it is very much likely that you may face some error, install a poor quality extension, or something may go wrong that will disrupt the store functionality.
Therefore, it is essential to set up staging environment in Magento 2.
It is like playing with fire for a Magento developer to perform tasks such as upgrade, migrate, or customize in live store rather than in the staging environment.
The Staging environment provides a near-production environment that includes a database, web server, and all the services.
You can use the staging area to see if, say, the upgrade process will go smoothly or not! It allows using both the front and backend of your site to see if everything is running properly.
Check the whole staging environment after any modification, upgrading to latest Magento 2 version, or installing a Magento extension. If everything works properly in the staging area of a primary store, then follow the same in your live site.
Now that you know how much necessary it is to set up staging environment in Magento 2, here’s the exact method for the same.
Just follow the below steps, and your staging area will be ready to execute.
Steps to Set Up Staging Environment in Magento 2:
There are four main basic steps to set up the staging environment of your primary store.
- Create Staging Sub Domain or Create Subfolder Under Your root Directory
- Create Staging Database
- Copy Production Store
- Update Configuration in Database
Create Staging Sub Domain or Create Subfolder Under Your root Directory
Setup a subdomain
Login to your hosting server(i.e., cPanel) for your domain, create a subdomain or create the subfolder under your root directory.
Create Staging Database
Setup a blank database
- While we’re still in the hosting server, we first have to set up a blank database for our staging store:
- Enter staging for the new database.
- For the Username, we’ll enter staging too.
- Enter whatever you’d like in the Password field.
- Assign ALL PRIVILEGES.
Copy Production Store
Copy our production store over to the sub domain directory
- Copy the main store’s Folders and Files.
- For example :
1zip -r mtbackup21012021.zip app/ bin/ dev/ downloads/ generated/ info/ lib/ phpserver/ setup/ update/ vendor/
- For example :
- Next, we need to copy the data from the Main Site’s database to our staging database. We’ll do this from SSH as well.
-
1mysqldump -u [database_user] -p [database_name] > backup_livestore.sql
-
- Now, you’ll be prompted to enter the password for the user with privileges to the Main Site database, so type that in the password field. Make sure to set up a strong password.
- Press the Enter key.
- It can take a couple of seconds or a couple of minutes, depending on your database’s size.
- Just wait until it finishes.
- Once it finishes, we need to import this data to our staging database:
-
1mysql -p -u STAGING_DBUSER STAGING_DBNAME < backup_livestore.sql
-
- You’ll be prompted to enter a password; this time, it’s for the user who has privileges for the staging database.
Update Configuration in Database
Update the Unsecure and Secure URL for our staging store:
Pass the below commands:
1 |
SELECT * FROM `core_config_data` WHERE `path` LIKE '%base_url%'; |
1 |
UPDATE core_config_data SET value = 'staging_url' WHERE path LIKE 'web/unsecure/base_url'; |
1 |
UPDATE core_config_data SET value = 'staging_url' WHERE path LIKE 'web/secure/base_url'; |
Here you go, once you set up the staging area, you can modify code, content as per your requirement without the fear of stopping the live site.
Additionally, upgrading a Magento store is one of the most complex tasks of Magento. So, use the above steps to set up the staging environment and then upgrade your store safely.
Still, if you find any difficulty while upgrading your store, Meetanshi’s experienced and certified developers offer professional Magento Upgrade Service to upgrade your store to the Magento 2.4.6 without any data loss and zero downtime. You may also love to read our blog post on setting up store information in Magento 2.
Done!
If you find any difficulty while setting up the staging area of your Magento store, do share that with us.
I’ll be happy to help you out.
Thank you.
6 Comments
HI, I followed all the steps, but when I visit the stage url, it redirect on live site url. How can I solve it?
Hii Giulio, Firstly make sure that you update you stag site URL in core_config_data table as mentioned in blog,
and flush the Magento cache and Try Again, Thank You.
Hi Kishor,
does it work for Magento 2.4.x as well?
When do I have to repeat these steps – what is useful?
If i repeat it, the database has to be empty – the files too?
Thanks
Hello Nukleuz,
Yes, it will work with 2.4.x as well.
All the steps would remain same.
The staging database needs to be empty and files are going to be pasted
Thank You
Hi Kishor,
why you don’t copy the PUB folfer too? Is it irrelevant? thanks
Hello Claudio,
If you want to copy all the images in the staging site then you need to copy the pub/media folder.
Otherwise it will regenerate while upgrading pub/static or else you can copy the whole pub folder too.
Thank You