#!/bin/bash # Exit on error set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Default values for WordPress admin WP_ADMIN_USER="admin" WP_ADMIN_PASS="admin" WP_ADMIN_EMAIL="admin@example.com" # Function to display usage usage() { echo "Usage: $0 [options]" echo "" echo "Options:" echo " --wpusername=USERNAME WordPress admin username (default: admin)" echo " --wppassword=PASSWORD WordPress admin password (default: admin)" echo " --wpemail=EMAIL WordPress admin email (default: admin@example.com)" echo " -h, --help Display this help message" echo "" echo "Example:" echo " $0 --wpusername=myusername --wppassword=mypassword123 --wpemail=myemail@domain.com" exit 1 } # Parse command line arguments while [ $# -gt 0 ]; do case "$1" in --wpusername=*) WP_ADMIN_USER="${1#*=}" ;; --wppassword=*) WP_ADMIN_PASS="${1#*=}" ;; --wpemail=*) WP_ADMIN_EMAIL="${1#*=}" ;; -h|--help) usage ;; *) echo -e "${RED}Error: Invalid option $1${NC}" usage ;; esac shift done # Validate parameters if [[ -z "$WP_ADMIN_USER" ]]; then echo -e "${RED}Error: WordPress admin username cannot be empty${NC}" usage fi if [[ -z "$WP_ADMIN_PASS" ]]; then echo -e "${RED}Error: WordPress admin password cannot be empty${NC}" usage fi if [[ ! "$WP_ADMIN_EMAIL" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]]; then echo -e "${RED}Error: Invalid email format${NC}" usage fi # Define WordPress root directory WP_ROOT="/var/www/webroot/ROOT" # Get the domain without the node prefix FULL_HOSTNAME=$(hostname -f) DOMAIN=$(echo "$FULL_HOSTNAME" | sed 's/^node[0-9]*-//') # Set HTTP_HOST for WP-CLI export WP_CLI_CONFIG_PATH="/tmp/wp-cli-config-$RANDOM.yml" cat > "$WP_CLI_CONFIG_PATH" < /dev/null; then echo -e "${RED}PHP is not installed. Please install PHP first.${NC}" exit 1 fi # Check if MySQL/MariaDB is installed if ! command -v mysql &> /dev/null; then echo -e "${RED}MySQL/MariaDB is not installed. Please install MySQL/MariaDB first.${NC}" exit 1 fi # Check if WordPress root directory exists if [ ! -d "$WP_ROOT" ]; then echo -e "${RED}WordPress root directory $WP_ROOT does not exist.${NC}" exit 1 fi # Change to WordPress root directory cd "$WP_ROOT" # Install WP-CLI if not already installed if ! command -v wp &> /dev/null; then echo -e "${YELLOW}Installing WP-CLI...${NC}" 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 # Verify WP-CLI installation if ! command -v wp &> /dev/null; then echo -e "${RED}Failed to install WP-CLI${NC}" exit 1 fi echo -e "${GREEN}WP-CLI installed successfully${NC}" else echo -e "${GREEN}WP-CLI is already installed${NC}" fi # Database Preparation echo -e "${YELLOW}Preparing database...${NC}" # Automatically generate a new secure password for the root user new_root_password=$(openssl rand -base64 12) # Generate random database name, user, and password for the new database DB_NAME="db_$(openssl rand -hex 4)" DB_USER="user_$(openssl rand -hex 4)" DB_PASSWORD="$(openssl rand -base64 12)" DB_HOST="127.0.0.1" echo -e "${YELLOW}New root password will be: $new_root_password${NC}" echo -e "${YELLOW}New database credentials:${NC}" echo -e "Database Name: $DB_NAME" echo -e "Database User: $DB_USER" echo -e "Database Password: $DB_PASSWORD" echo -e "${YELLOW}Attempting to stop the MariaDB service...${NC}" # Stop the MariaDB service sudo systemctl stop mariadb echo -e "${YELLOW}Starting MariaDB in safe mode...${NC}" # Start MariaDB in safe mode with no networking and no grants sudo mysqld_safe --skip-grant-tables --skip-networking & # Wait for MariaDB to fully start in safe mode sleep 5 echo -e "${YELLOW}Resetting the root password...${NC}" # Reset the root password in safe mode sudo mysql -u root <