Recently I decided to create a personal blog on WordPress and chose DigitalOcean as the hosting, as the most suitable for me in terms of price and features. I did not work with this VPS earlier, so I decided to write this post, thus creating for myself a small cheat sheet, which I or anyone could use in the future. In this article I will try to describe in detail how to migrate WordPress blog to DigitalOcean server.

The transfer will consist of 5 steps:

  1. Registration in DigitalOcean and creating a droplet
  2. Server and firewall setup, creating a new user
  3. Install and configure Nginx, PHP, MySQL
  4. Domain transfer and installation of SSL certificate
  5. Uploading WordPress to the server and deploy the project

Registration in DigitalOcean and creating a droplet

First you need to register with DigitalOcean, if you do this via this link, you will receive $ 100 in credit over 60 days.

After that, you need to create a new project by clicking on the appropriate button. Go into this project and click on the button Create -> Droplets.

In this article, we will consider server configuration using Ubuntu 18.04 as an example, so I suggest choosing this from the possible options. Next, choose a tariff plan. For a simple blog, the most minimal plan is suitable, if you have a more serious project, then it is better to take a more powerful virtual machine. Then select the datacenter region, try to choose the one that is closest to your target audience.

creating a droplet

Now choose an authentication method. The most secure is logging in using SSH keys. You can read about creating an SSH key in Windows here, on MacOS or Linux here. If it is already on your computer, then click on the “New SSH Key” button and paste the public key into the appropriate field.

Next, select additional settings if you need them and enable the creation of automatic backups if you do not plan to create them manually. Everything is ready, click on the “Create Droplet” button.

Server and firewall setup, creating a new user

Before starting the server setup, create a new Firewall for it. To do this, go to the Networking page, the Firewalls tab and click on the “Create Firewall” button.

Creating Firewall in DigitalOcean

Name the firewall. You can leave Outbound Rules by default, and in Inbound Rules add the settings for http and https, as follows:

new firewall

Below in “Apply to Droplets”, start typing the name of your droplet (you can find it on the droplets page) and select it. After these steps click on “Create Firewall”.

Now that the droplet and firewall have been created for it, on the droplets page, see what ip address is assigned to it. Using this ip, you can connect to the server via ssh. Now you need to create a Non-root User on your server. You can use this tutorial for this. You only need to perform steps 1, 2 and 3 from it (log in as root, create a new user and give him administrative privileges). Step 4 is not necessary, because we already configured the Firewall in the DigitalOcean panel and if you configure it again, they can conflict.

Install and configure Nginx, PHP, MySQL

First of all, on a new droplet, you need to configure a web server. We will install Nginx. Configuring it for your site is quite simple by following this instruction (you will only have to skip step 3 in it, as we already configured the firewall).

Now, following this instruction we need to install PHP 7.4 FPM for Nginx or update the version of PHP if you already have it installed. Now we just have to install MySQL on Ubuntu 18.04, you can read how to do this at this link.

Domain transfer and installation of SSL certificate

In order to associate your domain with a server, you will need to specify the ip address of the created droplet in the DNS settings of the domain registrar.

With a Namecheap registrar, this is done as follows. In Domain List -> Advanced DNS you need to add A Record with the server ip address.

Domain transfer to DigitalOcean

Changing the DNS can take several hours. When changing is over, you can enter your domain in a browser and you will see a standard Nginx page on your server. Now we need to install a free Let’s Encrypt certificate for the site. This is easy enough to do by following this guide. If you do everything right, then your site’s settings in /etc/nginx/sites-enabled/ will look something like this:

server {
        root /var/www/andreyblog.com/www;
        index index.php index.html index.htm;

        server_name andreyblog.com www.andreyblog.com;

        gzip on;
        gzip_disable "msie6";
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

        location ~ /\. {
                deny all;
        }

        location ~* /(?:uploads|files)/.*\.php$ {
                deny all;
        }

        location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
                access_log off;
                log_not_found off;
                expires max; 
        }

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/andreyblog.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/andreyblog.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = www.andreyblog.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    if ($host = andreyblog.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
        listen 80 ;
        listen [::]:80;
        server_name andreyblog.com www.andreyblog.com;
    return 404; # managed by Certbot
}

Uploading WordPress to the server and deploy the project

The last step before launching the site is to upload your local WordPress project to the server and configure it.

To copy the “project1” folder from the local computer to the /var/www directory on the server via SSH, you will need to use the SCP command. To do this, go to the directory where the above folder is located and run the command:

scp -r project1 user@server:/var/www

Here, instead of “user”, enter the name of the user you created in the second part of the article, and instead of “server”, enter the ip address of your droplet.

After copying, the project will appear on your server.

Now export your local database to sql file and copy it to the server in a temporary folder, for example in /var/www/temp :

scp db.sql user@server:/var/www/temp

Go to this folder on the server and run the command that imports your database dump into MySQL:

mysql -u username -p user_pass db_name1 < db.sql

Here in “username” enter the user that you created when you installed MySQL, and “user_pass” – his password. “db_name1” is the name of your database on the server, and “db.sql” is the name of your dump that you uploaded to the server.

One step left to deploy your Worpress site on the server. Go to your project directory and open the wp-config.php file and change the following lines:

define( 'DB_NAME', 'db_name1' );
define( 'DB_USER', 'user1' );
define( 'DB_PASSWORD', 'pass1' );

In place of “db_name1” write the name of your database on the server, instead of “user1” – your MySQL user, and “pass1” – his password.

Save the file and that’s it!

Now you can be congratulated, the transfer WordPress blog to the DigitalOcean server has been successfully completed.

Share post
Twitter Facebook