Migrate WordPress on Ubuntu 17.04 / 17.10 with Nginx, MariaDB and PHP Support

Someday you may find yourself wanting to move your current WordPress website or blog to a new host. Maybe be your current host isn’t serving your needs and you think a new host might and cost you less.

There are many reasons webmasters migrate their websites and blogs. Migrating your websites isn’t as bad as some might think. The primary challenge is getting your data over to the new host intact and without issues.

This brief tutorial shows students and new users how to migrate their WordPRess websites or blogs to new a hosts without missing anything. Users will learn how to setup the new host, get the data over and restore the website easily.’

To get started with WordPRess migration, continue below

For this tutorial, our new host server will be running the latest version of Ubuntu with Nginx, MariaDB and PHP. So, let’s get started.

Step 1: Logon to the new server and Set it Up

When you receive your new server, your first task will be to set it up. This means installing and configuring Nginx, MariaDB and PHP. Most host providers now allow SSH access. So, logon via SSH and run the commands below to update the Ubuntu server.

sudo apt update && sudo apt dist-upgrade && sudo apt autoremove

STEP 2: INSTALL Nginx

On the new server, run the commands below to install Nginx… We’re assuming that Nginx is being used on the old server.

sudo apt install nginx

Next, run the commands below to stop, start and enable Nginx service to always start up with the server boots.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

STEP 3: INSTALL MARIADB

On the new server, run the commands below to install MariaDB database server.

sudo apt-get install mariadb-server mariadb-client

After installing, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots.

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

After that, run the commands below to secure MariaDB server and create a new root password.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]:  Y
  • Reload privilege tables now? [Y/n]:  Y

Restart MariaDB server

sudo systemctl restart mariadb.service

STEP 4: INSTALL PHP-FPM AND RELATED MODULES

Now that Nginx and MariaDB are installed, run the commands below to install PHP and related PHP modules on the new server. This is a good list of PHP modules to install.

sudo apt install php-fpm php-common php-mbstring php-xmlrpc php-soap php-gd php-xml php-intl php-mysql php-cli php-mcrypt php-ldap php-zip php-curl

After install PHP, run the commands below to open Nginx PHP default file.

sudo nano /etc/php/7.1/fpm/php.ini        # Ubuntu 17.10
sudo nano /etc/php/7.0/fpm/php.ini        # Ubuntu 17.04

Then change the following lines below in the file and save.

post_max_size = 20M
memory_limit = 256M
max_execution_time = 300
max_input_vars = 10000
upload_max_file_size = 64M

STEP 5: CREATE A BLANK WORDPRESS DATABASE

At this point, all the required WordPress packages and and servers are installed. The new server  is now ready to host WordPress… On the new server, create a blank WordPress database. We’ll use this blank database to restore the database from the old server into.

Run the commands below to logon to the database server. When prompted for a password, type the root password you created above.

sudo mysql -u root -p

Then create a blank database called WP_database  you can use the same database name from the old server.

CREATE DATABASE WP_database;

Create a database user called wp_user with new password.  You can use the same username and password from the old server.

CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'type_password_here';

Then grant the user full access to the database.

GRANT ALL ON WP_database.* TO 'wp_user'@'localhost' IDENTIFIED BY 'type_password_here' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

STEP 6: CONFIGURE THE NEW WORDPRESS SITE

Next, configure the WordPress configuration file on the new server. Run the commands below to create a new configuration file called wordpress

sudo nano /etc/nginx/sites-available/wordpress

Then copy and paste the content below into the file and save it. Replace example.com with your domain name.

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/wordpress;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

    location / {
    try_files $uri $uri/ /index.php?$args;        
    }

    location ~ \.php$ {
    fastcgi_split_path_info  ^(.+\.php)(/.+)$;
    fastcgi_index            index.php;
    fastcgi_pass             unix:/var/run/php/php7.1-fpm.sock;     #Ubuntu 17.10
  # fastcgi_pass             unix:/var/run/php/php7.0-fpm.sock;     #Ubuntu 17.04
    include                  fastcgi_params;
    fastcgi_param   PATH_INFO       $fastcgi_path_info;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

}

Save the file and exit.

STEP 7: ENABLE THE WORDPRESS SITE AND REWRITE MODULE

After configuring the VirtualHost above, enable it by running the commands below

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/

STEP 8: RESTART Nginx

To load all the settings above, restart Nginx by running the commands below.

sudo systemctl restart nginx.service

Step 9: Backup Your Database on the old Server

At this stage, the new server is ready for WordPress data… Logon to the OLD server and run the commands below to backup the database for the WordPress site to the /tmp directory.

sudo mysqldump -u root -p WP_database > /tmp/WP_database.sql

You should be prompted for the root password.. if the password you typed is correct, the database content will be dumped or stored in the /tmp directory.

Step 10: Restore WordPress Site and Database on the NEW Server

Logon to the NEW server and restore the content from the old server… there are many tools you can use to copy the data over securely… Rsync  and SCP are great tools to copy data securely. We’re going to be using rsync here.

The syntax for rsync is:

rsync option source-directory destination-directory

Run the commands below on the new server to copy the database content that was backed up on the old server to the /tmp directory..

rsync -avzP username@192.168.20.22:/tmp/WP_database.sql /tmp

Replace the 192.169.20.22 with the old server’s IP… the commands above will copy the WP_database content via SSH over to the local /tmp directory on the new server.

Next, run the commands below to copy WordPress website content stored in the /var/www/html/wordpress directory on the old server to the new server in the /var/www/html directory. Rsync will copy over the entire wordpress folder to the /var/www/html on the new server.

sudo rsync -avzP username@192.168.20.22:/var/www/html/wordpress /var/www/html

Wait for the files to transfer… if all the database and website content were transferred successfully, then you’re golden. Run the commands below to restore the database content into the blank database created above on the new server

sudo mysql -u root -p WP_database < /tmp/WP_database.sql

This should restore the database on the new server.

At this stage, the new server should have WordPress site content in the /var/www/html/wordpress and the database restored on the new server… Hopefully everything is working without issues.

You might want to use the same database and username with the correct password on both servers… (old and new) so you don’t have to reconfigured WordPress database connection settings.

Run the commands below to configure allow Nginx web server to own  the WordPress content.

sudo chown -R www-data:www-data /var/www/html/wordpress/
sudo chmod -R 755 /var/www/html/wordpress/

Restart Nginx web server.

sudo systemctl reload nginx.service

Step 11: Switch the Domain Name to Point to the new server

Finally, go to your DNS provider and switch or flip the domain name to point to the new server IP address. If everything is copied over correct and the correct usernames and password were used… the new server should begin serving WordPress content.

Wait a few before shutting down the old server.

Congratulations! You have successfully migrated WordPress to a new host

You may also like the post below:

Install phpBB on Ubuntu 17.04 / 17.10 with Apache2, MariaDB and PHP Support