Here is a step-by-step guide on moving Magento site from one server to another. This can be from localhost to server or from server to localhost or from one server to another.
Step 1: Enable maintenance mode
This is to be done so that no users could place any order while you are backing up your website data. After you enable maintenance mode, your website will not be accessible to users. They will see ‘Service not avaiable’ message.
To enable maintenance mode:
Create a file maintenance.flag on your website root folder.
Step 2: Backup files
You can copy all the files using FTP. However, there are thousands of files in Magento. If you have SSH access, then it will be quick to compress all the files and then download the compressed single file.
You can zip files using the following command on SSH:
zip -r archive_name.zip *
You can also use other compressions like tar, tar.gz, bz2, etc. See more: Linux commands to compress & extract files and folders
Step 3: Backup database
You can use phpMyAdmin to backup/export your database.
If you have SSH access, then you can use mysqldump command to backup your Magento database:
mysqldump -h hostname –u username –p password database_name > /path/backup.sql
You may also backup your database in compressed form:
mysqldump -h hostname –u username –p password database_name | gzip > /path/backup.sql.gz
mysqldump -h hostname –u username –p password database_name | bzip2 > /path/backup.sql.bz2
See more: MySQL: Backup and Restore database & table
Step 4: Upload files to new server
After you download all files and database backup from old server, you can now upload them into new server.
Step 5: Extract files
This step is not required if you have not compressed your files.
Extract your compressed Magento website files. You can do this through File Manager (Cpanel).
If you have SSH access then you can extract compressed file through the following command:
unzip archive_to_extract.zip
Step 6: Edit app/etc/local.xml
Edit app/etc/local.xml file and put correct mysql hostname, username, password, and database name.
1
Step 7: Adjust files and folders permission
You might get “Internal Server Error” if your Magento files and folders permission is not correct. All files should have 644 permission and folders should have 755 permission.
You can change the file and folder permission through FTP.
In SSH, you can do through the following commands:
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;
After that, you can adjust again the permission of ‘media‘, ‘var‘, and ‘app/etc‘ directory:
chmod -R 777 media var app/etc
Step 8: Import/Restore database in new server
Create a new database in the new server. Then you can import the database you have exported/backup from the old server with the help of phpMyAdmin.
If you have SSH access, you can restore/import database from the following command:
– Restore plain sql database file
mysql -h hostname –u username –p password db_name < /path/backup.sql - Restore database as gzip compressed file (Linux) gunzip < /path/backup.sql.gz | mysql -h localhost -u username -p database_name - Restore database as bzip2 compressed file (Linux) bunzip2 < /path/backup.sql.bz2 | mysql -h localhost -u username -p database_name
Step 9: Update base URL
You need to update base URL to your new website’s URL. In the below sql statement, change example.com to your new website URL.
UPDATE core_config_data SET value=”http://example.com/” WHERE path=”web/unsecure/base_url”;
UPDATE core_config_data SET value=”https://example.com/” WHERE path=”web/secure/base_url”;
You’re done.
Finally, you have moved/transferred your Magento website from one server/location to another.
One issue I faced while moving Magento site
After I completed the transfer of files and changed all the necessary settings in the new location, I got problem with the URLs of the site. SEO URL rewrite was not working properly even though mod_rewrite was enabled. URLs worked when I put index.php in them.
For example:
http://example.com/category (not working)
http://example.com/index.php/category (working)
The main cause to this problem was “missing .htaccess” file. Due to some reason, there was no .htaccess file in Magento root directory of the new site. I copied .htaccess from old site and added/uploaded the file and everything is working fine now.
Hope it helps. Thanks.