Moving A Public Website (Usually Production) (Ubuntu 20.10)

Scenario

This tutorial is about moving a public website to another server but keeping the same domain name.

This is usually referring to a production site because development and test sites generally include the name of the server in their domain name, so you may move the data, you wouldn't keep the same domain name in those scenarios.

Domains

You need to know the primary domain name of the web server you are moving the site to:

webserver1.mattifesto.com

You need to know the domain of the website:

mattifesto.com

When you start this process the website domain will point somewhere other than the new web server, by the time we are done it will have a DNS CNAME record that points to the new web server.

Prepare The Source Website For Transfer

  • Go to the source website.

  • Go to Develop > Update in the admin area.

  • Click the "Backup Database" button.

  • Future: Place the site into maintenance mode. This will prevent any further significant database changes from occurring.

  • Change the DNS A record for the website domain to the IPv4 address of the destination web server.

Setup GitHub SSH

2021_04_04

These instructions may be needed but I do not need them today. They should be reviewed.

If you have private GitHub repositories you will need to authenticate with GitHub using SSH keys. You should generate keys for each server but save them on your development machine so that you can copy them each time you recreate the virtual machine for that server rather than creating new keys every time.

This link tells you more about this process.

https://docs.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

If you have your keys on your dev machine but just created a new virtual machine these instructions help you copy them to the new VM.

In MacOS terminal signed in to Mac:

  • $ scp -r <key directory> webserver1:~

In MacOS terminal sign in to server VM:

  • move keys to .ssh directory

  • $ eval "$(ssh-agent -s)"

  • $ ssh-add <key name>

Create Website Directory On The New Web Server

  • Navigate to the folder the contains all your website directories.

  • I use the reverse domain name for my website directories, but you can use whatever naming scheme you like.

  • $ mkdir -p com_mattifesto/logs

  • $ cd com_mattifesto

  • $ mkdir document_root

  • $ cd document_root

  • $ echo "<?php echo 'site is being upgraded';" > index.php

In time, we will improve this process so that the site looks the same during transition.

Create Virtual Host

Reference:

https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-20-04#step-4-—-creating-a-virtual-host-for-your-website

  • $ sudo vi /etc/apache2/sites-available/com_mattifesto.conf

    <VirtualHost *:80>
    
        ServerName      mattifesto.com
    
        ServerAdmin     matt@mattifesto.com
        DocumentRoot    /home/mattcalkins/www/com_mattifesto/document_root
        ErrorLog        /home/mattcalkins/www/com_mattifesto/logs/error.log
        CustomLog       /home/mattcalkins/www/com_mattifesto/logs/access.log combined
    
        <Directory "/home/mattcalkins/www/com_mattifesto/document_root">
            AllowOverride   all
            Require         all granted
        </Directory>
    
    </VirtualHost>
    
  • $ sudo a2ensite com_mattifesto

  • $ sudo apache2ctl configtest

  • $ sudo systemctl reload apache2

  • Test site to make sure it works

  • Reference: https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-20-04

  • certbot should already be installed

  • $ sudo certbot --apache

    Follow instructions.

  • Test site with https to make sure it works.

Create Database

One of the principles of the Mattifesto Method is using unique identifiers which makes searching and potentially replacing much easier. The database is named "com_mattifesto_database" because naming it "mattifesto" would create a name overlap in many places.

  • $ sudo mysql

  • mysql> create database com_mattifesto_database;

  • mysql> create user 'web'@'localhost' identified with mysql_native_password BY '********';

  • mysql> grant all on com_mattifesto_database.* to 'web'@'localhost';

  • mysql> flush privileges;

  • mysql> exit;

  • $ mysql -u web -p

  • mysql> show databases;

  • mysql> exit;

Copy Repository

  • $ cd ..

  • $ rm -rf document_root

  • delete document root directory

  • $ rsync -a <username>@<source_web_server_domain>:~/<document_root>/ document_root

    Notice slash after directory name on source, not slash on directory destination.

Set Up The Website

  • $ cd www/com_mattifesto/document_root/

  • $ cp tmp/database-backups/<most recent backup file> import.sql

  • $ vi import.sql

  • :%s/<old_database_name>/com_mattifesto_database/gc

    This moment is where it really helps if the database names are unique like we mentioned at the beginning of this document.

  • $ mysql -u web -p < import.sql

  • $ vi colby-configuration.php

  • Manually update the constant values for for the new website.

  • On your workstation browser, navigate to:

    https://mattifesto.com/admin/

    Your password is the same as it was for the copied site.

    Go to the update admin page for the website you are copying and click on the "Update Site" button to create the tables that aren't exported in database backups.

  • $ rm import.sql