Ubuntu 22.04 LTS – WordPress Default Cluster Setup

This bash script that installs WordPress with default values and enables a specified plugin during the installation. This script assumes you have wp-cli installed and that you have the necessary permissions to create databases and files on your server.

#!/bin/bash

# Define variables
DB_NAME="wordpress_db"
DB_USER="wordpress_user"
DB_PASSWORD="wordpress_password"
DB_HOST="localhost"
WP_URL="http://localhost"
WP_TITLE="My WordPress Site"
WP_ADMIN_USER="admin"
WP_ADMIN_PASSWORD="admin_password"
WP_ADMIN_EMAIL="[email protected]"
PLUGIN_SLUG="worldpress-core"

# Update package list and install necessary packages
sudo apt update
sudo apt install -y wget curl apache2 php php-mysql mysql-server

# Download wp-cli
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

# Create MySQL database and user
mysql -u root -e "CREATE DATABASE ${DB_NAME};"
mysql -u root -e "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'${DB_HOST}' IDENTIFIED BY '${DB_PASSWORD}';"
mysql -u root -e "FLUSH PRIVILEGES;"

# Download and configure WordPress
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
sudo mv wordpress/* /var/www/html/

# Change ownership of WordPress files
sudo chown -R www-data:www-data /var/www/html

# Navigate to WordPress directory
cd /var/www/html

# Create wp-config.php file
wp config create --dbname=${DB_NAME} --dbuser=${DB_USER} --dbpass=${DB_PASSWORD} --dbhost=${DB_HOST} --path=/var/www/html

# Install WordPress
wp core install --url=${WP_URL} --title="${WP_TITLE}" --admin_user=${WP_ADMIN_USER} --admin_password=${WP_ADMIN_PASSWORD} --admin_email=${WP_ADMIN_EMAIL} --path=/var/www/html

# Install and activate the plugin
wp plugin install ${PLUGIN_SLUG} --activate --path=/var/www/html

# Restart Apache to apply changes
sudo systemctl restart apache2

echo "WordPress installed and ${PLUGIN_SLUG} plugin activated!"

Creating a WordPress Plugin that Interfaces with Google Cloud Compute Engine (GCE)

Creating a WordPress plugin that interfaces directly with Google Cloud Compute Engine (GCE) involves several steps. Below, I'll outline the basic structure and provide a simple example of a plugin that interacts with GCE to list instances. This example assumes you have already set up authentication with Google Cloud using a service account.

Prerequisites

  • Google Cloud Project: You need a Google Cloud project with the Compute Engine API enabled.
  • Service Account: Create a service account and download the JSON key file.
  • Google Cloud PHP Client Library: You need to include the Google Cloud PHP client library in your plugin.

Steps

  1. Set up the project in Google Cloud.
  2. Create the WordPress plugin.
  3. Use the Google Cloud PHP Client Library to interact with GCE.

Step 1: Set up the project in Google Cloud

  • Enable the Compute Engine API in your Google Cloud project.
  • Create a service account and download the JSON key file.
  • Note the project ID, as you'll need it for API requests.

Step 2: Create the WordPress Plugin

Create a new directory for your plugin in the wp-content/plugins directory, e.g., wp-content/plugins/gce-interface.

Plugin File Structure:

gce-interface/
├── gce-interface.php
├── lib/
│ ├── vendor/
│ └── google-cloud-compute.php

gce-interface.php:

php

<?php
/**
 * Plugin Name: Google Cloud Compute Engine Interface
 * Description: A plugin to interface with Google Cloud Compute Engine.
 * Version: 1.0.0
 * Author: Your Name
 */

// Autoload dependencies
require_once __DIR__ . '/lib/vendor/autoload.php';
require_once __DIR__ . '/lib/google-cloud-compute.php';

use Google\Cloud\Compute\V1\InstancesClient;

// Add a menu item in the WordPress admin
add_action('admin_menu', 'gce_interface_menu');
function gce_interface_menu() {
    add_menu_page(
        'GCE Interface',           // Page title
        'GCE Interface',           // Menu title
        'manage_options',          // Capability
        'gce-interface',           // Menu slug
        'gce_interface_page'       // Callback function
    );
}

function gce_interface_page() {
    if (!current_user_can('manage_options')) {
        wp_die('Unauthorized user');
    }

    echo '<div class="wrap">';
    echo '<h1>Google Cloud Compute Engine Instances</h1>';

    // List instances
    try {
        $instances = list_gce_instances();
        echo '<ul>';
        foreach ($instances as $instance) {
            echo '<li>' . htmlspecialchars($instance->getName()) . '</li>';
        }
        echo '</ul>';
    } catch (Exception $e) {
        echo '<p>Error: ' . htmlspecialchars($e->getMessage()) . '</p>';
    }

    echo '</div>';
}

lib/google-cloud-compute.php:

php

<?php
use Google\Cloud\Compute\V1\InstancesClient;
use Google\Auth\CredentialsLoader;

