Magento migration: move your shop to a new server

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

1.2.3.4  yourdomain.com

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.

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!

Andrey Tataranovich

Andrey Tataranovich

Andrei believes that only at work people can fully express themselves, so he does his best to implement all his knowledge while in the office. Having made computer science his hobby, Andrei is constantly developing his professional skills. But despite such a busy schedule he always finds time to read science fiction books and communicate with new interesting people.

You may also like...

10 Responses

  1. Anastasia says:

    Hello,

    I followed your steps and moved my Magento eshop to new server.

    Via the hosts file everything is working correctly , except that when I add a product to cart every page is a blank page.

    If I clear the cache, everything is back to normal. Have you ever encountered this problem?

    Regards,
    Anastasia

    • Ksenia Dobreva Ksenia Dobreva says:

      Hi Anastasia,

      to know what exactly causes a blank page you need to check your server and Magento logs and to look for errors – you’ll have more information then.

      • Anastasia says:

        Hello Ksenia,
        I checked the logs but nothing it doesn’t show anything regarding this error.

        Also another interesting thing.. I checked my eshop through this https://hosts.cx/

        and everything is working correctly apart from the following:
        I add a product to the cart, product added successfully to cart. As long as I stay on the same page I can see my cart that has one product. When I navigate to another page the cart is empty.
        Also no errors is written either in the logs or in the console of the browser.

  2. Billy Ross says:

    Hi,
    I have moved my Magento shop to a new server before few days ago by following your procedure which you have mentioned above and i want to say that everything is working really well.
    So, want to say thanks to you for this useful information and please keep it up.

  3. Peter Maskolin says:

    Hello
    I have finally move the my magento eshop for new server and everything is working correctly,
    Thanks

  4. Robert Williams says:

    Great article Ksenia.

    I successfully migrated my Magento website using your tips for which I can’t thank you enough.

    I won’t have to bother Rosehosting about migration now.

    Cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *