wordpress-install/wordpressinstall
2025-06-02 13:57:00 +02:00

164 lines
5.6 KiB
Bash
Executable File

#!/bin/bash
#######################################################################################################
# First Created: 22052021 Author: Allan Desc: Installs Wordpress on Ubuntu requires Nginx and MariaDB #
#######################################################################################################
#
# Are we root
#
if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo "" ; exit ; fi
#
# Are we in the right directory
#
scriptdir="wordpress-install" && whereami=$(pwd |awk -F'/' '{print $NF}')
if [ "$whereami" != "$scriptdir" ]; then printf "\nWrong directory! Script must be run from %s\n\n" "$scriptdir"; exit 1; fi
#
# Define variables and functions
#
line () { printf -- '-%.0s' {1..50}; printf '\n'; }
nginxfiles="/etc/nginx/nginxsnippets"
phpver=$(php -v |head -1 |awk -F'.' '{print $1"."$2}' |sed 's/PHP //') ; phpfpm="php$phpver-fpm"
#
# Function title
#
title () { printf "\nWordpress install script V1.0\n\n"; }
#
# Function usage
#
usage () { clear ; printf -- "\n"
printf -- "wordpressinstall \n\n"
printf -- "Usage: \n"
printf -- "./wordpressinstall [-n] <wordpress domain> \n"
printf -- " [-d] <wordpress database name> \n"
printf -- " [-u] <wordpress database user> \n"
printf -- " [-p] <wordpress database password> \n"
printf -- " [-r] <mariadb root password> \n"
printf -- " [-help] <this screen> \n\n"
printf -- "Examples: \n"
printf -- "./wordpressinstall -n wp.something.xyz -d wpdb -u wpuser -p wppassword \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; fi; }
#
# Function input check
#
inputcheck () {
if [[ -z "$hostname" ]]; then usage ; printf "Error! Hostname Empty...\n\n" ; exit; fi
if [[ -z "$wpdbname" ]]; then usage ; printf "Error! Database name Empty...\n\n" ; exit; fi
if [[ -z "$wpdbuser" ]]; then usage ; printf "Error! Database username Empty...\n\n" ; exit; fi
if [[ -z "$wpdbpass" ]]; then usage ; printf "Error! Database Password Empty...\n\n" ; exit; fi
if [[ -z "$mariadbpwd" ]]; then usage ; printf "Error! Database Password Empty...\n\n" ; exit; fi; }
#
# If Nginx and MariaDB is not running then die
#
service="mariadb" ; servicedead ; service="nginx" ; servicedead
#
# Display title
#
clear ; title
#
# Configure command line options
#
# shellcheck disable=SC2199
if [[ ! $@ =~ ^\-.+ ]]; then usage; fi
# shellcheck disable=SC2086
while getopts "n:d:u:p:r:h:" option; do
case $option in
n) # hostname
hostname=$(echo ${OPTARG} | tr '[:upper:]' '[:lower:]');;
d) # wordpress database name
wpdbname=$OPTARG;;
u) # wordpress database user
wpdbuser=$OPTARG;;
p) # wordpress database password
wpdbpass=$OPTARG;;
r) # mariadb root password
mariadbpwd=$OPTARG;;
h) # display help
usage ; exit;;
\?) # invalid option
printf "\nType sudo " ; printf "%s" "$0" ; printf " -help for help\n\n" ; exit;;
esac
done
#
# Check if input conditions are met
#
inputcheck "$hostname" "$wpdbname" "$wpdbuser" "$wpdbpass" "$mariadbpwd"
#
# Download Wordpress
#
wget --no-verbose https://wordpress.org/latest.tar.gz
#
# Install and configure Wordpress
#
tar zxfp latest.tar.gz
cp wordpress/wp-config-sample.php wordpress/wp-config.php
sed -i "s/database_name_here/$wpdbname/" wordpress/wp-config.php
sed -i "s/username_here/$wpdbuser/" wordpress/wp-config.php
sed -i "s/password_here/$wpdbpass/" wordpress/wp-config.php
mv wordpress "$hostname"
mkdir -p /var/www/html
mv "$hostname" /var/www/html
chown -R www-data: /var/www/html/"$hostname"
#
# Check if nginxsnippets exist if not download them
#
if [[ ! -d "$nginxfiles" ]]; then
git clone --quiet https://git.x-files.dk/ubuntu-web-server/nginx-install.git ; cp -R nginx-install/nginxsnippets /etc/nginx
fi
#
# Create Wordpress database
#
wpsql="
connect mysql
create database $wpdbname;
CREATE USER '$wpdbuser'@'localhost' IDENTIFIED BY '$wpdbpass';
GRANT ALL PRIVILEGES ON $wpdbname.* TO '$wpdbuser'@'localhost';
FLUSH PRIVILEGES;
"
printf '%s\n' "${wpsql[@]}" |sed '1d; $d' > wpsql.sql
mysql -u root -p"$mariadbpwd" < wpsql.sql
#
# Create a Wordpress Nginx configuration file
#
curl --silent https://git.x-files.dk/ubuntu-web-server/nginx-install/raw/branch/main/cfg-apps/wordpress.80.conf > /etc/nginx/conf.d/"$hostname".conf
cp /etc/nginx/nginxsnippets/rate-limit.conf /etc/nginx/conf.d/
sed -s -i "s/DOMAIN/$hostname/g" /etc/nginx/conf.d/"$hostname".conf
sed -i "s/VERSION/$phpver/" /etc/nginx/conf.d/"$hostname".conf
#
# Restarting Nginx and Phpfpm for changes to take effect
systemctl restart nginx && systemctl restart "$phpfpm"
#
# Wordpress notice
#
wpnotice="
-----------------------------------------------------------------------------------
IMPORTANT:
Go to http://$hostname/wp-admin/install.php and complete the setup
-----------------------------------------------------------------------------------
"
#
# Print notice
#
clear ; printf '%s\n' "${wpnotice[@]}"
#
# Cleaning up
#
rm -Rf wpsql.sql latest.tar.gz nginx-install
#
# End of script
#