Moving Magento To Another Server

Moving Magento To Another Server


Here is an overview of the steps we’ll take to do this:

  1. Make a MySQL dump file of the database
  2. Archive the media and theme directories
  3. Copy the files to the other server
  4. Install a clean version of Magento
  5. Import the dump file to a blank database
  6. Move our copied data to the correct places

Old Server

The first thing you’ll want to do is login to the server where Magento currently resides. You will need to go to the directory where Magento is installed. For the sake of this article, I’m going to assume your directory structure on both servers looks like this:


The public_html directory is considered the root directory for your domain, so if we had a file called test.html in there, it would be accessible like so:

With that in mind, we need to go to the directory where Magento is installed. In our case, it’s installed in the root of the domain, so once we are logged in we will run the following:

  1. cd public_html

By default, when you login to SSH it will place you in the very root directory that you have access rights to, which is typically one directory above the public_html directory.

We will start by making a folder called backup, so we can place all of the files we want to transfer over to the new server in:

  1. mkdir backup

MySQL Dump

Now we need to create a MySQL dump file of our database:

  1. mysqldump -h DBHOST -u DBUSER -pDBPASS DBNAME > data.sql

You will need to replace the values with the correct information:

  • DBHOST – Database host name, usually just localhost
  • DBUSER – Database user with access rights to the database
  • DBPASS – The password for the database user
  • DBNAME – The name of the database that Magento is using

NOTE: If your password contains special characters or punctuation marks—for example, if your password was Ure+Ha_A?r2anp—you’ll want to use this command instead:

  1. mysqldump -h DBHOST -u DBUSER -p DBNAME > data.sql

After you run this command, it will ask you to enter the database password, and you can safely enter it.

Depending on the size of your database, this can take a couple seconds to a couple minutes. If no errors are reported and you’re left with a new command line, then the dump file was created successfully. We can safely move this to the backup directory we created earlier:

  1. mv data.sql backup/

Media Directory

