Moving Magento To Another Server

Last modified by draiocht on Sun, June 5, 2011 16:56
Source|Old Revisions  |  Back To Group

This is an old revision of the document!


The following article will outline the steps and commands involved to successfully transfer Magento from one server to another. This article assumes you have a basic understanding of SSH, and that you have SSH access on both servers.

Overview

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:

/home/username/public_html/

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:

http://www.example.com/test.html

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 you’re running MySQL 4.x, you should include the following option:

--default-character-set=utf8

NOTE: If your password contains special characters or punctuation marks—for example, if your password was U!re+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. This file contains the encryption key, the user name and password to connect to the database. If the connection parameters are different between your old and new servers, then you need to edit this file to reflect the changes. The editing is straight forward as the password for the database connection is not encrypted. Now, let’s copy that over as well:

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

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

If you omit this file in the new server, you will have to go through the setting up process the first time you browse your site in the new server. Here, Magento will make a new encryption key which will cause problems with the credit card information and passwords in the database.


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


NOTE: Before continue with the movement of your site make sure that your server has all the necessary software components installed. Do the Magento Server Compatability Check as described here: http://www.magentocommerce.com/knowledge-base/entry/how-do-i-know-if-my-server-is-compatible-with-magento.

A couple of tips:

1) PDO for mysql needs to be installed (ubuntu: apt-get install php5-mysql)

2) The curl extension for PHP5 needs to be installed. (ubuntu: apt-get install php5-curl). Without this when attempting to access the admin login you will see a blank page on the server you have transferred the magento install to.


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 http://www.example.com/backup/local.xml
  2. wget http://www.example.com/backup/app.tar
  3. wget http://www.example.com/backup/data.sql
  4. wget http://www.example.com/backup/media.tar
  5. wget http://www.example.com/backup/skin.tar

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.


IMPORTANT SECURITY NOTE: Using the method above you are putting all of your data (including credit card data if you are storing it), your website files and the config file containing your database password on the public internet for anyone to download. If you must use this method, you may want to use something other than /backup as your path, and you will certainly want to delete the files immediately when you have copied them. Consider putting the files in a private folder and copying via SFTP instead.

Alternatively you can do a secure copy (SCP) as below:

  1. scp -r username@example.com:/home/username/public_html/backup .

Sure this copy will be really secure only if /backup folder is out of /public_html as below:

  1. scp -r username@example.com:/home/username/backup .

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 http://www.magentocommerce.com/downloads/assets/1.5.1.0/magento-1.5.1.0.tar.gz
  2. tar -zxvf magento-1.5.1.0.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. ./mage mage-setup .
  2. ./mage install community Mage_All_Latest

Lastly, a little bit of cleanup:

  1. rm -rf downloader/pearlib/cache/* downloader/pearlib/download/*
  2. rm -rf magento/ magento-1.5.1.0.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 .

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 ../../../
  8. tar -xvf media.tar
  9. rm -rf media.tar
  10. cd ..

Restore Config File

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, all the fields are plain and not encrypted.

So the last thing left to do is to move the local.xml file where it belongs:

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

Magento Connect Extensions & configurations

To migrate your extensions you need to copy these folders:

  • app/code/community
  • app/code/local
  • app/etc/modules
  • js

Update 1

You need to edit a few things in the database if your changing domain names or if you’re using a test server that’s just an IP address. Using PHPMyAdmin, Navicat or something similar, connect to the database. Table core_config_data has rows that need to be changed and updated with the new url. For a single store installation you need to just update:

path:                       value:
web/unsecure/base_url       http://[you_domain_here]/
web/secure/base_url         https://[your_secure_domain_here]/

with multiple stores you need also change:

path:                          value:
web/unsecure/base_url          http://[you_domain_here]/
web/secure/base_url            https://[your_secure_domain_here]/
web/unsecure/base_link_url     http://[your_domain_here]/
web/unsecure/base_skin_url     http://[your_domain_here]/skin/
web/unsecure/base_media_url    http://[your_domain_here]/media/
web/unsecure/base_js_url       http://[your_domain_here]/js/
web/secure/base_link_url       https://[your_secure_domain_here]/
web/secure/base_skin_url       https://[your_secure_domain_here]/skin/
web/secure/base_media_url      https://[your_secure_domain_here]/media/
web/secure/base_js_url         https://[your_secure_domain_here]/js/

~Fastmover

evisboy on forums provide example of code for MySQL:

  1. UPDATE core_config_data SET value="unsecure.domain.com" WHERE path=’web/unsecure/host’;

Have A Beer (STZBR!)

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. Make sure you Refresh or Disable the cache before exporting the database. You can also do an “Export” via phpMyAdmin.

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.

Open the new database with phpMyAdmin (or however you prefer), go to the [mage]core_config_data table, and edit the web/unsecure/base_url and web/secure/base_url (config_id’s 2 and 3) rows to reflect the new URL of the new server.

(Note: some seem to avoid this step by changing the paths to { {base_url} } in the Admin section [System→Configuration→General→Web→Secure and Unsecure] before attempting to move the site, and then changing it in the Admin after the move.)

(Another quick way to deal with the URL changes is to load the SQL dump into Editplus or something similar and search and replace the URLs before importing)

Edit the local.xml (app/etc/local.xml) file and update the database variables if they’ve changed (but keep the same security/encryption key).

Lastly, in order to make magento connect to work correctly edit the pear.ini (downloader/pearlib/pear.ini) and update al the paths that you see in this file. Otherwise magentoconnect will try to upgrade your old site when you use it.

(the files downloader/pearlib/pear downloader/pearlib/peardev downloader/pearlib/pecl may also need the paths updating if you have changed the path)

(Note: Updating this file -pear.ini- is a tedious job, because when you change the paths, you also need the update the field before it which starts with “s:”. You need to type in the length of characters of the path field that you have updated.)

A simple helper script to generate a new the pear.ini file: drop your old pear.ini in the same directory as the PHP script, adjust the paths in the script, open it in a browser and paste the contents into your new pear.ini.

  1. <?php
  2. header('Content-type: text/plain');
  3. $contents = explode("n", file_get_contents('pear.ini'));
  4. $data = unserialize($contents[1]);
  5.  
  6. foreach($data as $key => $value) {
  7.     if(is_string($value)) {
  8.         $data[$key] = str_replace('/old/path', '/new/path', $value);
  9.     }
  10. }
  11.  
  12. echo $contents[0] . "n";
  13. echo serialize($data);
  14. ?>

Note: The code above splits the ini file into lines by detecting the “n” character - depending on the operating system you are running on the correct new line character to split your file may vary.

Make sure there is nothing in the cache directory (var/cache and var/session):

  1. rm -rf var/*

Finally make sure the permissions are still correct on the new server:

  1. chmod o+w var var/.htaccess app/etc
  2. chmod -R o+w media

If you find any errors, make sure folder permissions are set to 755 and file permissions to 644. If your administration submenu pulldowns don’t work, make sure /js/index.php is 644.

That’s it, you’re all done!




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs