Optimized Shrunken Ubuntu Setup Script for Automated WordPress Multisite and MySQL

This script automates the setup process for a server, including the following key steps:

  1. Enables root auto-login: Configures the system to log in automatically as the root user.
  2. Updates and installs packages: Installs necessary software, including development tools (like g++, Perl, and Python), ROS (Robot Operating System), web server packages (Apache, PHP, and MySQL), and networking tools.
  3. Installs and configures MySQL: Sets up MySQL in safe mode to reset the root password and creates a wpuser MySQL user with full privileges.
  4. Downloads and configures WordPress: Downloads WordPress, configures it for multisite, and uses the machine's MAC address as the default site name.
  5. Handles WordPress installation: Sets up WordPress admin credentials and performs a silent installation.
  6. Tests MySQL connections: Verifies that both the MySQL root and wpuser accounts are functional.
  7. Logs credentials: Stores MySQL and WordPress credentials in a file for future reference.

THESE ARE MY NOTES: NOT 100% FUNCTIONAL

This script helps automate the setup of a development environment, particularly for WordPress multisite installations with MySQL and web server dependencies.

#!/bin/bash

# Function to display and log messages
log() {
    echo "$1"
}

log "Starting the setup process for MySQL, WordPress Multisite, SSH, and robotics packages."

# Step 1: Enable auto-login for root user
log "Configuring auto-login for root user..."
AUTOLOGIN_SERVICE_DIR="/etc/systemd/system/[email protected]"
sudo mkdir -p $AUTOLOGIN_SERVICE_DIR

sudo bash -c "cat > $AUTOLOGIN_SERVICE_DIR/override.conf" <<EOL
[Service]
ExecStart=
ExecStart=-/sbin/agetty --autologin root --noclear %I \$TERM
EOL

sudo systemctl daemon-reload

# Step 2: Update packages and install Bluetooth, Wireless, and other dependencies
log "Updating package lists and installing required packages..."
sudo apt update -y

# Install all required packages together for optimization
sudo apt install -y \
    bluez network-manager wireless-tools wpasupplicant \
    g++ perl python3 libpcap-dev \
    apache2 php php-mysql libapache2-mod-php php-xml php-mbstring php-curl php-gd php-zip php-pear php-intl php-cli php-xdebug php-dev \
    mysql-server python3-rpi.gpio i2c-tools || echo "Some packages may have been already installed."

# Step 3: Install ROS (if not already installed)
log "Setting up ROS repository and installing ROS packages..."
if ! dpkg -l | grep -q ros-noetic-desktop-full; then
    sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
    sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
    sudo apt update
    sudo apt install -y ros-noetic-desktop-full python3-rosinstall python3-rosinstall-generator python3-wstool python3-rosdep
    sudo rosdep init
    rosdep update
else
    log "ROS Noetic Desktop Full is already installed. Skipping..."
fi

# Step 4: Remove unnecessary services (if installed)
log "Removing unnecessary services..."
PACKAGES_TO_REMOVE="cups cups-bsd cups-client cups-common avahi-daemon modemmanager"
for PACKAGE in $PACKAGES_TO_REMOVE; do
    if dpkg -l | grep -q $PACKAGE; then
        sudo apt purge -y $PACKAGE || echo "$PACKAGE could not be removed."
    else
        log "$PACKAGE is not installed, skipping..."
    fi
done

# Step 5: Fix MySQL Root Access in Safe Mode
log "Starting MySQL in safe mode to reset root password..."
sudo systemctl stop mysql
sudo mysqld_safe --skip-grant-tables &

# Wait for MySQL to start
sleep 5

# Reset root password to use mysql_native_password
log "Resetting root password and setting mysql_native_password..."
mysql -u root <<EOF
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY 'new_root_password';
FLUSH PRIVILEGES;
EOF

# Stop MySQL safe mode
sudo mysqladmin shutdown

# Start MySQL normally
sudo systemctl start mysql
log "MySQL has been restarted normally."

# Step 6: Create WordPress database and user with the correct privileges
log "Setting up WordPress database and user..."

WP_DB_USER="wpuser"
WP_DB_PASSWORD=$(openssl rand -base64 12)
WP_DB_NAME="wordpress_db"
MYSQL_ROOT_PASSWORD="new_root_password"

