mb-admin/scripts/install-wordpress.sh

362 lines
11 KiB
Bash
Raw Normal View History

2025-03-31 17:37:54 +00:00
#!/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" <<EOF
config create:
extra-php: |
\$_SERVER['HTTP_HOST'] = '$DOMAIN';
EOF
echo -e "${YELLOW}Starting WordPress installation process...${NC}"
echo -e "${YELLOW}Using domain: $DOMAIN${NC}"
# Check if PHP is installed
if ! command -v php &> /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 <<EOF
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY '$new_root_password';
ALTER USER 'root'@'127.0.0.1' IDENTIFIED BY '$new_root_password';
FLUSH PRIVILEGES;
EOF
# Check if the password reset was successful
if [ $? -eq 0 ]; then
echo -e "${GREEN}Root password reset successful.${NC}"
else
echo -e "${RED}Failed to reset the root password. Exiting.${NC}"
sudo pkill -f mariadbd
sudo pkill -f mysqld_safe
exit 1
fi
echo -e "${YELLOW}Stopping the MariaDB safe mode process...${NC}"
# Terminate the MariaDB safe mode processes
sudo pkill -f mysqld_safe
sleep 2
sudo pkill -f mariadbd
sleep 2
echo -e "${YELLOW}Starting the MariaDB service normally...${NC}"
# Start the MariaDB service normally
sudo systemctl start mariadb
# Check if MariaDB started successfully
if sudo systemctl is-active --quiet mariadb; then
echo -e "${GREEN}MariaDB service is running. Root password has been reset.${NC}"
else
echo -e "${RED}Failed to start MariaDB service. Please check the service status manually.${NC}"
exit 1
fi
# Introduce a delay to allow MariaDB to fully initialize
sleep 5
# Create MySQL database and user with the new root password
echo -e "${YELLOW}Creating MySQL database and user...${NC}"
mysql -u root -p"$new_root_password" <<EOF
CREATE DATABASE ${DB_NAME};
CREATE USER '${DB_USER}'@'${DB_HOST}' IDENTIFIED BY '${DB_PASSWORD}';
GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'${DB_HOST}' IDENTIFIED BY '${DB_PASSWORD}';
FLUSH PRIVILEGES;
EOF
# Check if the database and user creation was successful
if [ $? -eq 0 ]; then
echo -e "${GREEN}Database ${DB_NAME} and user ${DB_USER} created successfully.${NC}"
else
echo -e "${RED}Failed to create database or user. Please check the MySQL status manually.${NC}"
exit 1
fi
# Backup existing wp-config.php if it exists
if [ -f "wp-config.php" ]; then
echo -e "${YELLOW}Backing up existing wp-config.php...${NC}"
cp wp-config.php wp-config.php.bak
fi
# Download WordPress core if not already present
2025-04-04 15:55:18 +00:00
if [ ! -f "index.php" ] && [ ! -d "wp-admin" ]; then
echo -e "${YELLOW}WordPress files not detected. Downloading WordPress core...${NC}"
2025-03-31 17:37:54 +00:00
wp core download --skip-content --allow-root
2025-04-04 15:55:18 +00:00
# Now WordPress core installation will handle theme installation automatically
# since we removed the --skip-themes flag from the wp core install command
echo -e "${GREEN}WordPress core downloaded successfully${NC}"
else
echo -e "${GREEN}WordPress files already exist. Skipping download.${NC}"
2025-03-31 17:37:54 +00:00
fi
2025-04-04 15:55:18 +00:00
# Immediate check to directly create wp-config.php regardless of previous steps
# This is a critical fix to ensure wp-config.php exists before installation continues
echo -e "${YELLOW}CRITICAL FIX: Directly creating wp-config.php to prevent errors...${NC}"
if [ ! -f "wp-config.php" ]; then
echo -e "${RED}wp-config.php is still missing. Creating it directly...${NC}"
2025-04-04 14:55:12 +00:00
cat > wp-config.php <<EOF
<?php
2025-04-04 15:55:18 +00:00
define( 'WP_CACHE', true );
/**
* The base configuration for WordPress
*
* @package WordPress
*/
// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
2025-04-04 14:55:12 +00:00
define( 'DB_NAME', '${DB_NAME}' );
2025-04-04 15:55:18 +00:00
/** Database username */
2025-04-04 14:55:12 +00:00
define( 'DB_USER', '${DB_USER}' );
2025-04-04 15:55:18 +00:00
/** Database password */
2025-04-04 14:55:12 +00:00
define( 'DB_PASSWORD', '${DB_PASSWORD}' );
2025-04-04 15:55:18 +00:00
/** Database hostname */
2025-04-04 14:55:12 +00:00
define( 'DB_HOST', '${DB_HOST}' );
2025-04-04 15:55:18 +00:00
/** Database charset to use in creating database tables. */
2025-04-04 14:55:12 +00:00
define( 'DB_CHARSET', 'utf8' );
2025-04-04 15:55:18 +00:00
/** The database collate type. Don't change this if in doubt. */
2025-04-04 14:55:12 +00:00
define( 'DB_COLLATE', '' );
2025-04-04 15:55:18 +00:00
/**
* Authentication unique keys and salts.
*/
2025-04-04 14:55:12 +00:00
define( 'AUTH_KEY', '$(openssl rand -base64 48)' );
define( 'SECURE_AUTH_KEY', '$(openssl rand -base64 48)' );
define( 'LOGGED_IN_KEY', '$(openssl rand -base64 48)' );
define( 'NONCE_KEY', '$(openssl rand -base64 48)' );
define( 'AUTH_SALT', '$(openssl rand -base64 48)' );
define( 'SECURE_AUTH_SALT', '$(openssl rand -base64 48)' );
define( 'LOGGED_IN_SALT', '$(openssl rand -base64 48)' );
define( 'NONCE_SALT', '$(openssl rand -base64 48)' );
2025-04-04 15:55:18 +00:00
/**
* WordPress database table prefix.
*/
2025-04-04 14:55:12 +00:00
\$table_prefix = 'wp_';
2025-04-04 15:55:18 +00:00
/* Custom values */
define( 'WP_AUTO_UPDATE_CORE', false );
define( 'WP_HOME', 'https://${DOMAIN}' );
define( 'WP_SITEURL', 'https://${DOMAIN}' );
define( 'WP_MEMORY_LIMIT', '256M' );
/**
* For developers: WordPress debugging mode.
*/
2025-04-04 14:55:12 +00:00
define( 'WP_DEBUG', false );
2025-04-04 15:55:18 +00:00
// Fix for HTTP_HOST issue when running WP-CLI
if (!isset(\$_SERVER['HTTP_HOST'])) {
\$_SERVER['HTTP_HOST'] = '${DOMAIN}';
}
/** Absolute path to the WordPress directory. */
2025-04-04 14:55:12 +00:00
if ( ! defined( 'ABSPATH' ) ) {
2025-04-04 15:55:18 +00:00
define( 'ABSPATH', __DIR__ . '/' );
2025-04-04 14:55:12 +00:00
}
2025-04-04 15:55:18 +00:00
/** Sets up WordPress vars and included files. */
2025-04-04 14:55:12 +00:00
require_once ABSPATH . 'wp-settings.php';
EOF
2025-04-04 15:55:18 +00:00
echo -e "${GREEN}wp-config.php created directly with fixed method${NC}"
# Ensure file was created and is readable
ls -la wp-config.php
2025-04-04 14:59:03 +00:00
chmod 644 wp-config.php
2025-04-04 15:55:18 +00:00
chown litespeed:litespeed wp-config.php
echo -e "${GREEN}wp-config.php permissions fixed${NC}"
else
echo -e "${GREEN}wp-config.php already exists. Continuing...${NC}"
2025-04-04 14:55:12 +00:00
fi
2025-04-04 15:55:18 +00:00
# Skip the normal wp-config.php creation process since we've already handled it directly
echo -e "${YELLOW}Continuing with WordPress installation...${NC}"
2025-03-31 17:37:54 +00:00
# Set proper permissions
echo -e "${YELLOW}Setting proper permissions...${NC}"
sudo chown -R litespeed:litespeed "$WP_ROOT"
sudo find "$WP_ROOT" -type d -exec chmod 755 {} \;
sudo find "$WP_ROOT" -type f -exec chmod 644 {} \;
# Install WordPress if not already installed
if ! wp core is-installed --allow-root; then
echo -e "${YELLOW}Installing WordPress...${NC}"
wp core install \
--url="https://$DOMAIN" \
--title="My WordPress Site" \
--admin_user="$WP_ADMIN_USER" \
--admin_password="$WP_ADMIN_PASS" \
--admin_email="$WP_ADMIN_EMAIL" \
--skip-plugins \
--allow-root
# Remove default plugins if any were installed
echo -e "${YELLOW}Removing default plugins...${NC}"
wp plugin delete hello akismet --allow-root
else
echo -e "${GREEN}WordPress is already installed.${NC}"
# Update site URL if needed
wp option update siteurl "https://$DOMAIN" --allow-root
wp option update home "https://$DOMAIN" --allow-root
fi
echo -e "${GREEN}WordPress installation completed successfully!${NC}"
echo -e "${YELLOW}Database Credentials:${NC}"
echo -e "Database Name: $DB_NAME"
echo -e "Database User: $DB_USER"
echo -e "Database Password: $DB_PASSWORD"
echo -e "Database Host: $DB_HOST"
echo -e "${YELLOW}Admin login:${NC}"
echo -e "Username: $WP_ADMIN_USER"
echo -e "Password: $WP_ADMIN_PASS"
echo -e "Email: $WP_ADMIN_EMAIL"
echo -e "${YELLOW}Site URL: https://$DOMAIN${NC}"
# Clean up the temporary WP-CLI config
rm -f "$WP_CLI_CONFIG_PATH"