Hosting Websites on Bare Minimum VPS/Dedicated Servers

Migrating WordPress to a VPS

Tags: , , , , , Date/Time: May 16, 2019 @ 3:53 pm, by Dusko Savic

Migrating WordPress to a VPS

This tutorial will show you how to migrate your present WordPress blog to a new VPS. Perhaps

  • Your current site is on a shared hosting and you want more flexibility
  • Your blog is becoming really popular and you want to make your site available to all those visitors
  • You are installing all of your sites on a new VPS and one of them is in WordPress
  • You may have several WP sites and you want a guide on how to transfer them to the new VPS

WordPress Icon Logo

What We Are Going To Cover

  • Export WordPress database
  • Import it to your new server
  • Export WordPress installation directory from the old server
  • Upload WordPress installation directory to your new VPS using FTP, as well as
  • Transfer WordPress files from server to server, directly
  • Edit wp-config.php file to reflect the changes.

Prerequisites

The VPS you’ll migrate your WordPress installation to will run on

  • Ubuntu 16.04 with at least 512Mb of RAM and
  • 15Gb of free disk space.
  • You will need root user access.
  • LAMP stack (Linux, Apache, MySQL, PHP) already installed.

Run the following two commands to ensure that extensions that may be missing from the basic PHP setup are present: zip, unzip, php-mysql (php-mysql allows PHP to access MySQL databases):

sudo apt install php-mysql -y zip unzip
sudo systemctl restart apache2

On the old host you should be able to export and compress the database, either through

  • cPanel installed
  • or using SSH access.

On the desktop computer, you should have:

  • FTP client to connect to the new VPS (we use FileZilla), and
  • text file editor (we recommend Notepad++ on Windows and BBEdit or TextEdit on a Mac)

The Goal: Have The Same Blog in Two Places

I am going to migrate my own blog from duskosavic.com. It looks like this on its original address:

WordsPress duskosavic.com

In the end, we shall see the same blog on another IP address.

Step 1: Downloading Database From the Original Server

With cPanel, you can download database either via phpMyAdmin or Backup Wizard options from the main cPanel menu.

Download Database With phpMyAdmin

WordsPress phpMyAdmin Export

Log into phpMyAdmin and search on the left side which database you need to migrate. For a typical WordPress installation, database name would end with _wrdp1. However, if you have installed WordPress several times, the names could be _wrdp2, _wrdp3 and you need to know which one to use exactly.

Click on a database, and phpMyAdmin will expand it with the list of tables it contains:

WordsPress phpMyAdmin Export

Click on the Export tab and then scroll down and press Go. Download and save the file, and note the filename and location on your local machine.

Download Database With Backup Wizard

WordsPress phpMyAdmin Export

Select Backup/Restore, then Select Partial Backup, then MySQL Databases and choose the database to download.

Step 2: Install FTP On the Server

Now we use a well-known package called vsftpd to install a FTP server on your new VPS. We then connect to it via FileZilla and upload the database you exported in the previous step.

To install vsftpd, run the following commands:

sudo apt update
sudo apt install vsftpd
sudo systemctl enable vsftpd
sudo systemctl status vsftpd

The FTP service should be up and running:

WordPress FTP up and running

We also set up a firewall to restrict access to only certain ports. The simplest one to use is ufw (uncomplicated firewall). Install it by running:

sudo apt install uwf

Then, run the following commands to allow access to SSH, HTTP, HTTPS, FTP (active and passive) protocols:

sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
sudo ufw allow 20/tcp
sudo ufw allow 21/tcp

Finally, enable it:

sudo ufw enable

Add a FTP user, set its permissions and allow it to change files in the html directory:

sudo adduser ftpuser
sudo usermod -d /var/www ftpuser
sudo chown ftpuser:ftpuser /var/www/html

Create a strong password when it asks you to enter it. We shall need it later.

Back up the old config file and create a new one with nano:

sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.bak
sudo rm /etc/vsftpd.conf
sudo nano /etc/vsftpd.conf

Add the following lines:

listen=NO
listen_ipv6=YES
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
use_localtime=YES
xferlog_enable=YES
connect_from_port_20=YES
chroot_local_user=YES
secure_chroot_dir=/var/run/vsftpd/empty
pam_service_name=ftp
ssl_enable=NO
force_dot_files=YES

Save and close the file. The configuration shown above orders vsftpd to allow access to local user accounts.

Restart vsftpd for the configuration to take effect:

sudo systemctl restart vsftpd

Step 3: Configure FileZilla

Let’s set up the VPS connection details in FileZilla. Click on File > Site Manager from the main menu and then, in tab General, enter IP address of the server, FTP user name and its password:

Adding User to FileZilla

In the Advanced tab, enter the local directory as well as /var/www/html for your remote directory. If you wish, check Use synchronized browsing. That option saves many clicks and “puts you on the same page” regarding paths, both locally and remotely.

Upload the exported database via FileZilla to the new server, under the /html folder. Once uploaded, you can check its contents from the SSH console:

sudo nano /var/www/html/<db name>.sql

It will look similar to this:

Migration Database Contents

Step 4: Creating a Database On the New Server

