Migrating a WP server

Get the original files

  1. On the original server open the Duplicator plugin
  2. Create a new package and change the file name to the current date
  3. Download both files - installer.php and the archive.zip

Prepare the new server

  1. Get the current php and mysql versions

    php -v
    # PHP 7.4.30
    mysql --version
    # mysql  Ver 8.0.19

  2. Install PHP

    # Add PHP repository
    sudo add-apt-repository ppa:ondrej/php
    sudo apt update
    
    # Install PHP 7.4 and required extensions
    sudo apt install php7.4 php7.4-cli php7.4-common php7.4-curl php7.4-mbstring php7.4-mysql php7.4-xml php7.4-zip php7.4-fpm libapache2-mod-php7.4
    
    # Verify installation
    php -v

  3. Install MySQL

    # Add MySQL repository
    sudo apt install software-properties-common
    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 467B942D3A79BD29
    sudo add-apt-repository 'deb http://repo.mysql.com/apt/ubuntu focal mysql-8.0'
    sudo apt update
    
    # Install MySQL 8.0
    sudo apt install mysql-server
    
    # Verify installation
    mysql --version

  4. Install apache2

    sudo apt install apache2
    
    sudo systemctl status apache2

  5. Install other utilities

    sudo apt install unzip
    sudo apt install certbot python3-certbot-apache

Upload files and configure Apache2

  1. Upload the archive.zip to this location (this might take a while):

    scp /Users/marko/Downloads/archive.zip root@<new_domain>:/root
  2. Create the new website directory: sudo mkdir -p /var/www/<new_domain>

  3. Unzip the files

    sudo unzip /root/archive.zip -d /var/www/<new_domain>
  4. Upload the installer file to the new directory

    scp /Users/marko/Downloads/installer.php root@<new_domain>:/var/www/<new_domain>
  5. Change the ownership and permissions of the files:

    sudo chown -R www-data:www-data /var/www/<new_domain>
    
    sudo find /var/www/<new_domain> -type d -exec chmod 755 {} \;
    sudo find /var/www/<new_domain> -type f -exec chmod 644 {} \;
  6. Go to the original server and copy the contents of /etc/apache2/sites-available/<old_domain>

  7. Connect to the new server and create a file /etc/apache2/sites-available/<new_domain> and copy the contents of the file from the old server into it
    # Enable the site
    sudo a2ensite <new_domain>.conf
    
    # Enable required modules
    sudo a2enmod rewrite
    sudo a2enmod headers
    
    # Test the configuration
    sudo apache2ctl configtest
    
    # Restart Apache
    sudo systemctl restart apache2

Create the database

  1. sudo mysql -u root
  2. Run the following commands and replace where necessary:

    # Create the database
    CREATE DATABASE <DB_NAME>;
    
    # Create the user
    CREATE USER '<DB_USER>'@'localhost' IDENTIFIED BY '<DB_PASSWORD>';
    
    # Grant privileges to the user
    GRANT ALL PRIVILEGES ON <DB_NAME>.* TO '<DB_USER>'@'localhost';
    
    # Apply the changes
    FLUSH PRIVILEGES;
    
    # Exit MySQL
    EXIT;

Install the website from the archive

  1. Open <new_domain>/installer.php in your browser and run the installer by clicking Validate and then Next
  2. Go to Settings > Permalinks and save (to refresh permalink structure)
  3. Clear the browser cache and test that everything works like before
  4. Remove duplicator installation files in the Duplicator settings

Optional: migrating domains

If you want to rename a WP server's domain do the following steps:

  1. Install wp-cli

    curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
    
    # verify the file
    php wp-cli.phar --info
    
    # make it executable
    chmod +x wp-cli.phar
    
    # move it to a Directory in Your PATH
    sudo mv wp-cli.phar /usr/local/bin/wp
    
    # test the installation
    wp --info
  2. Change database entries using the wp command

    sudo -u www-data wp --path=/var/www/<new_domain> search-replace 'http://<old_domain>' 'https://<new_domain>' --all-tables
  3. Update wp-config.php:

    define('WP_HOME', 'https://<new_domain>');
    define('WP_SITEURL', 'https://<new_domain>');
  4. Rename folder in /var/www/<old_domain> to /var/www/<new_domain>

  5. Update the apache2 config

    mv /etc/apache2/sites-enabled/<old_domain>.conf /etc/apache2/sites-enabled/<new_domain>.conf
  6. Update the domain inside of the config sudo nano <new_domain>.conf

  7. Clear caches & reload apache

    sudo -u www-data wp cache flush
    sudo systemctl reload apache2

Enable https on the server

  1. Install Certbot with the following commands:

    sudo apt update
    sudo apt install python3-pip python3-dev python3-setuptools python3-venv
    
    python3 -m venv /opt/certbot
    source /opt/certbot/bin/activate
    
    pip install --upgrade pip
    pip install certbot certbot-apache
    sudo ln -s /opt/certbot/bin/certbot /usr/local/bin/certbot
    
    # verify install
    certbot --version

  2. Change the DNS record for the new domain to point to the new server

  3. Run the following command to create https certificates using certbot:

    sudo certbot --apache -d <new_domain> -d www.<new_domain>

  4. Run this command to check if automatic certificate renewal works:

    sudo certbot renew --dry-run

Testing if everything works

  1. Open tail -f /var/log/apache2/<new_domain>.access.log on the new server
  2. Open the website in your browser and check if the access log updates

Troubleshooting

Assets are loading over HTTP instead of HTTPS

  1. Check the wp-config.php file and validate that the domain contains https and not just http!
  2. Install the Better Search Replace plugin and replace all http://<old_domain> with https://<new_domain>