Copying Magento to another server

This is my recipe for replicating an existing Magento instance to a new Centos 6 Linux dedicated server. Note that there are many variations on this theme and your setup will vary depending on your configuration and particular requirements. Always do backups first and always do tests in a development environment before trying anything risky in a production environment. If you are copying to a new server that is not already running Magento, it is also good idea to do a vanilla install fronm Magento installation package first. This will ensure that you have all appropriate software pre-requisites and allow you to test email and credit card validation.


In Source Server:


1. Backup Magento files

Backup Magento files with tar command. Create a tar.gz backup file.

tar cfzv /home/myMagentoScriptBackup.tar.gz    /var/www/
where:
c = create
f = file=ARCHIVE Use archive file (or device) ARCHIVE.
z = gzip, –ungzip filter the archive through gzip
v = verbose Operate verbosely

tar.gz file is your destination path and file
/var/www/ is what you want to copy.

2. Backup Magento database
mysqldump -uUSERNAME -pPASSWORD DATABASENAME > /someDirectory/dumpfile.sql 
where:
mysqldump - is a backup program used to dump a database or a collection of databases for backup or transfer to another SQL server.
-u indicates username to follow
-p indicates password to follow
DATABASENAME is your mySQL database name
/someDirectory/dumpfile.sql is the location and name of your mySQL dump file.


In Target Server:


3. Verify that you have php and apache pre-requisites

Verify that you have php pre-requisites set up. To do this, create a program with phpinfo().


mcrypt

Look for section in phpinfo page with mcrypt module. If you can't find it, then try enabling it php.ini. If this does not work then install it.

To install mcyrypt

wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm

yum update
yum install php-mcrypt*

if you get error on yum command, try

sudo yum upgrade ca-certificates --disablerepo=epel

mod_rewrite

Many Magento sites use apache mod_rewrite module. If your sites requires this, you have to make sure it is enabled.

check httpd.conf file. Make sure mod_rewrite.so is not commented out

LoadModule rewrite_module modules/mod_rewrite.so
then find all occurences of
AllowOverride None
and change to
AllowOverride All

4. Create new VirtualHost in Apache http.conf file

Create a new new VirtualHost in http.conf file. In this example, Magento code should be found in /var/www/MYSTORELOCATION and site will be called MYSITE.example.com

vi /etc/httpd/conf/httpd.conf
<VirtualHost *:80> 
 DocumentRoot /var/www/MYSTORELOCATION
 ServerName MYSITE.example.com
</VirtualHost>  

5. Reboot apache server

Reboot the apache server and watch for errors

service httpd stop
service httpd start

6. Restore Magento mySQL Database

You may first have to create database and assign rights in mySQL.

Restore database from SSH (Linux command line)

mysql -uUSERNAME -pPASSWORD DATABASENAME  < /someDirectory/dumpfile.sql 
where:
-u indicates username to follow username
-p indicates password to follow
DATABASENAME is your mySQL database name
/someDirectory/dumpfile.sql is the location and name of your mySQL dump file.

You may have to delete and re-create database first if it already exists (this is simpler than clearing all tables). You may also have to GRANT ALL PRIVILEGES to specific user if you are not using default user to access mySQL.

drop database DATABASENAME;
create database DATABASENAME  DEFAULT CHARACTER SET UTF8 COLLATE UTF8_UNICODE_CI;

7. Restore Magento scripts

If old copy of target Magento directory is already on the target server, you may want to remove it.

cd /var/www/
rm -r MYSTORELOCATION

or

rm -r /var/www/MYSTORELOCATION

Use tar command to restore backed up files

tar zxvf  /home/myMagentoScriptBackup.tar.gz 
where:
-z, –gzip, –ungzip filter the archive through gzip
-x, –extract, –get extract files from an archive
-v, --verbose Operate verbosely
-f, --file=ARCHIVE Use archive file (or device) ARCHIVE.
-C, --directory DIR Change to directory DIR before performing any operations.
-r, delete recursively

Depending how you created the backup, you may have to move the restore to the correct location.

 mv /var/www/MYSTORELOCATION/var/www/MYSTORELOCATION/  /var/www/
or a recursive copy
cp -R /var/www/MYSTORELOCATION/var/www/MYSTORELOCATION/  /var/www/

You may also want move the directory it with the move command if old name is not the same as new name.

 mv OLDNAME MYSTORELOCATION

8. Reset base_urls in Magento

These SQL commands will indicate to Magento the name of the secure (checkout) and unsecure (everything else) base_urls

