wp-setup/wp-setup.sh

188 lines
3.9 KiB
Bash

#!/bin/bash
set -u
# THis script is a basic wordpress setup. It does
# - download wordpress latest
# - download wp-cli.phar
# - install wordpress with plugins / themes
# - create a mysql database and user
# - create apache2 config ( nginx still needs implementation )
# - restarts apache2
#
# The Wordpress Site is then available at localhost/YOUR_SITENAME
# user: admin pw: admin ( change !! )
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
if [[ $# -eq 0 ]] ; then
echo 'Site name required. Exiting ......'
exit 1
fi
#
# Vars
# Edit according to your needs
#
# apache2 document root
document_root="/var/www/html/"
domain="localhost"
admin_email="mail@schubertdaniel.de"
site_name=$1
plugins="login-lockdown cachify"
themes=""
db_new_host="localhost"
db_new_name="wp_$site_name"
db_new_user=$db_new_name
db_new_pw=$(tr -cd '[:alnum:]' < /dev/urandom | fold -w8 | head -n1)
#
# Functions
#
check_service() {
echo "Checking if $1 is running."
systemctl is-active --quiet $1
if [ $? -eq 0 ]; then
echo "++ $1 is running. Good."
echo
return 0
else
echo "-- $1 is not running."
echo
return 1
fi
}
function install_wp {
echo "Download wp-cli ....."
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
# bug when renaming wp-cli.phar on CentOs
# https://github.com/wp-cli/config-command/issues/31
# Are we on Debian?
if [ -f /etc/release ]; then
mv wp-cli.phar wp; chmod +x ./$cmd
cmd="./wp"
# Are we on RedHat / CentOS / Fedora?
elif [ -f /etc/redhat-release ]; then
chmod +x wp-cli.phar
cmd="./wp-cli.phar"
# dunno ....
else
chmod +x wp-cli.phar
cmd="./wp-cli.phar"
fi
echo "Installing Wordpress with \"$cmd\" "
$cmd core download --locale=de_DE --allow-root
$cmd config create --dbname=$db_new_name --dbuser=$db_new_user --dbhost=$db_new_host --dbpass=$db_new_pw --skip-check --allow-root
$cmd core install --url=$domain/$1 --title=$1 --admin_user=admin --admin_password=admin --admin_email=$admin_email --skip-email --allow-root
if [ -n "${plugins+x}" ]; then
echo "installing Plugins $plugins"
$cmd plugin install $plugins --allow-root
fi
if [ -n "${theme+x}" ]; then
echo "installing Theme: $theme"
$cmd theme install $theme --allow-root
fi
}
configure_nginx() {
# TODO : not implemented yet
return 0
}
configure_apache2() {
chown -R www-data. $document_root$site_name
cat > /etc/apache2/sites-available/$site_name.conf << EOL
<VirtualHost *:80>
DocumentRoot $document_root
ServerName localhost
Alias /$site_name $document_root$site_name
DirectoryIndex index.php index.html
<Directory $document_root$site_name/>
AllowOverride All
Require all granted
Options -Indexes
</Directory>
</VirtualHost>
EOL
a2ensite $site_name.conf
systemctl reload apache2
}
setup_db(){
echo "Creating new database $db_new_name"
mysqladmin create $db_new_name
mysql -e "CREATE USER '$db_new_user'@'localhost' IDENTIFIED BY '$db_new_pw';"
mysql -e "GRANT ALL PRIVILEGES ON $db_new_name . * TO '$db_new_user'@'localhost';"
}
run(){
mkdir $document_root$1
cd $document_root$1
echo "We have $2 running; Configuring it "
setup_db
install_wp $1
configure_$2
echo "Finished. Exitinig now."
exit
}
############################################
# script starts here:
#############################################
# Check for running database
check_service mysql
ret_mysql=$?
check_service mariadb
ret_maria=$?
n=$(($ret_maria + $ret_mysql))
if [ n == 2 ]; then
echo "No MySQL or MariaDB running. Exiting .."
exit
fi
# check for running webserver
check_service nginx
if [ $? -eq 0 ]; then
run $1 nginx
fi
check_service apache2
if [ $? -eq 0 ]; then
run $1 apache2
fi
echo "Neither nginx nor apache2 found running."
echo "Continuing anyway, but you will have to configure your webserver manually to make this work."
exit