wordpress_install/test.sh

72 lines
2.1 KiB
Bash

#!/bin/bash
# ====== CONFIGURATION ======
DB_NAME="wordpress"
DB_USER="wordpressuser"
DB_PASSWORD="Change-me123@!"
DB_ROOT_PASSWORD="RootSecure123!@#" # New root password for MySQL
# ====== Update & Install Packages ======
apt update && apt upgrade -y
apt install -y apache2 mysql-server php libapache2-mod-php php-mysql curl rsync unzip
# ====== Secure MySQL Installation ======
echo "Securing MySQL..."
mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '${DB_ROOT_PASSWORD}'; FLUSH PRIVILEGES;"
SECURE_MYSQL=$(expect -c "
set timeout 10
spawn mysql_secure_installation
expect \"Enter password for user root:\"
send \"${DB_ROOT_PASSWORD}\r\"
expect \"Press y|Y for Yes, any other key for No:\"
send \"y\r\"
expect \"New password:\"
send \"${DB_ROOT_PASSWORD}\r\"
expect \"Re-enter new password:\"
send \"${DB_ROOT_PASSWORD}\r\"
expect \"Remove anonymous users?\"
send \"y\r\"
expect \"Disallow root login remotely?\"
send \"y\r\"
expect \"Remove test database and access to it?\"
send \"y\r\"
expect \"Reload privilege tables now?\"
send \"y\r\"
expect eof
")
echo "$SECURE_MYSQL"
# ====== Create WordPress Database and User ======
mysql -u root -p"${DB_ROOT_PASSWORD}" <<MYSQL_SCRIPT
CREATE DATABASE ${DB_NAME};
CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASSWORD}';
GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost';
FLUSH PRIVILEGES;
MYSQL_SCRIPT
# ====== Download and Configure WordPress ======
cd /tmp
curl -LO https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
# Update wp-config.php with DB info
sed -i "s/database_name_here/${DB_NAME}/" /tmp/wordpress/wp-config.php
sed -i "s/username_here/${DB_USER}/" /tmp/wordpress/wp-config.php
sed -i "s/password_here/${DB_PASSWORD}/" /tmp/wordpress/wp-config.php
# ====== Deploy WordPress ======
rsync -avP /tmp/wordpress/ /var/www/html/
chown -R www-data:www-data /var/www/html/
chmod -R 755 /var/www/html/
rm -f /var/www/html/index.html
# ====== Restart Apache ======
systemctl restart apache2
echo "✅ WordPress installation completed successfully."