CREDENTIALS_FILE="/usr/local/bin/credentials.txt"
echo "MySQL root password: $MYSQL_ROOT_PASSWORD" > "$CREDENTIALS_FILE"
echo "MySQL user password: $WP_DB_PASSWORD" >> "$CREDENTIALS_FILE"

log "Creating WordPress database and user with the correct privileges..."
mysql -u root -p"$MYSQL_ROOT_PASSWORD" <<EOF
CREATE DATABASE IF NOT EXISTS $WP_DB_NAME;
CREATE USER IF NOT EXISTS '$WP_DB_USER'@'%' IDENTIFIED WITH mysql_native_password BY '$WP_DB_PASSWORD';
GRANT ALL PRIVILEGES ON *.* TO '$WP_DB_USER'@'%' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON $WP_DB_NAME.* TO '$WP_DB_USER'@'%';
FLUSH PRIVILEGES;
EOF

if [ $? -eq 0 ]; then
    log "Database and user creation successful."
else
    log "Error occurred during database and user creation."
    exit 1
fi

# Step 7: Get MAC address from enp0s3 and use it as WordPress site name
log "Retrieving the MAC address from enp0s3..."
MAC_ADDRESS=$(ip link show enp0s3 | awk '/ether/ {print $2}' | sed 's/://g')
if [ -z "$MAC_ADDRESS" ]; then
    MAC_ADDRESS="defaultsite"
fi

# Step 8: Download and install WordPress
log "Downloading and installing WordPress..."
wget https://wordpress.org/latest.tar.gz -P /tmp
tar -xzf /tmp/latest.tar.gz -C /tmp
sudo mv /tmp/wordpress/* /var/www/html/

# Set correct permissions
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

# Step 9: Configure WordPress
log "Configuring WordPress..."
if [ -f /var/www/html/wp-config-sample.php ]; then
    sudo cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
    sudo sed -i "s/database_name_here/$WP_DB_NAME/" /var/www/html/wp-config.php
    sudo sed -i "s/username_here/$WP_DB_USER/" /var/www/html/wp-config.php
    sudo sed -i "s/password_here/$WP_DB_PASSWORD/" /var/www/html/wp-config.php

    sudo sed -i "/^\/\* That's all, stop editing! Happy publishing. \*\//i \
    define('WP_ALLOW_MULTISITE', true);" /var/www/html/wp-config.php
else
    log "Error: wp-config.php is missing!"
    exit 1
fi

# Restart Apache
sudo systemctl restart apache2

# Step 10: Create WordPress admin credentials
log "Setting up WordPress admin credentials..."
WP_ADMIN_USER="admin"
WP_ADMIN_PASSWORD=$(openssl rand -base64 12)
WP_ADMIN_EMAIL="[email protected]"

echo "WordPress admin username: $WP_ADMIN_USER" >> "$CREDENTIALS_FILE"
echo "WordPress admin password: $WP_ADMIN_PASSWORD" >> "$CREDENTIALS_FILE"
echo "WordPress admin email: $WP_ADMIN_EMAIL" >> "$CREDENTIALS_FILE"

# Silent installation for WordPress multisite using MAC address as site name
log "Installing WordPress multisite with site name: $MAC_ADDRESS..."
sudo -u www-data php /var/www/html/wp-admin/install.php --url="localhost" --title="$MAC_ADDRESS" --admin_user="$WP_ADMIN_USER" --admin_password="$WP_ADMIN_PASSWORD" --admin_email="$WP_ADMIN_EMAIL" --skip-email

# Step 11: Test MySQL credentials
log "Testing MySQL root login..."
if mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "EXIT"; then
    log "MySQL root login: SUCCESS"
else
    log "MySQL root login: FAILED"
fi

log "Testing MySQL wpuser login..."
if mysql -u wpuser -p"$WP_DB_PASSWORD" -e "EXIT"; then
    log "MySQL wpuser login: SUCCESS"
else
    log "MySQL wpuser login: FAILED"
fi

log "Setup completed successfully. You can find the credentials in $CREDENTIALS_FILE."

# Navigate to /usr/local/bin at the end
cd /usr/local/bin