As time moves on and your business grows, there may be a need to move your Magento store to another server. For example, you need an upgraded server, or you just don’t like the level of service of your hosting company, or you want to extend your platform functionality with infinite scroll and other add-ons and ensure it performs smoothly. Whatever the reasons of Magento migration are, moving a functioning store is not the easiest task and you may face various challenges while performing it.
This article describes how to move Magento 2 to another server in detail, and I really hope it’ll help you avoid some typical mistakes of Magento migration to another server.
Prepare to move your store
Before doing anything, go and check if the new server suits the minimal Magento requirements. It is possible you may need to set up your PHP or install the extensions missing. Also, make sure you have enough disk space on the new server; it should be okay to store the whole shop plus you need some spare space.
Store’s backup and copy
We always warn our clients so they won’t make rush decisions. Remember: always test your changes so they won’t harm your Magento shop or interrupt the cash flow.
Switch the domain to the new server only when you’re absolutely sure the shop is working just fine (and you’ll need to sync the data before the final switch).
We have already checked our new server and made sure it suits all the requirements, and now we are ready to move on!
Create a website and a database for your Magento shop.
After that create a backup copy for files and the database. To make the size of the backup copy smaller, exclude var/cache, var/full_page_cache, var/log, var/report, var/session.
[php]cd /home/mageold/public_html
tar -czf ~/magento-files.tar.gz --exclude=var/cache --exclude=var/full_page_cache --exclude=var/log --exclude=var/report --exclude=var/session .
mysqldump -h localhost -u mageold_magento -p mageold_magento | gzip > ~/magento-db.sql.gz[/php]
Use a convenient way to transfer the backup files to the new server. We suggest SCP:
[php]scp ~/magento-files.tar.gz ~/magento-db.sql.gz magenew@newhost:[/php]
Now you need to unpack the backup copy on the new server:
[php]tar -xzf ~/magento-files.tar.gz -C /home/magenew/public_html
gzip -dc ~/magento-db.sql.gz | mysql -h localhost -u magenew_magento -p magenew_magento[/php]
Install the transferred Magento backup files
Edit app/etc/local.xml and add the new database settings to it. If you need that, change access permissions. There are two variants:
- The web server runs scripts on behalf of the user which is the owner of the files (magenew)
- The web server runs scripts on behalf of a separate user (www-data)
For variant 1 you need to set the permissions like this:
[php]find /home/magenew/public_html -type f -print0 | xargs -r0 chmod 640
find /home/magenew/public_html -type d -print0 | xargs -r0 chmod 750[/php]
Allow the web server to save the files it doesn’t own for the second variant:
[php]find /home/magenew/public_html -type f -print0 | xargs -r0 chmod 644
find /home/magenew/public_html -type d -print0 | xargs -r0 chmod 755
chmod -R a+w /home/magenew/public_html/{app/etc,media,var,includes}[/php]
Test your moved Magento shop
As your domain is still pointing at the old server, add this line to the hosts file (/etc/hosts for unix-like systems and %SYSTEMROOT%\system32\drivers\etc\hosts for Windows-like ones):
[php]1.2.3.4 yourdomain.com[/php]
Where 1.2.3.4 is your new server IP address and yourdomain.com is the domain attached to your Magento shop. Don’t forget to delete this line from the hosts file after the test.
Now it’s time for a big test! Carefully check how the transferred shop is working. Imitate users’ behavior to check if the process of shopping is going smoothly. This is the crucial step of migrating to the new server.
Sync data before full migration
If you didn’t find any errors on the previous step or fixed them all, it’s time to sync your data. Please keep in mind these two important points while completing the process of moving Magento to a new server:
- DNS settings can’t be updated instantly, so switch your domain TTL time for the smallest available beforehand. Don’t start the final stage of migrating before the old TTL time expires.
- While you sync your data, the shop on your old server should be off.
To switch the old store server off, turn cron off and enable maintenance mode at touch /home/mageold/public_html/maintenance.flag.
Now it’s time to sync the data. To speed the process we advise to use rsync.
[php]rsync -avz --delete -exclude=var/cache --exclude=var/full_page_cache --exclude=var/log --exclude=var/report --exclude=var/session --exclude=app/etc/local.xml /home/mageold/public_html/ magenew@newhost:/home/magenew/public_html/
mysqldump --add-drop-table -h localhost -u mageold_magento -p mageold_magento | gzip > ~/magento-db.sql.gz
scp ~/magento-db.sql.gz magenew@newhost:[/php]
Now upload the database to the new server.
[php]gzip -dc ~/magento-db.sql.gz | mysql -h localhost -u magenew_magento -p magenew_magento[/php]
Access permissions may be changed during the synchronization – make sure you check or restore them.
Clear Magento cache:
[php]rm -fr /home/magenew/public_html/var/{cache,full_page_cache,log,report,session}[/php]
Last stage of Magento migration
Almost done! Turn cron tasks on for the new server and change DNS setting so the domain points to the new server. As TLL time expires, traffic will be directed to the new server.
Please note that the old shop must be turned off starting from this point! This backup copy is your so-called insurance you can make use of in case of unexpected circumstances.
Have you ever transferred your Magento store to a new server? Welcome to share your experience in comments. If you're still afraid to switch to a new server yourself, drop us a line, and we'll be happy to help you!