Launch MySQL shell:

sudo mysql -u root -p

When asked, enter the password for MySQL user root.

Once in MySQL prompt, copy and paste the following code as a whole, then press Enter on the keyboard:

CREATE DATABASE blogdb character set utf8 collate utf8_bin;
GRANT ALL PRIVILEGES  on blogdb.* to bloguser@localhost identified by "<your strong password>";
FLUSH PRIVILEGES;
USE blogdb;
SOURCE /var/www/html/<your db file name>.sql;
FLUSH PRIVILEGES;
SHOW TABLES;
EXIT;

Replace with your password and the exact file name, where noted.

The first line will create database blogdb and specify charset to be UTF8.

GRANT ALL PRIVILEGES creates a new user called bloguser and grants it all privileges on database blogdb.

FLUSH PRIVILEGES reloads user permissions with the changes made. * USE* directs which database to use and SOURCE executes the file’s contents, which will actually import the database.

To confirm that the SOURCE command ran successfully, we run SHOW TABLES to list all of the imported tables.

Migration Database Transferred

The last command is EXIT, to close the MySQL prompt and return back to the usual Ubuntu shell.

With this, the database from the old site has been successfully copied into a database on the new site. For security reasons, delete the exported database file now when it is no longer needed:

sudo rm /var/www/html/<your db file name>.sql

Step 5: Installing WordPress on the VPS

With the database set up, the next step is to copy the old WordPress installation over to the VPS.

Transferring WordPress Installation With FileZilla

From FileZilla, download your old WordPress installation onto the local computer. Then, upload it to a VPS folder named blog. This will take some time to finish, so be patient.

You will have to use one FileZilla account to access the original blog and another account — the one we installed in this tutorial — to upload files to the new server.

Uploading Compressed WordPress Installation With FileZilla

A faster way to upload WordPress installation is to zip the entire folder on the desktop computer and then use FileZilla to upload it to /var/www/html folder on the new server.

Compressing the Original WordPress Installation

Compression From cPanel

Choose File Manager from cPanel, click on the blog directory and then on option Compress.

Migration cPanel Compress

Download the compressed file to the desktop computer via FileZilla. It is crucial to make a backup of compressed WordPress installation on your desktop computer!

Transferring zipped WordPress Installation

If you have SSH access to the old server, you can use zip or tar commands to compress blog directory. Use cd command to transfer to the right directory of the old server, then run zip:

zip -r duskosavicblog.zip blog

On the new server, do this:

cd /var/www/html
wget https://duskosavic.com/duskosavicblog.zip
zip -r duskosavicblog.zip blog

It will transfer the zipped file and unpack WordPress.

Compression With tar

The same idea, but another command on the old server:

tar -czvf duskosavicblog.tar blog

Now on the new server:

cd /var/www/html
wget https://duskosavic.com/duskosavicblog.tar
tar -xzvf duskosavicblog.tar

NOTE: You should do either zip or tar, depending on the source of compression. If you are compressing blog directory on the desktop computer, you will always use zip and not tar.

Server to Server Transfer — Speed in Seconds

Using wget is the fastest possible way to transfer files. The above transfer with wget, lasted for about a second from one server to another.

Compressed File Desktop to Server Transfer — Speed in Minutes

Uploading the same compressed file from desktop to the new server, using FileZilla, lasted for a couple of minutes.

Uncompressed Files Desktop to Server Transfer — Speed in Hours

Uploading of the uncompressed blog directory from desktop to VPS with FileZilla may take an hour or more, depending on your Internet upload speed.

This kind of gain in speed is the main reason why you would want to have your own VPS!

When the transfer is finished, delete compressed file from the old server:

rm duskosavicblog.tar

Step 6: Reconfiguring WordPress

Our new blog resides in /var/www/html/blog. Now we change parameters for database access, because we created a new one in Step 4. WordPress stores its configuration in a file named wpconfig.php_ so let’s open it for editing:

sudo nano /var/www/html/blog/wp-config.php

Find the lines that set database credentials and replace them with the following:

/** The name of the database for WordPress */
define( 'DB_NAME', 'blogdb' );

/** MySQL database username */
define( 'DB_USER', 'bloguser' );

/** MySQL database password */
define( 'DB_PASSWORD', '<your db password>' );

Remember to replace your database user password in the last line, then save and close the file.

You can now browse to http://SERVER_IP/blog. You will see your WordPress blog, which should look the same as the one on the old hosting, only with a different address.

Migration WordPress Finished Migration

What To Do Next

You should point a domain name to your new VPS, if you haven’t already. You will have to wait 48 hours to be quite sure that the new DNS records have propagated to the entire Internet. In those two days, some visitors will see the old and some will see the new site, so you may not want to add any new content during that period. You may also disable comments on the old site, so that new comments won’t get discarded.

If you wanted to leave your previous host, you may do so after these 48 hours have passed. You will have the backup of the site on your local machine and it would be wise to make a backup of that backup, just in case.

No Comments

Leave a Reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Quoting webhostingtalk.com URL seems to get binned consistently here, but I do peek into the spam box frequently to publish those comments.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

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