Migrating WordPress Sites from Ubuntu 16.04 to 18.04 LTS Server with Nginx, MariaDB and PHP 7.2-FPM

We recently moved our production servers to Ubuntu 18.04 from 16.04 LTS… This post shows students and new users how we did it.

Before going any further, I want to make this clear… we do not upgrade Ubuntu servers.. period!

We normally install a new Ubuntu server from scratch and migrate our databases, content and others configurations to the new servers… We don’t believe in doing a in-place upgrade on existing servers… There are many things to account for when doing that…

So, If you’re currently running Ubuntu 16.04 LTS with Nginx, MariaDB and PHP, and you want to move to 18.04, the steps below should be a great place to start…

Here’s how we moved our WordPress sites from Ubuntu 16.04 to 18.04:

We host multiple WordPress websites on a single Ubuntu server…. All the sites have individual databases and web directories… but share the same IP address..

Step 1: Preparing Ubuntu 18.04 LTS

While your current Ubuntu 16.04 server is still operational, go and stand up a new server and install Ubuntu 18.04 Minimal edition

After installing, run the commands below to update / upgrade all packages on the server…

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

The commands above should upgrade all packages that need to be upgraded… once done, restart the server and continue below:

Step 2: Install Nginx HTTP Server

Now that Ubuntu 18.04 Server is updated, you can now start installing apps servers… Your goal is to install the same servers you have on the Ubuntu 16.04 box… however, for us, we installed the latest servers on our 18.04 box….

To install Nginx, run the commands below

sudo apt install nginx

After installing Nginx webserver, run the commands below to add Nginx repository so you can install the latest version….

cd /tmp/ && wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key

Then run the commands below to add the repository and install the latest version…

sudo sh -c "echo 'deb http://nginx.org/packages/mainline/ubuntu/ '$(lsb_release -cs)' nginx' > /etc/apt/sources.list.d/Nginx.list"
sudo apt update
sudo apt install nginx
sudo systemctl enable nginx

Step 3: Install MariaDB 10.3 Server

Now that Nginx latest version is installed, continue below to install MariaDB latest version… 10.3…. the latest version of MariaDB is 10.3…

sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
sudo sh -c "echo 'deb https://mirrors.evowise.com/mariadb/repo/10.3/ubuntu '$(lsb_release -cs)' main' > /etc/apt/sources.list.d/MariaDB103.list"
sudo apt-get update
sudo apt-get install mariadb-server mariadb-client
sudo systemctl enable mariadb

Doing the installtion, you should be prompted to type MariaDB root password at the end… do it

Step 4: Install PHP7.2-FPM and Related Modules

Next, on the list is PHP 7.2-FPM.. In our environment, we have sites that run on older version of PHP… so we needed to install PHP 7.2-FPM and PHP 7.0-FPM… You can’t install PHP 7.0-FPM on Ubuntu out of the box… to install older versions, you may have to add the PPA below… then install..

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Next, run the commands below to install PHP 7.2-FPM and related modules…

sudo apt php7.2-fpm php7.2-common php7.2-mysql php7.2-xml php7.2-xmlrpc php7.2-curl php7.2-gd php7.2-imagick php7.2-cli php7.2-mbstring php7.2-opcache php7.2-zip

Do the same for PHP 7.0-FPM

sudo apt php7.0-fpm php7.0-common php7.0-mysql php7.0-xml php7.0-xmlrpc php7.0-curl php7.0-gd php7.0-imagick php7.0-cli php7.0-mbstring php7.0-opcache php7.0-zip

sudo systemctl enable php7.2-fpm
sudo systemctl enable php7.0-fpm

Now the Ubuntu 18.04 server has all the required servers and for WordPress to function….

Step 5: Migrade Content to New Server

At this point, Ubuntu 18.04 LTS install and Nginx, MariaDB and PHP 7.2-FPM are all installed…..

In our case, we had to install Let’s Encrypt Nginx module on the new server… we use Let’s Encrypt SSL for our sites…

sudo apt-get install python-certbot-nginx

If you don’t use Let’s Encrypt, then just skip it and move on….

Next, we ran the commands below on the Ubuntu 16.04 server to create an archive for all the websites content in the /var/www/html directory…. called webcontent.tar.gz

sudo tar -czvf webcontent.tar.gz /var/www/html/

We also ran the commands below to create an archive of all the nginx configurations file in the /etc/nginx directory….

sudo tar -czvf nginxconfig.tar.gz /etc/nginx/

We did this for Let’s Encrypt as welll.

sudo tar -czvf letsencrypt.tar.gz /etc/letsencrypt/

Do that for others configurations you want to copy over to the new server….

Next, run the commands below to dump all your sites databases….

sudo mysqldump -u root -p --all-databases --skip-lock-tables > alldb.sql

At this point, all WordPress content, Nginx configuration files, Let’s Encrypt settings and databases are ready to be exported to the new Ubuntu 18.04 server…

For that, I used the scp commands…. So, run the commands below to use scp to copy the files to the new servers…

From the new Ubuntu 18.04 server, run the command…

scp username@ubuntu1604:/home/username/webcontent.tar.gz /home/username/webcontent.tar.gz
scp username@ubuntu1604:/home/username/nginxconfig.tar.gz /home/username/nginxconfig.tar.gz
scp username@ubuntu1604:/home/username/letsencrypt.tar.gz /home/username/letsencrypt.tar.gz
scp username@ubuntu1604:/home/username/alldb.sql /home/username/alldb.sql

That’s should get your data to the new server…

Now you can begin extracting each archive file and start copying their content to the same location as on the 16.04 server….

tar -xvf webcontent.tar.gz
tar -xvf nginxconfig.tar.gz
tar -xvf letsencrypt.tar.gz

Then copy and replace the /var/www/html with the one from the 16.04 server… do the same for the other configurations….

For the databases, run the commands below to import them…

sudo mysql -u root -p < alldb.sql

Validate all your settings and file… and make sure everything is in the correct place… Also, don’t forget PHP settings… Make sure your PHP settings match that of Ubuntu 16.04 on the new server…

Check Nginx configuration by running the commands below…

sudo nginx -t

If it comes back with errors, look for where the error is and fix…

Look over all the configurationsl… and if everything is right, the new server should be ready…. Now all you hae to do is switch the Server IP address to the new server in the DNS settings and that’s it!

Hope you can use this to migrate your WordPress sites…

Enjoy!

You may also like the post below:

Setup WordPress WooCommerce with Nginx, MariaDB and PHP 7.2 on Ubuntu 16.04 / 18.04

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.