#!/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 $scriptdir\n\n" ; exit ; fi # # Define variables and functions # red='\033[0;31m' ; bred='\033[1;31m' ; green='\033[0;92m' ; blue='\033[0;36m' ; bold='\033[1m' ; normal='\033[0m' line (){ for i in {1..50}; do echo -n "$1"; done && printf "\n"; } nginxfiles="/etc/nginx/nginxsnippets" phpver=$(php -v |head -1 |awk -F'.' '{print $1"."$2}' |sed 's/PHP //') phpfpm="php$phpver-fpm" ; phpclidir="/etc/php/$phpver/cli" ; phpfpmdir="/etc/php/$phpver/fpm" ; socket="php$phpver" # # Function title # title () { printf "\nWordpress install script V1.0\n\n"; } # # Function usage # usage () { clear ; printf -- "\n" printf -- "${bold}wordpressinstall${normal} \n\n" printf -- "${bold}Usage:${normal} \n" printf -- "./wordpressinstall [-n] \n" printf -- " [-d] \n" printf -- " [-u] \n" printf -- " [-p] \n" printf -- " [-help] \n\n" printf -- "${bold}Examples:${normal} \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$service 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 "${bold}Error! ${normal}Hostname Empty...\n\n" ; exit; fi if [[ -z "$wpdbname" ]]; then usage ; printf "${bold}Error! ${normal}Database name Empty...\n\n" ; exit; fi if [[ -z "$wpdbuser" ]]; then usage ; printf "${bold}Error! ${normal}Database username Empty...\n\n" ; exit; fi if [[ -z "$wpdbpass" ]]; then usage ; printf "${bold}Error! ${normal}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 # if [[ ! $@ =~ ^\-.+ ]]; then usage; fi while getopts "n:d:u:p: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;; h) # display help usage ; exit;; \?) # invalid option printf "Type $0 -help for help\n\n" ; exit;; esac done # # Check if input conditions are met # inputcheck $hostname $wpdbname $wpdbuser $wpdbpass # # 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 < 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 #