How to move your Magento store to a new server

Apr 20, 2022 Val Kelmuts 7 min read

Reliable hosting and well-configured servers are essential to optimal website performance. In an ideal world, you might choose a Magento hosting provider and never have to worry about your website hosting.

But the reality is that not every hosting provider offers the level of service or resources suitable for your business. Even if you choose the right hosting provider, as your business grows, you might need servers or resources beyond the capabilities of your current provider.

Once you decide to move Magento to a new server, you must remember that changing hosts and migrating servers are risky. Downtime and data loss are two of the most significant risks of moving your store to a new host.

In this article, we’ll show you how to move your Magento store to a new server while mitigating possible risks.

Move your Magento 2 store to a new server in 5 easy steps

If you’re switching to a managed hosting provider, chances are they’ll offer a free migration service with your hosting subscription. But if you’re migrating to a DIY provider like DigitalOcean or Amazon Web Services (AWS), you’ll need to do everything yourself.

Note:

Before you begin, make sure you take a complete backup of your website. If you’re using a managed hosting service, you can use the control panel to back up your store manually or reach out to your provider’s technical support team for assistance. Alternatively, if you’re using a provider such as DigitalOcean or AWS, you can take a snapshot of your server.

Step 1: Create a backup of the Magento database

During the server migration, you need to preserve and transfer two essential aspects of your Magento store: the database and the filesystem.

The Magento database contains information about your store, such as customers, products, orders, and store settings. As for the filesystem, it contains all product media, extension code, and the Magento codebase.

We’ll be transferring the contents of the Magento filesystem using rsync, a file synchronization tool in Linux. However, as the Magento database is stored in a separate location, we’ll create a backup of the database in the Magento filesystem to help us transfer everything to the new server using a single command.

If you’re using a Magento version under 2.3.0, you can create a database backup from within the Magento admin panel by following Adobe’s guide for Magento backups or via CLI. For more recent versions of Magento, you’ll only have to use the command line to back up your store database.

Here’s how you can create a complete backup of your Magento database via the command-line interface using the mysqldump utility:

  • 1. Access your server over SSH and switch to the Magento filesystem owner.
  • 2.Navigate to the Magento filesystem root by modifying and entering the following command in the terminal:

<code>

$ cd <path-to-magento-root>

</code>

  • 3. Verify you’re in the right location by running the following command to list all the directory contents:

<code>

$ ls -la

</code>

You should see a list of all the files and folders in the Magento root directory, such as app/, pub/, var/, vendor/.

  • 4. Once you’ve verified that you’re in the right place, you can use the mysqldump tool to create a complete backup of the Magento database by running the following command:

<code>

$ mysqldump -u magento-db-user -p magento-db-name > var/backups/magentobackup.sql

</code>

Make sure you replace the value for magento-db-user and magento-db-name with the appropriate values for your store. Also, make sure that the /var/backups folder exists in your file system. Otherwise, the command will return an error.

After executing the command, you’ll need to enter the Magento database user’s password. Once you’ve entered the password and hit “Enter,” you won’t see anything until after the database backup is complete.

On average, you can expect the backup to take around 30 seconds to 1 minute per gigabyte. So, if you’re migrating a large store database, expect to wait a while before the backup completes.

You won’t see a confirmation message once the backup is complete. Instead, the terminal will resume to the standard prompt screen.

  • 5. Verify the contents of the var/backups directory by running the following command:

<code>

$ ls -la var/backups

</code>

That’s it; you’ve successfully backed up the Magento database inside the Magento filesystem.

Before moving further, put your store in maintenance mode. This step will prevent data inconsistencies during the file migration.

You can enable maintenance mode by navigating to the Magento root directory and executing the following command as the Magento filesystem owner:

<code>

$ php bin/magento maintenance:enable

</code>

Step 2: Prepare the destination server

Now it’s time to prepare your destination server by installing the extensions Magento requires for its operations. It’s important to remember that you should ensure each extension version is compatible with your version of Magento. You can reference Adobe’s system requirements and compatible software for Magento versions 2.3.0 and above before proceeding.

You should also ensure your server has the resources necessary to operate a CPU-bound application like Magento. Explore our guide detailing the hardware requirements of Magento or get in touch with us here if you need help setting up a new server.

Here are a few points to keep in mind when you’re preparing your new server:

  • 1. Use the same name for the Magento filesystem owner on the destination server as the origin server.
  • 2. Use matching database credentials on the origin and destination server.
  • 3. Use matching credentials for any other tools such as Varnish or Redis on both servers.

Once you’ve prepared your new server, make sure you empty the destination Magento root folder by executing the following commands as a superuser or the directory owner:

<code>