UPDATE core_config_data SET value= 'http://MYSITE.example.com/' WHERE path= 'web/unsecure/base_url';
UPDATE core_config_data SET value= 'http://MYSITE.example.com/' WHERE path= 'web/secure/base_url';

9. Set SQL username and password in Magento

Indicate to Magento correct SQL username and password. This is contained in app/etc/local.xml. In XML below, make sure that host, username, password and dbname correspond to values in mySQL database.

vi /var/www/MYSTORELOCATION/app/etc/local.xml 
<host><![CDATA[localhost]]></host>
<username><![CDATA[SQL-UserName]]></username>
<password><![CDATA[SQL-Password]]></password>
<dbname><![CDATA[DATABASENAME  ]]></dbname>
<initStatements><![CDATA[SET NAMES utf8]]></initStatements>
<model><![CDATA[mySQL4]]></model>
<type><![CDATA[pdo_mySQL]]></type>
<pdoType><![CDATA[]]></pdoType>
<active>1</active>

11. Clear cache

Remove cache and session data imported from source server. This is more important than you may think. Many apparent problems can be resolved by clearing the cache.

rm -rf /var/www/MYSTORE/var/cache/m* 
rm -rf /var/www/MYSTORE/var/session/*

12. Check file ownership

Find the web server user. This is usually the user 'apache'. To do this:

grep User /etc/httpd/conf/httpd.conf

change the owner of all files and directories in your magento root directory. Example:

chown -R apache /var/www/magento
13. Verify permissions

These are recommended permissions as per Magento documentation. The idea is to give minimal required permissions.

400 permissions for files (-r--------) prevent any user (even the web server user) from overwriting files. This prevents attacks that depend on overwriting existing files with malicious content.

500 permissions for directories (dr-x------) gives the web server user read and execute privileges to prevent the accidental deletion or modification of files in the directory. Other users have no access to Magento directories.

700 permissions (drwx------) for the media/ and var/ directories give full control (that is, read/write/execute) to the owner and no permissions to anyone else.

600 permissions (-rw-------) for files in the media/ and var/ directories enable the web server user to write to them and to overwrite them.

To adjust persmissions

find . -type f -exec chmod 400 {} \;
find . -type d -exec chmod 500 {} \; 
find var/ -type f -exec chmod 600 {} \; 
find media/ -type f -exec chmod 600 {} \;
find var/ -type d -exec chmod 700 {} \; 
find media/ -type d -exec chmod 700 {} \;
chmod 700 includes
chmod 600 includes/config.php

Two additional places where program dynamically creates and deletes files

chmod 660 var/locks
chmod 777 var/locks  - verify?
chmod 660 /var/log

Note that other custom modules may require different permissions.


10. Test

goto your site and test...

http://MYSITE.example.com/

Other tasks


A. Proxy Server

Are you doing tests behind a proxy server?

to set up for your current session

export http_proxy="999.999.999.999:3128"
export https_proxy="999.999.999.999:3128"
replace above values with your proxy server IP and port number

to set up permanently, for all users, edit /etc/profile and add

export http_proxy="http://999.999.999.999:3128"
export https_proxy="https://999.999.999.999:3128"
replace above values with your proxy server IP and port number

B. default value for the media type charset

Specify a default value for the media type charset parameter. The most common type is utf-8.

In file httpd.conf add, or change if it's already there

AddDefaultCharset utf-8

or this line to your .htaccess

IndexOptions +Charset=UTF-8

C. Set permissions, clean up of custom logs

If your site has some special log files make sure that your application has write permissions

set directory to rwx------

chmod 700 /var/www/mysite/myAPI/log

set files to rw-------

chmod 600 /var/www/mysite/myAPI/log/*

You may also want to clean out your custom logs

echo "" > /var/www/mysite/myAPI/log/api-access.log
echo "" > /var/www/mysite/myAPI/log/api-error.log

D. Some Administrative Settings in Magento Admin Console

You may want to identify this site differently, if so you may want to change some store parameters:

Change store name

Configuration -> General -> Store Name

Change store address

Configuration -> Store Email Addresses

Change Sales emails

Configuration -> Sales Email

E. Credit card interface

If you use credit card interface

  • change from prodcution to test credentials
  • make sure flag set to testing
  • point to appropriate url endpoint (test or prodction)

F. Other
  • remove/change analytics utilities
  • are you picking up orders from remote system: check IP addresses, credentials
  • you may want to delete catalog, product, sales. Some nice recipes here.
  • delete old crash reports in /store/var/report