Magento mirgation: move your shop to a new server

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. 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 this process in detail, and I really hope it’ll help you avoid some typical mistakes of Magento migration.

Prepare to move your Magento 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.

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

Use a convenient way to transfer the backup files to the new server. We suggest SCP:

scp ~/magento-files.tar.gz ~/magento-db.sql.gz magenew@newhost:

Now you need to unpack the backup copy on the new server:

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

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:

  1. The web server runs scripts on behalf of the user which is the owner of the files (magenew)
  2. The web server runs scripts on behalf of a separate user (www-data)

For variant 1 you need to set the permissions like this:

find /home/magenew/public_html -type f -print0 | xargs -r0 chmod 640
find /home/magenew/public_html -type d -print0 | xargs -r0 chmod 750

Allow the web server to save the files it doesn’t own for the second variant:

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}

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

Where is your new server IP address and 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.

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:

Now upload the database to the new server.

gzip -dc ~/magento-db.sql.gz | mysql -h localhost -u magenew_magento -p magenew_magento

Access permissions may be changed during the synchronization – make sure you check or restore them.
Clear Magento cache:

rm -fr /home/magenew/public_html/var/{cache,full_page_cache,log,report,session}

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!