Now let’s compress the contents of the media directory:

  1. tar -cvf media.tar media/*

And move it over to our backup directory:

  1. mv media.tar backup/

Theme Files

Next, we’re going to copy the theme directory from both the app and skin directories. For the sake of this article, I’m going to assume you have not created a new interface, and that we’re using the default interface instead. We’ll start with the theme in the app directory:

  1. cd app/design/frontend/default/

If you were wondering what I meant by interface, it’s the last directory in the previous command. Most people do not create a new interface, but if you did, you’ll want to change the last directory name with the interface name you’re using.

The directory we’re in now is where all the themes for the default interface reside. There should already be a folder in here called default. I hope you’re not modifying the files in this directory to customize the theme for your website. If you have, you’ll want to rename this directory to something more appropriate so it does not get overwritten when you upgrade Magento.

For the sake of this article, we’re going to assume that our theme is called mytheme, so let’s compress it:

  1. tar -cvf app.tar mytheme/

We need to move this tar file to our backup directory now:

  1. mv app.tar ../../../../backup/

We need to do the same thing for the theme in our skin directory, so let’s change in to that directory:

  1. cd ../../../../skin/frontend/default/

This works the same way as the interface and theme in the app directory. If you’re using another interface, change the last directory name out with your interface name. Just like the app directory, we should have a directory in here called mytheme, so let’s compress that as well:

  1. tar -cvf skin.tar mytheme/

And move it to our backup directory:

  1. mv skin.tar ../../../backup/

Now let’s head back to the main directory:

  1. cd ../../../

Config File

Only thing left is our config file, so let’s copy that over as well:

  1. cp app/etc/local.xml backup/

Alright, so we have all of the data we need!

NOTE: If you want to copy your .htaccess or php.ini (assuming you have one) in to the backup directory, now is the time to do it. You don’t need the .htaccess or php.ini file unless you’ve made changes to them. However, if you do want to move them over, run the following command:

  1. cp .htaccess php.ini backup/

Our backup directory will now have the following files in it:

  • local.xml
  • app.tar
  • data.sql
  • media.tar
  • skin.tar

New Server

We’re ready to set Magento up on the other server now, so SSH in to your account and go to your public_html directory:

  1. cd public_html

We now want to grab all of the files from the other server, but let’s create a backup directory here so we have somewhere to move the files:

  1. mkdir backup

And let’s go in to that directory:

  1. cd backup/

Copy Files

Time to grab the files from the remote server:

  1. wget
  2. wget
  3. wget
  4. wget
  5. wget

You’ll want to replace the above URLs with the IP address or domain name from the old server. Usually whatever domain you had setup on the old server will be the same one you have on the one.

NOTE: If you’ve already updated the DNS nameservers for your domain and it’s pointing to the server we’re moving the files over to, you cannot use the domain name. Use the IP address instead. If you haven’t updated the nameservers for your domain yet, then you can use that.

Now we have all of our files over here. So let’s do a clean install of Magento. We need to go back to the public_html directory, because right now we’re still in the backup directory:

  1. cd ..

Install Magento

Ok, time to install Magento:

  1. wget
  2. tar -zxvf magento-1.1.6.tar.gz
  3. mv magento/* magento/.htaccess .
  4. mv php.ini.sample php.ini
  5. chmod o+w var var/.htaccess app/etc
  6. chmod -R o+w media

Import Database

If you haven’t setup a blank database on this server yet, do so now. We’re going to import the data from the dump file in to this database:

  1. mysql -h DBHOST -u DBUSER -pDBPASS DBNAME < backup/data.sql

Replace the values like you did when we created a dump file.

PEAR Downloader

Now we want to initialize the PEAR registry and let Magento update anything in our database that needs to be updated:

  1. ./pear mage-setup .
  2. ./pear install magento-core/Mage_All_Latest

Lastly, a little bit of cleanup:

  1. rm -rf downloader/pearlib/cache/* downloader/pearlib/download/*
  2. rm -rf magento/ magento-1.1.6.tar.gz
  3. rm -rf index.php.sample .htaccess.sample STATUS.txt

Restore Media & Theme

Almost finished! We just have to move the media directory back in place and the themes:

  1. cp backup/app.tar app/design/frontend/default/
  2. cp backup/skin.tar skin/frontend/default/
  3. cp backup/media.tar media/

And we need to extract the data:

  1. cd app/design/frontend/default/
  2. tar -xvf app.tar
  3. rm -rf app.tar
  4. cd ../../../../skin/frontend/default/
  5. tar -xvf skin.tar
  6. rm -rf skin.tar
  7. cd ../../../media/
  8. tar -xvf media.tar
  9. rm -rf media.tar
  10. cd ..

Restore Config File

Ok, so everything should be in its place. The only thing left to do is move the local.xml file where it belongs:

  1. mv backup/local.xml app/etc/

If your database details have changed from what you had on the old server, you’ll need to edit the local.xml file and update the appropriate information. It should be pretty clear what you need to update in there.

Have A Beer

That’s it! Open up your browser and your site should be working. If it is, you can delete the backup directory on the new server:

  1. rm -rf backup/

The data on the older server is safe to delete as well.

Alternative Method

The above method starts you out with a fresh, clean installation of the latest version of Magento. If you experience problems using this method, there’s another way to copy the data.

Follow the instructions above for creating a MySQL dump file, and leave that in the directory where Magento is installed.

You’re then going to archive the entire public_html directory, so you’ll want to be one directory above the directory where Magento is installed:

  1. tar -cvf backup.tar public_html/

You will then need to use wget like we did before to transfer this file from the old server to the new one. Extract the data from the backup archive we created:

  1. tar -xvf backup.tar

And move it out of it’s directory:

  1. mv public_html/* public_html/.htaccess .

You can remove the extracted public_html directory now. Do not confuse this with the public_html directory for your site, this is only the name of the directory that was in our backup file!

  1. rm -rf public_html/

Now you need to create a blank database and import the MySQL dump file to it. The instructions are listed in the previous method.

Lastly, edit the local.xml file and update the database variables if they’ve changed.

You’ll also want to delete your cache directory out:

  1. rm -rf var/cache/* var/session/* var/report/*

That’s it, you’re all done!

Share this post

Leave a Reply

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