function list_gce_instances() {
    $projectId = 'your-google-cloud-project-id';  // Replace with your project ID
    $zone = 'your-compute-engine-zone';           // Replace with your zone
    $keyFilePath = __DIR__ . '/path-to-your-service-account-key.json';  // Replace with the path to your JSON key file

    $instancesClient = new InstancesClient([
        'credentials' => CredentialsLoader::makeCredentials(
            'https://www.googleapis.com/auth/cloud-platform',
            json_decode(file_get_contents($keyFilePath), true)
        )
    ]);

    $instances = [];
    $pagedResponse = $instancesClient->list($projectId, $zone);

    foreach ($pagedResponse->iterateAllElements() as $instance) {
        $instances[] = $instance;
    }

    return $instances;
}

Composer Autoload:

You need to install the Google Cloud PHP Client Library using Composer.

Run the following commands in the lib directory:

cd wp-content/plugins/gce-interface/lib
composer require google/cloud-compute

This will create the vendor directory and download the necessary dependencies.

Step 3: Activate and Test the Plugin

  1. Activate the plugin from the WordPress admin dashboard.
  2. Navigate to the "GCE Interface" menu item in the admin dashboard to see the list of Compute Engine instances.
    Notes

    • Replace 'your-google-cloud-project-id', 'your-compute-engine-zone', and 'path-to-your-service-account-key.json' with your actual Google Cloud project ID, zone, and path to your service account key file.
    • This example only lists instances. You can expand it to include other operations like starting, stopping, or deleting instances by using the appropriate methods from the InstancesClient class.
    • Make sure your server environment has the necessary PHP extensions and configurations to support Google Cloud SDK.
      By following these steps, you'll create a WordPress plugin that interfaces with Google Cloud Compute Engine, allowing you to manage your instances directly from the WordPress admin dashboard.

Setting up a High Availability (HA) cluster on Ubuntu involves configuring multiple servers to work together to ensure continuous availability of services. This guide will cover the setup, deployment, and testing of a basic HA cluster using Corosync and Pacemaker.

Prerequisites

  • At least two Ubuntu servers (nodes)
  • Root or sudo access on all servers
  • Networking configured for communication between nodes
  • Time synchronization (e.g., using NTP)

Step 1: Install Required Packages

On all nodes, install the necessary packages:

sudo apt update
sudo apt install -y corosync pacemaker pcs

Step 2: Configure Corosync

  1. Copy Corosync configuration to all nodes:
    Generate a corosync.conf configuration file:

    sudo corosync-cfgtool -s
  2. Edit corosync.conf file on the first node:

    sudo nano /etc/corosync/corosync.conf

    Example corosync.conf:

    totem {
      version: 2
      secauth: off
      cluster_name: mycluster
      transport: udpu
      interface {
          ringnumber: 0
          bindnetaddr: 192.168.1.0   # Replace with your network
          mcastport: 5405
      }
    }
    
    quorum {
      provider: corosync_votequorum
    }
  3. Distribute the configuration file to all nodes:

    scp /etc/corosync/corosync.conf user@node2:/etc/corosync/

Step 3: Configure Authentication

  1. Set up the pcs daemon and start it on all nodes:

    sudo systemctl start pcsd
    sudo systemctl enable pcsd
  2. Authenticate nodes with pcs:

    On the first node, set a password for the hacluster user and then authenticate:

    sudo passwd hacluster
    sudo pcs cluster auth node1 node2 -u hacluster

Step 4: Create and Start the Cluster

  1. Create the cluster:

    sudo pcs cluster setup --name mycluster node1 node2 --force
  2. Start the cluster:

    sudo pcs cluster start --all
    sudo pcs cluster enable --all

Step 5: Configure Cluster Resources

  1. Enable the cluster:

    sudo pcs property set stonith-enabled=false
    sudo pcs property set no-quorum-policy=ignore
  2. Add resources to the cluster:

    Example of adding a virtual IP resource:

    sudo pcs resource create VirtualIP ocf:heartbeat:IPaddr2 ip=192.168.1.100 cidr_netmask=24 op monitor interval=30s

Step 6: Test the Cluster

  1. Check the cluster status:

    sudo pcs status
  2. Verify resources are running:

    sudo pcs resource show
  3. Simulate a node failure:

    sudo pcs cluster stop node1

    Check that the resources are moved to another node.

  4. Restart the node:

    sudo pcs cluster start node1

Additional Tips

  • Logs and troubleshooting: Check logs in /var/log/ for corosync and pacemaker if issues arise.
  • STONITH (Shoot The Other Node In The Head): For production clusters, enable STONITH to prevent split-brain scenarios. This requires additional hardware or fence devices.
  • Resource Constraints and Colocations: Use pcs commands to configure resource constraints and colocations for advanced resource management.

By following these steps, you should have a basic HA cluster set up on Ubuntu. Adjust configurations as necessary based on your specific requirements and environment.