How to: Move your WordPress site to a new VPS using Command Line

There are several reasons for you to want to move your WordPress site to a VPS, most notably, your site has outgrown shared hosting or you want to improve performance by leveraging caching functionality. Transferring a WordPress site is not complicated if you do your prepwork first.

Step 1: Prepping your old WordPress site for transfer

A WordPress installation has main components:

1) The installation (the files)
2) The database

You will need to prep both of these to transfer. This tutorial covers transferring your Wordpres site using SSH. This is the fastest but not recommended for people who are unfamiliar using SSH.

Open your Terminal.app (PuTTY or SecureCRT for Windows users) and type the command to connect to your old host.

Once you log in, navigate to the directory where your WordPress is installed.

Once you’re at your WordPress installation, you will need to get a dump of the MySQL database. To do this, you’ll need to know the database name, the server, the username, and password. If you don’t remember this information, it is all in your wp-config.php file located in the root directory of your wordpress installation. Get the dump using the following command:

mysqldump –u USERNAME –p DATABASENAME > FILENAME.sql

Where:
* USERNAME is your database username
* DATABASENAME is the name for your database
* FILENAME.sql is the name of the file you wish to export your database to. It can be anything as long as you remember the name.

Once you are done with the database dump you will want to compress all your WordPress files and you SQL file into one to make the transfer easier. Make sure you are in your WordPress directory. To compress all the files run the following command to compress everything into a file called wordpress.tar.gz:

tar cvzf wordpress.tar.gz *

WARNING:
The tar command will not save hidden files in the current directory, if you have .htaccess file then save it to copy to the new WordPress directory on the new VPS

Step 2: Importing your WordPress Install on your NEW VPS

Next you will need to create a database and a database user for your WordPress installation.

mysql -u root -p

Create a new mysql database or use the db name used on the old VPS:

mysql> CREATE DATABASE wordpress;
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO "wpuser"@"localhost" IDENTIFIED BY "password";
mysql> FLUSH PRIVILEGES;
mysql> quit

Where:
* wordpress is your mysql database name
* wpuser is your mysql username
* password is your mysql username password
* any of these can be changed as long they match the credentials in the wp-config.php

Next you will need to move your compressed site (wordpress.tar.gz) from your old server to your new VPS.
You can use a ftp client or scp to upload the file in the website root directory:

from the old vps server command line:
scp -P22 wordpress.tar.gz user@newvpsip:/var/www/
or downloading it from the new vps server:
scp -P22 user@oldvpsip:/var/www/wordpress.tar.gz .

Where:
* scp is the command line program to transfer files securely
* -P tell scp the server port to connect to (the port ssh server is listening to)
* /var/www is the wordpress root directory path, it could differ depending by your new/old vps apache configuration

Once the file (wordpress.tar.gz) has finished transferring and then moved to the wordpress root directory in the new VPS, untar using:

tar xvzf wordpress.tar.gz

This should have uncompressed all your files. You can verify by using the ls command.

As mentioned before the tar command in the compressing process did not pack the root directory hidden files, most likely the only file
you need is .htaccess, copy it in the new vps wordpress root directory.
Also you need to give the apache user ownership to all the uncompressed files, in the wordpress root directory write the following command:

chown -R www-data.www-data *

Where:
* www-data.www-data is user and group (separated by a dot) for apache files, nowdays www-data is the common username & group, it could be different. By using the ps aux command line you can see which user is running the apache process

The next step is to import the sql file you created earlier, into the database you just created. It’s another command line:

mysql -u wpuser -p wordpress < filename.sql

Where:
* wpuser is your mysql username
* wordpress is your mysql database name

At this point assuming you have set up apache configuration for the new vps the next step is to configure DNS to point your website to the new VPS IP.

Leave a Reply

Your email address will not be published. Required fields are marked *