290 lines
8.8 KiB
Bash
290 lines
8.8 KiB
Bash
|
#!/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
|
||
|
if [ ! -f "wp-config.php" ]; then
|
||
|
echo -e "${YELLOW}Downloading WordPress core...${NC}"
|
||
|
wp core download --skip-content --allow-root
|
||
|
|
||
|
# Get the latest Twenty* theme
|
||
|
echo -e "${YELLOW}Detecting latest Twenty* theme...${NC}"
|
||
|
mkdir -p wp-content/themes
|
||
|
LATEST_TWENTY_THEME=$(wp theme search twenty --fields=name,version --format=csv --allow-root | grep "^twenty" | sort -V | tail -n 1 | cut -d',' -f1)
|
||
|
|
||
|
if [ -n "$LATEST_TWENTY_THEME" ]; then
|
||
|
echo -e "${YELLOW}Installing latest WordPress default theme: $LATEST_TWENTY_THEME${NC}"
|
||
|
wp theme install "$LATEST_TWENTY_THEME" --activate --allow-root
|
||
|
else
|
||
|
echo -e "${RED}Could not detect latest Twenty* theme. Falling back to twentytwentyfive${NC}"
|
||
|
wp theme install twentytwentyfive --activate --allow-root
|
||
|
fi
|
||
|
fi
|
||
|
|
||
|
# Create wp-config.php with the new database credentials
|
||
|
echo -e "${YELLOW}Creating wp-config.php...${NC}"
|
||
|
wp config create \
|
||
|
--dbname="$DB_NAME" \
|
||
|
--dbuser="$DB_USER" \
|
||
|
--dbpass="$DB_PASSWORD" \
|
||
|
--dbhost="$DB_HOST" \
|
||
|
--allow-root
|
||
|
|
||
|
# 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 \
|
||
|
--skip-themes \
|
||
|
--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"
|