Migrating a WordPress website

I was running my website on a DigitalOcean Droplet and I wanted to migrate it to a new server. When I first set it up I just used the ready-made One-Click-Wordpress server, which was using Ubuntu and ran a script that had installed and prepared everything for me.

I decided to lower my monthly costs and spin up a new server with lower cost and memory. In this post I will describe the steps I followed.

I chose to use a 6€ per month droplet with a Debian 12 server. It only has 1GB of RAM and a 25GB SSD, but I don’t need any more than that.

Note: I am using SSH to connect to the server and my user is root. If you’re following along and you’re not using the root account, then you need to preface all the commands with sudo for them to run.

Exporting the old WordPress site

The first step we need to do is export the data from our old server. In my case I have a very light WordPress installation with a theme and just a few plugins residing in /var/www/lepuri. The first thing we need to do is create a ZIP file.

zip -r lepuri.zip /var/www/lepuri

But we still need to get a dump of the database. For that we run:

mysqldump -u your_db_username -p your_wordpress_db_name > wordpress_db.sql

Just replace your_db_username and your_wordpress_db_name with your actual username and database name, and enter the password when prompted.

These 2 files are what we need. What I did was copy them over to my local system and later I will send them to the new server.

Preparing the environment

First things first, we need to update the repositories and any outdated software. For this we run:

apt update && apt-upgrade

Once that is done we need to install some packages for WordPress to run. If you just do a normal PHP install then it will install PHP version 8.0, but Debian 12 also has the repositories for PHP 8.2 so I’m going to use that.

Note: If you don’t want to use PHP 8.2 and want to stick with version 8.0 then just remove the “8.2” from the following commands.

Optional: Setting up a swap file

Since my server only has 1GB of RAM and I will use it for other services as well, I decided to set up a swap file to increase it’s capacity. A swap file is a section of memory that your PC/server can use to store things in memory when it runs out of RAM. The downside is that it is much slower than using the RAM. This is totally not necessary and you don’t have to do this step.

fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile

To verify that the swap file was created you can run:

swapon -s

In the output of this command you will also see a UUID. Copy that cause we’ll need it for the next step, where we make the swap file permanent by adding it to the /etc/fstab file. Open this file with any editor and add the following line:

your_UUID /swapfile none swap sw 0 0

For WordPress to run we need to install PHP and MySQL. You can choose between MySQL and MariaDB, they are essentially the same and interchangable in this project.

apt install php8.2 php8.2-fpm php8.2-mysql php8.2-curl php8.2-gd php8.2-intl php8.2-mbstring php8.2-soap php8.2-xml php8.2-xmlrc php8.2-zip

To install MySQL we first need to add the repositories, like this:

wget https://dev.mysql.com/get/mysql-apt-config_0.8.33-1_all.deb
dpkg -i mysql-apt-config_0.8.33-1_all.deb

And then update again to see them:

apt update

Now you can install the mysq-server package:

apt-install mysql-server

And remove the downloaded file since we don’t need it anymore:

rm mysql-apt-config_0.8.33-1_all.deb

Now you can install MySQL by running:

mysql_secure_installation

Finally, we also need a web server for our website to run. I am using Apache2 for this. Later on, we will configure it to set up virtual hosts and also SSL.

apt install apache2 libapache2-mod-php

At this point you should have all the necessary packages installed. The most important ones are:

  • PHP (either version 8.0 or 8.2).
  • PHP-FPM
  • MySQL (or MariaDB, depending on your preference).
  • Apache2 (you can also use Nginx but I’m not covering that in this tutorial)

Now we need to get our services running.

systemctl enable --now apache2
systemctl enable --now php8.2-fpm
systemctl enable --now mysql

You can check if they’re running by doing systemctl status SERVICE for example systemctl status apache2. If everything is running then we can proceed with the next step.

Importing the WordPress site

In the first step, where we exported the old website, we got 2 files, namely: lepuri.zip and wordpress_db.sql. You should now transfer these files to the new server. I used SFTP, but you can use any method you want for the transfer.

First of all we will unzip the lepuri.zip file and move it to the /var/www directory:

