initial commit

This commit is contained in:
2025-10-23 15:46:59 +02:00
commit 1e94746f13
4 changed files with 442 additions and 0 deletions

243
wordpressinstall Executable file
View File

@@ -0,0 +1,243 @@
#!/usr/bin/env bash
# Author : Allan Christensen
# First Created : 22052021 (DD-MM-YYYY)
# Description : Installs WordPress on Ubuntu 24.04
# License : MIT License (see LICENSE file for details)
#
# Are we root
#
if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo "" ; exit 1 ; fi
#
# Define variables and functions
#
phpver=$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;' 2>/dev/null || echo "8.3") ; phpfpm="php$phpver-fpm"
#
# Function usage
#
usage () {
printf -- "\nwordpressinstall\n\n"
printf -- "%s\n\n" "$socket"
printf -- "Installs WordPress on Ubuntu\n\n"
printf -- "Usage:\n"
printf -- "sudo ./wordpressinstall -n <domain> -d <dbname> -u <dbuser> -p <dbpass> [options]\n\n"
printf -- "Options:\n"
printf -- " -m <mariadb admin password> Root or admin password $socketusage\n"
printf -- " -a <mariadb admin user> Optional admin username $socketusage\n"
printf -- " -h | -help | --help Show this help screen\n\n"
printf -- "Examples:\n"
printf -- " sudo ./wordpressinstall -n wp.something.xyz -d wpdb -u wpuser -p wpPass123\n"
printf -- " sudo ./wordpressinstall -n wp.something.xyz -d wpdb -u wpuser -p wpPass123 -m rootpwd\n"
printf -- " sudo ./wordpressinstall -n wp.something.xyz -d wpdb -u wpuser -p wpPass123 -a admin -m adminpwd\n\n"
}
#
# Function to check if a service is already running or not
#
serviceno () { printf "\n%s" "$service" ; printf " is not running cannot continue...\n\n"; }
servicedead () { status=$(systemctl is-active "$service"); if [[ "$status" != "active" ]]; then serviceno ; exit 1 ; fi; }
#
# Function inputcheck
#
inputcheck () {
[[ -z "$hostname" ]] && { usage; echo ""; echo "Error! Hostname empty."; echo ""; exit 1; }
[[ -z "$dbname" ]] && { usage; echo ""; echo "Error! Database name empty."; echo ""; exit 1; }
[[ -z "$dbuser" ]] && { usage; echo ""; echo "Error! Database user empty."; echo ""; exit 1; }
[[ -z "$dbpass" ]] && { usage; echo ""; echo "Error! Database password empty."; echo ""; exit 1; }
}
#
# If Nginx and MariaDB is not running then die
#
service="mariadb" ; servicedead ; service="nginx" ; servicedead
#
# Check MariaDB authentication method (socket or not)
#
if mysql -u root -e ";" 2>/dev/null; then
socket="SOCKET DETECTED — no need for -a or -m"
socketusage="SOCKET DETECTED — this flag is not needed"
socketauth="yes"
else
socket="NO SOCKET DETECTED — you must use -a and -m"
socketusage="NO SOCKET DETECTED — this flag is required"
socketauth="no"
fi
#
# Let's go
#
clear
#
# Trap: show usage if user requests help. Recognizes -help and --help as early exits before getopts
#
if [[ "$1" == "-help" || "$1" == "--help" ]]; then usage ; exit 0 ; fi
#
# Trap: malformed or missing input. Catches empty argument or argument not starting with '-'
#
if [[ $# -eq 0 || ! $1 =~ ^- ]]; then usage ; exit 1 ; fi
#
# Configure command line options
#
while getopts ":n:d:u:p:m:a:h" option; do
case "$option" in
n) hostname=$(echo "$OPTARG" | tr '[:upper:]' '[:lower:]');;
d) dbname="$OPTARG";;
u) dbuser="$OPTARG";;
p) dbpass="$OPTARG";;
m) mariadbpwd="$OPTARG";;
a) mariadbadmin="$OPTARG";;
h) usage; exit 0;;
:) usage; echo ""; echo "Error! Option -$OPTARG requires an argument."; echo ""; exit 1;;
\?) usage; echo ""; echo "Error! Invalid option: -$OPTARG"; echo ""; exit 1;;
esac
done
#
# Check if input conditions are met
#
inputcheck "$hostname" "$dbname" "$dbuser" "$dbpass"
#
# Download, install, and configure the latest WordPress version
#
printf "\nDownloading latest WordPress package...\n"
mkdir -p /var/www/html
curl -sL https://wordpress.org/latest.tar.gz | tar -xzf - --transform "s,^wordpress,$hostname," -C "/var/www/html" || { echo "WordPress download or extraction failed"; exit 1; }
wptarget="/var/www/html/$hostname"
cp "$wptarget/wp-config-sample.php" "$wptarget/wp-config.php"
sed -i "s/database_name_here/$dbname/" "$wptarget/wp-config.php"
sed -i "s/username_here/$dbuser/" "$wptarget/wp-config.php"
sed -i "s/password_here/$dbpass/" "$wptarget/wp-config.php"
chown -R www-data: "$wptarget"
#
# Clone nginx-snippets; if nginx-snippets exists then just pull latest changes
#
nginxsnippets="/etc/nginx/nginx-snippets"
repo="https://git.x-files.dk/webserver/nginx-snippets.git"
if [[ -d "$nginxsnippets/.git" ]]; then git -C "$nginxsnippets" pull --quiet; else git clone --quiet "$repo" "$nginxsnippets"; fi
#
# Copy and adjust WordPress hardening config (runtime-only)
#
runtimeinc="/etc/nginx/conf.d/includes" ; mkdir -p "$runtimeinc"
if [[ -f "$nginxsnippets/wp-hardening.conf" ]]; then
cp "$nginxsnippets/wp-hardening.conf" "$runtimeinc/wp-hardening.conf"
sed -i "s/phpVERSION/php$phpver/" "$runtimeinc/wp-hardening.conf"
else
echo "Warning: wp-hardening.conf not found in nginx-snippets"
fi
#
# Copy WordPress rate-limit config (if missing)
#
if [[ -f "$nginxsnippets/wp-rate-limit.conf" ]]; then
cp --no-clobber "$nginxsnippets/wp-rate-limit.conf" /etc/nginx/conf.d/wp-rate-limit.conf 2>/dev/null
else
echo "Warning: wp-rate-limit.conf not found in nginx-snippets"
fi
#
# Determine MariaDB login method (uses earlier socket variable)
#
mariadbadmin="${mariadbadmin:-root}"
printf "\nChecking MariaDB access method...\n"
if [[ "$socketauth" == "yes" ]]; then
dbmethod="socket"
printf "Socket authentication detected (root)\n"
elif [[ -n "$mariadbpwd" && -n "$mariadbadmin" ]]; then
dbmethod="admin"
printf "Using admin user authentication (%s)\n" "$mariadbadmin"
elif [[ -n "$mariadbpwd" ]]; then
dbmethod="password"
printf "Using root password authentication\n"
else
printf "\nERROR: No valid MariaDB authentication method found.\n"
printf "Tried socket, root password, and admin credentials.\n\n"
exit 1
fi
#
# Create WordPress database
#
case "$dbmethod" in
socket)
mysql -u root <<EOF
CREATE DATABASE IF NOT EXISTS $dbname;
CREATE USER IF NOT EXISTS '$dbuser'@'localhost' IDENTIFIED BY '${dbpass//\'/\'\\\'\'}';
GRANT ALL PRIVILEGES ON $dbname.* TO '$dbuser'@'localhost';
FLUSH PRIVILEGES;
EOF
;;
password)
mysql -u root -p"${mariadbpwd}" <<EOF
CREATE DATABASE IF NOT EXISTS $dbname;
CREATE USER IF NOT EXISTS '$dbuser'@'localhost' IDENTIFIED BY '${dbpass//\'/\'\\\'\'}';
GRANT ALL PRIVILEGES ON $dbname.* TO '$dbuser'@'localhost';
FLUSH PRIVILEGES;
EOF
;;
admin)
mysql -u "${mariadbadmin}" -p"${mariadbpwd}" <<EOF
CREATE DATABASE IF NOT EXISTS $dbname;
CREATE USER IF NOT EXISTS '$dbuser'@'localhost' IDENTIFIED BY '${dbpass//\'/\'\\\'\'}';
GRANT ALL PRIVILEGES ON $dbname.* TO '$dbuser'@'localhost';
FLUSH PRIVILEGES;
EOF
;;
esac
#
# Create WordPress Nginx configuration
#
cp "$nginxsnippets/hostfiles/wordpress.80.conf" /etc/nginx/conf.d/"$hostname".conf
sed -i "s/DOMAIN/$hostname/g" /etc/nginx/conf.d/"$hostname".conf
sed -i "s/VERSION/$phpver/" /etc/nginx/conf.d/"$hostname".conf
sed -i "s|/etc/nginx/conf.d/wp-hardening.conf|$runtimeinc/wp-hardening.conf|" /etc/nginx/conf.d/"$hostname".conf
#
# Validate Nginx configuration before restarting
#
nginx -t || { echo "" ; echo "Nginx configuration test failed. Please check /etc/nginx/conf.d/ for errors." ; echo "" ; exit 1; }
#
# Restart Nginx and PHP-FPM for changes to take effect
#
systemctl restart "$phpfpm" && systemctl restart nginx
#
# Wordpress notice
#
wpnotice=$(cat <<EOF
-----------------------------------------------------------------------------------
IMPORTANT:
Go to http://$hostname/wp-admin/install.php and complete the setup
-----------------------------------------------------------------------------------
EOF
)
#
# Print notice
#
printf '%s\n' "$wpnotice"
#
# All done
#
printf "\nAll Done — WordPress files located at: %s\n" "$wptarget"
#
# End of script
#