The easiest and fastest way to migrate wordpress blog with LetsEncrypt SSL certificate to GCP

2020年05月22日 753Browse 3Like 0Comments

Dockerize Wordpress

Estimated Time

30 minutes - one hour if:

  • be confortable with command line and GCP console
  • everything goes smoothly

Tools

  1. Wordpress plugin: UpdraftPlus
  2. Docker and docker compose
  3. Git

Steps

Part 1: Backup data on the original blog machine

  1. Login to wordpress blog admin page and install UpdraftPlus plugin;
  2. Use UpdraftPlus to backup all the data of wordpress blog, then download to local drive(coz I don't use the cloud drive);

Part 2: Create and setup a GCP VM instance

  1. Login to the GCP web console
  2. Create a VM instance, set it to allow HTTP/HTTPS traffic, use static IP address;
  3. Copy computer's SSH public key content in file ~/.ssh/id_rsa.pub (generated by command ssh-keygen), add it to VM instance SSH Keys;
  4. Use ssh -i ~/.ssh/id_rsa username@vm_static_ip to login to the GCP VM instance(or via the web console's SSH Connect feature open in a browser window);
  5. Install and configure git, docker, docker compose;

Part 3: Update domain IP mapping from DNS provider(mine is GoDaddy)

  • Change the domain type A value to GCP instance static IP from the DNS provider website;

Part 4: Install LEMP stack and WordPress then restore on GCP VM instance

  1. git clone https://github.com/mjstealey/wordpress-nginx-docker to gcp instance. Read the document to get a general idea of the customized lemp containers before doing the following;

  2. Edit the Dockerfile docker-compose.yml to setup mysql and wordpress user/password:

    • cp .env_example .env
    • vim docker-compose.yml
  3. Use the shortcut way to install LEMP stack with wordpress package:

    • sudo -i
    • docker-compose up -d # run it in the cloned repository's root directory
  4. Open http://vm-instance-static-ip in the browser to setup and restore a new wordpress site:

    • Install a new wordpress site(don't worry about the settings);
    • Install UpdraftPlus plugin and restore the blog with the backup data;
  5. Change default Nginx virtual host configuration to support SSL in nginx directory:

    • mv default.conf default.conf.bakcup
    • mv default_https.conf.template default.conf
    • vim default.conf: modify the FQDN_OR_IP string to the real domain name;
  6. Use bash script in letsencrypt directory to generate SSL certificates:

    • ./letsencrypt-init.sh my-domain

      This will remove the containers created at the second step.

  7. Re-execute: docker-compose up -d to create containers once again. The new containers use the data and configurations generated in previous steps.

Now, all migration work is done! Use the domain to view and check the new site.

Part 5: Windup

  • Set up a task in crontab to renew Let'sEncrypt SSL certificate every 2 months.
    crontab -e
    0 0 1 1-12/2 * /path/letsencrypt/letsencrypt-renew.sh

Conclusion

  1. Use root priviledge to run docker-compose command and other shell scripts;
  2. I run into failure of limitation of file size is 2M when use UpdraftPlus to upload backup zip archives . Alternative ways:
    • modify wordpress files to increase the minimal size for uploading, I didn't use this way;
    • backup and restore wordpress files via cloud drives(eg. google drive) when using UpdraftPlus;
    • use scp command to copy the files into UpdraftPlus plugin path (wordpress-nginx-docker/wordpress/wp-content/updraft) to restore (The way I choose);
  3. By choosing wordpress plugin UpdraftPlus and the shortcut way to dockerize wordpress and lemp stack, we can avoid many separate customization settings.

Reference

Sunflower

Stay hungry stay foolish

Comments