Moving a Magento e-commerce site to a new domain name by copying an existing installation can be a confusing task if you don’t know where to begin. If you find yourself in need of doing this, use this guide to save you some headaches.
Step 1 - Setup the new domain
It might go without saying, but the first step is to configure the new domain’s web hosting with the required Magento dependencies, usually just the LAMP stack. Verify that you can load up a PHP page on the new domain before going any further just to take another variable out of the equation.
Step 2 - Export/Import the MySql Database
The next step is to export the MySql database from the existing Magento site and use the resulting file to import into the new Magento site. Because a Magento database can be fairly large, I recommend using the command line to perform a mysqldump operation like so:
mysqldump -u [username] -p [database_name] > [dumpfilename.sql]
Once you have your .sql file, copy it over to the new domain and import the file into your new MySql database.
mysql -u [username] -p [database_name] < [dumpfilename.sql]
Other methods for importing and exporting a MySql database are also available.
Step 3 - Copy the existing installation directory to the new domain
The reason you’re going through all of this trouble is to start a new site using an existing site as a baseline. For this reason, you want to copy/paste the entire installation directory from the existing install into the new domain web root folder. Usually you’ll want to compress the directory before you move it to speed things along if you’re going between servers.
tar zcf existingMagento.tar.gz /path/to/webroot/*
Then after you move the compressed archive, expand the archive into your new webroot.
tar zxf existingMagento.tar.gz
Now that the archive has been expanded into your new webroot, you’ll probably need to adjust the ownership of the files - a commonly overlooked step.
chown myusername:mygroupname /path/to/webroot -R
Step 4 - Adjust the Magento configuration
You’ll now need to update the Magento configuration for the new site. This involves editing the config file to connect to the new database location and editing the database directly to set the proper domain name.
To adjust the config file, edit the contents of path/to/magento/app/etc/local.xml so that the database connection information matches your new parameters:
Next, open up phpMyAdmin or another MySql management tool and connect to your new database. Open the table named “core_config_data” and edit the values for “web/unsecure/base_url” and “web/secure/base_url” to match your new domain name.
Step 5 - Clear the Cache/Sessions
The final step is to clear out the cache and session files that may be present from the old installation. To do this, navigate to path/to/magento/var/cache and delete everything from the folder:
rm -rf path/to/magento/var/cache/*
Now, do the same for the session folder at path/to/magento/var/session
rm -rf path/to/magento/var/session/*
At this point you should be able to load up your new Magento installation on the new domain without having to start from scratch.