$ rm -r /var/www/public_html/*

</code>

Verify the directory is empty by listing its contents using the following command:

<code>

$ ls -la /var/www/public_html

</code>

Once you’ve verified it’s empty, you can move on to the next step and migrate your store data to the new server.

Note:

Make sure you disable any firewall rules on your origin and destination servers that may restrict the remote file transfer before proceeding. Alternatively, you can add IP whitelisting rules to allow data transfers between the two servers.

Step 3: Migrate the data

Now, it’s time to migrate your store data over to the new server. We’ll use rsync because of its performance advantages and lower bandwidth consumption.

Log into the destination server as the filesystem owner and run the following command to begin copying from the origin server over SSH:

<code>

$ rsync -avzh ssh <username>@<origin-server-ip>:/var/www/html/ /var/www/public_html/

</code>

Command explained:

This command can be broken into 7 components as follows:

  • 1. rsync: This initiates the rsync tool.
  • 2. -avzh: These are flags that alter the execution of the rsync command.
  • 3. ssh: This specifies that the data transfer must use the SSH protocol.
  • 4. <username>: This is the username of the filesystem owner on the origin server.
  • 5. <origin-server-ip>: This is the IP address of the origin server.
  • 6. :/var/www/html/: This points to the location of the directory on the origin server. It’s worth noting that the trailing “/” is important here. It specifies that you want to copy the contents of the folder and not the folder itself.
  • 7. /var/www/public_html/: This specifies the location on the destination server where you wish to copy all the files.

When you execute the above command, you’ll need to enter a password for the <username> user on the origin server. This is needed if you connect through SSH with the password login, but not with the key. Once the transfer begins, you’ll see a verbose output in your terminal screen as it progresses. The entire file transfer process can take a few minutes to complete depending on the size of your store data.

Once it’s complete, you can verify if all files have been copied by running the following command in the destination directory:

<code>

$ ls -la

</code>

Once you’ve verified all the files and folders match the origin directory, you can move on to the next step.

Step 4: Configure your new store

When we used the rsync command to transfer the files over to the new server, we preserved all permissions and ownerships as per the origin server. If the filesystem owner on your new server has the same name as the one on the origin server, you won’t need to reassign ownership.

However, you should reassign file permissions to ensure the rest of the setup goes through smoothly. Execute the following command as the filesystem owner in the Magento root folder:

<code>

find . -type f -exec chmod 644 {} \;

find . -type d -exec chmod 755 {} \;

find ./var -type d -exec chmod 777 {} \;

find ./pub/media -type d -exec chmod 777 {} \;

find ./pub/static -type d -exec chmod 777 {} \;

chmod 644 ./app/etc/*.xml

chmod 777 ./app/etc

chmod 777 var/log

chmod 777 generated

</code>

Next, we’ll restore our backed up database from the Magento filesystem. Run the following command in the terminal:

<code>

$ mysql -u magento-db-user -p magento-db < var/backups/magentobackup.sql

/<code>

Like when we ran the original database dump, this one may take a few minutes to execute. The screen will resume to the standard prompt screen once it finishes. After that, you’re ready to go live.

Step 5: Go live

If you’ve followed everything correctly up to this point, the next few steps will easily carry you over the finish line. Before you can access the website in a browser, you need to update the DNS records of your domain and point it to the new server IP address.

After you’ve updated the DNS records with your registrar, it can take anywhere from a couple of minutes to a few hours to propagate, depending on your TTL (Time-To-Live) settings. In the meantime, let’s tie up a few loose ends before you start using your website.

First, you need to install cron to allow Magento to perform vital background tasks. Execute the following command as the Magento filesystem owner in the Magento root directory to install cron:

<code>

$ php bin/magento cron:install

/<code>

Next, switch your store into production mode, so it clears caches, recompiles data, and disables maintenance mode. You can do it by executing the following command:

<code>

$ php bin/magento deploy:mode:set production

/<code>

Once it executes, you should be able to access your store in a browser. You might notice the website is a bit slow when you load it the first time. This issue should be resolved once the cache generates after the first load.

Closing thoughts

We hope our tutorial on moving a Magento store to a new server helped you understand the basics of migrating your website between hosts. Make sure you follow each step carefully so that you don’t run into any problems.

And if you’d like assistance with migrating your store or optimizing its performance, reach out to us for a quote!

About the author
Val Kelmuts
CEO & Co-Founder @ Staylime

Val Kelmuts is the Chief Executive Officer and Co-Founder at Staylime, an ecommerce design and development company headquartered in Redwood City, California. Val has 10 years of experience in custom software development, sales management, and business development. He is the Adobe Commerce sales accredited specialist, Shopify business certified expert, and PMI member.

Adobe Commerce sales accredited specialist
Shopify business certified expert
PMI member