How to Move a Website to a New Server

IT Finesse Level: 

  1. Start ssh Sessions

    • Start an ssh session to your source website host and change to the user home directory.
      ssh host.server.com
      cd ~
    • Repeat the above steps on the destination website host server
  2. Create an archive folder.

    • Make a new folder to store your backup files with the command...
      mkdir archive
    • Repeat the above step on the destination website host server
  3. Archive the website files

    Use tar on the source server to make a compressed copy of all your site files with the command...
    tar -czvf ~/archive/mysiterootfolder-yyyy-mm-dd.tar.gz -C ~/html mysiterootfolder
    Note the space between "-C ~/html" and "mysiterootfolder"

    A quick explanation of the above command...
    tar runs the program which compresses files
    -zcvf are the options selected when running the tar program
    c = create a compressed file
    z = use gunzip compression
    v = verbose (output all the program's actions while it's running)
    f = create a file
    The 1st path (~/archive/......) is the path of the compressed file you wish to create

    -C = Change to directory specified by the 2nd path - (~/html) This prevents the entire path from root being created in the archive
    The 3rd path (~/mysiterootfolder/......) is the path to the folder you want to make a compressed copy of.

  4. Copy Backup to the New Server Using rsync

    Either execute rsync on the source server to copy the compressed backup file from the original server to your second hosting server with the command
    rsync -azv --partial-dir ~/rsync-partial ~/archive/mysiterootfolder-yyyy-mm-dd.tar.gz user@host.server2.com:~/archive/

    Or execute rsync on the destination server to copy the compressed backup file from the original server to your second hosting server with the command
    rsync -azv --partial-dir ~/rsync-partial username@host.server.1.com:~/archive/mysiterootfolder-yyyy-mm-dd.tar.gz ~/archive/

    A quick explanation of the above commands...
    rsync runs the program which transfers the files
    -azv --partial-dir are the options used to make rsync behave in the desired way
    a = specifies that any sub-directories and nearly all files are included
    z = compress file to speed up the transfer
    v = verbose (output all the program's actions while it's running)
    partial-dir = creates a temporary copy of the file being transferred in the specified directory which can be resumed from if the transfer is interrupted part way. The directory is automatically resumed once the transfer is complete. This saves have to start the transfer again from scratch if it has previously failed.

  5. Unzip the archive on the destination server

    Use tar on the destination server to make a decompress the site files with the command...
    tar xvf archive/mysiterootfolder-yyyy-mm-dd.tar.gz -C public_html/

  6. Export the Site Database and Transfer to the New Server

    1. Export site database to a file on the source server
      mysqldump -u database_username -p database_name -hdatabase_host_address > ~/archive/sitename-yyyy-mm-dd.sql
    2. Transfer site database file to the destination server by executing rsync command on the destination server
      rsync -azv --partial-dir ~/rsync-partial username@host.server.1.com:~/archive/sitename-yyyy-mm-dd.sql ~/archive/
    3. Create a new database on the destination server
      The steps for this are typically to use the host providers mySql interface to
      • Create a new database, giving it the same name as the original database
      • Create a new database user, giving it the same name as the one used with the original database
      • Add the new user to the new database
  7. Import the database file into the new database on the destination server


    mysql -u database_username -p database_name < ~/public_html/sitename/sitename-yyyy-mm-dd.sql
  8. Edit Database Configuration

    1. Open the website config file
      This scenario assumes WordPress is being used whereby wp-config.php must be edited
      vi ~/public_html/mysiterootfolder/config-php
    2. Edit database related parameters to reflect the new database
      DB_NAME
      DB_USER
      DB_PASSWORD
      DB_HOST
  9. Setup the Domain on the Hosting Server

    1. Go to your hosting provider's domain or Add-on domain configuration page
    2. Enter the domain details including Domain name, Subdomain and the Document root e.g. public_html/sitename.com
    3. Confirm the addition
  10. Edit DNS Configuration

    1. Go to your site domain provider's configuration page
    2. Edit or enter the A record to reflect the new server ip address