Hey @DizzyLizzy, great question. Migrating servers was something I wanted to cover this year. I just didn’t think I’d be talking about it so soon There’s a couple of ways you can do this, but this is my preferred method.
I’m not going to go into too much detail here, because it’s Saturday, it’s late, and I want to catch up on The Punisher Season 2
Ok, so in this example, I’m going to make the following assumptions. Just swap out the variables where appropriate…
Original Server
IP: 123.123.123.123
Location of Magento files: /var/www/html/
SSH User: magento
SSH Pass: password1
New Server
IP: 456.456.456.456
Location of Magento files: /var/www/html/
SSH User: magento
SSH Pass: password1
Rsync
The method of which we will be transferring those files, is with the use of something called rsync. This is a pretty awesome tool that allows the secure transfer of files over SSH. Thus not requiring the need to download files from one server and upload them onto the other (which can take forever). More information on rsync
You can use rsync on either server, because you can either tell it to:
- Copy local files to a remote location
- Copy files from a remote server to a local directory
It doesn’t really matter which way around you do this. I guess it just comes down to either preference or convenience.
In my example, I will be logged into the New Server, therefore Copying Files from the Original Server to a local directory.
Step 1: Whitelisting
Before you can use rsync, you will need to whitelist your servers with one-another. Otherwise you won’t be able to connect. In my example, I’d have to allow 456.456.456.456
to connect to the Original Server over SSH (port 22). Hopefully, you know how to do that already.
Step 2: Log in
Next. we need to log into the New Server. If you log in as the superuser, don’t forget to switch to the magento
user before running the rsync command.
It’s important to note that when the files are copied across, they will have the ownership of whomever you ran the command as. For example, if you run the rsync command whilst logged in as root
, then the files will have those ownerships. And if you run the rsync command as magento
, then obviously they’ll be written as magento
.
If you mess up, it’s easy to fix. Once the files are copied over, just run the following command as a superuser to reset the permissions back to magento user: sudo chown -R magento:www-data /var/www/html/
Step 3: Navigate to web directory
What I like to do next, is navigate to my web directory. This isn’t necessary, but I think it’s a good habit to remember where you are on the server. So, in this case I’d simply do cd /var/www/html/
Step 4: Time to copy the files
Now, it’s time to use rsync to start transferring the files. Remember, we are transferring remote files to our local server:
rsync -avzhe ssh [email protected]:/var/www/html/ /var/www/html/
Breakdown: connect to Original Server as magento user and copy files from /var/www/html/
to /var/www/html/
or
rsync -avzhe ssh [email protected]:/var/www/html/ .
Breakdown: connect to Original Server as magento user and copy files from /var/www/html/
to the directory where I’m running the command from`
Running this command will take a little while, but you’ll see all of the files being transferred as it happens.
Step 5. Post transfer
Sometimes, after an rsync I get an issue with loading the site for the first time. If I remember correctly, I have to delete the following 2 folders. Don’t worry, they’ll regenerate next time you load the Frontend/Backend:
/var/www/html/pub/static/adminhtml
/var/www/html/pub/static/frontend
Also, I play-it-safe by running the pre-installation commands for resetting permissions (ref How to install Magento 2.3 post)
Then finally, I’ll clear the Magento cache with bin/magento c:c
Disclaimer: Always make backups before following any instructions or doing anything you’re not comfortable with
Final thoughts
There’s probably a good argument for zipping all of your files before transferring them. But I normally skip this step. Each to their own I guess.
Hopefully, this quick-guide helps. I know it’s a bit rushed and it’s easier to follow along on video - But this is the best I can do right now.
I hope I didn’t miss anything. Don’t forget to let us know how you get on.
Good luck