Note: If you don’t have unzip installed on your system then you need to run apt install unzip.

unzip lepuri.zip
mv lepuri/ /var/www/lepuri

Since we’re using Apache2, that means the user www-data was created on our system. We need to give ownership of the files to this user.

chown -R www-data:www-data /var/www/lepuri

After that we need to import the database, but before we do that, we first need to create the same database and user that was running on the old server. You can see these details if you open the wp-config.php file located on the root folder of your project, for example /var/www/lepuri/wp-config.php. There you’ll see something like this:

define( 'DB_NAME', 'your_db_name' );
define( 'DB_USER', 'your_db_user' );
define( 'DB_PASSWORD', 'your_db_password' );

To do this you first need to log in to MySQL:

mysql -u root -p

Once logged in you run the following commands to set up a database and create a user with all the necessary permissions:

Note: The quotation marks in the following SQL code are necessary.

CREATE DATABASE your_db_name;
CREATE USER 'your_db_user'@'localhost' IDENTIFIED BY 'your_db_password';
GRANT ALL PRIVILEGES ON your_db_name.* TO 'your_db_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Once that’s done you can import the old database into this one.

mysql -u root -p your_db_name < wordpress_db.sql

And that’s it! We have successfully imported the old WordPress site and data. For the last part we’re going to enable the Apache2 virtual host and set up SSL.

Creating a virtual host

Apache2 holds all the virtual host data in a directory called /etc/apache2/sites-available/, and then it creates symlinks to /etc/apache2/sites-enabled/.

Note: This is only for Debian based distros. On RedHat distros the Apache2 user and paths are different.

We’re going to create a configuration file for our website. In my case I will call it lepuri.conf and I will place it at /etc/apache2/sites-available/lepuri.conf. This is the code it contains:

<VirtualHost *:80>
    ServerName lepuri.net
    ServerAlias lepuri.net
    DocumentRoot /var/www/lepuri

    <Directory /var/www/lepuri>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The important parts are the ServerName, which you should set to your website’s domain, and the DocumentRoot and Directory, which you should set to your website’s path.

On Debian systems Apache2 has some useful commands to help us enable mods and sites. The following activates Apache’s Rewrite engine and enables our website:

a2enmod rewrite
a2ensite lepuri.net

Then we just need to restart it.

systemctl restart apache2

If you now visit your website’s domain (in my case http://lepuri.net), you will see your website running. Note that we’re visiting the HTTP version, not HTTPS, as we still don’t have an SSL certificate.

Enabling HTTPS / SSL

The last step is to enable SSL for secure HTTP traffic. We will use Let’s Encrypt to get a free certificate. Fortunately, we don’t have to do all the steps by hand, since there’s a very useful tool called certbot that will do everything for us. To install it we run:

apt install certbot python3-certbot-apache

This has installed the certbot tool together with some useful configurations specifically for Apache. Adding SSL encryption is now very simple:

certbot --apache -d lepuri.net

Note: If this is the first time you run certbot it might ask you for an email to notify you when the certificate is about to expire. If you don’t want to provide an email and want to instead skip this step, you should run it with the following flag:

certbot --apache -d lepuri.net --register-unsafely-without-email

This wil now have created a new file under /etc/apache2/sites-available/, namely lepuri-le-ssl.conf. This file contains all the necessary code for finding and using your SSL certificate.

As a last step, let’s make our website always redirect to the HTTPS version. For this we need to edit our /etc/apache2/sites-available/lepuri.conf file and just add a new line.

Note: The .conf file might not look exactly like before, since there might be some new lines added to the bottom by Apache’s rewrite engine.

<VirtualHost *:80>
    ServerName lepuri.net
    ServerAlias lepuri.net
    DocumentRoot /var/www/lepuri

    # ADD THIS LINE
    Redirect permanent / https://lepuri.net
    ###############

    <Directory /var/www/lepuri>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

And now all you need to do is restart the Apache server:

systemctl restart apache2

Note: Don’t forget to change your domain’s A records to point to the new server’s IP address!

And we’re done! We have successfully migrated our website to a new server! If you now visit yourdomain.com you will see the website running as normal.

«