Skip to content

Instantly share code, notes, and snippets.

@fer-ri
Created March 3, 2016 17:13
Show Gist options
  • Save fer-ri/e8faa9625428a6b4c3de to your computer and use it in GitHub Desktop.
Save fer-ri/e8faa9625428a6b4c3de to your computer and use it in GitHub Desktop.
Simple Wordpress Installer
#!/bin/bash
FIND_PATH="/home/*/domains/*/public_html/"
AWK_DOMAIN_POS="5"
DB_NAME=""
DB_USER=""
DB_USER_PASS=""
DOMAIN=""
DOMAIN_OWNER="nginx"
INSTALL_PATH="${DOMAIN}"
function find_available_domains {
DOMAINS_AVAILABLE=0
find $FIND_PATH -maxdepth 0 &> /dev/null
# First check to see if there are domains available. Suppress exit status.
if [ $? -eq 0 ]; then
find $FIND_PATH -maxdepth 0 > /tmp/domain.txt
DOMAINS_AVAILABLE=`cat /tmp/domain.txt | wc -l`
fi
if [ $DOMAINS_AVAILABLE -eq 0 ]; then
echo "No domains available for install. Please add a domain first."
exit
fi
} # End function find_available_domains
function choose_domain {
# Ask user which domain to install WP
counter=1
DOMAINS_AVAILABLE=`cat /tmp/domain.txt | wc -l`
echo ""
echo "Select the domain you want to install wordpress on, 1 to $DOMAINS_AVAILABLE"
while read LINE; do
data=`echo $LINE | awk -F"/" '{ print $'${AWK_DOMAIN_POS}' }'`
echo "$counter. $data"
let counter+=1
done < "/tmp/domain.txt"
let counter-=1
# Make sure user inputs a valid domain
SELECTDOMAIN="a"
until [[ "$SELECTDOMAIN" =~ [0-9]+ ]] && [ $SELECTDOMAIN -gt 0 ] && [ $SELECTDOMAIN -le $counter ]; do
echo -n "Selection (integer) : "
read SELECTDOMAIN
done
# Get full system path to domain
DOMAIN=`cat /tmp/domain.txt | awk NR==$SELECTDOMAIN`
rm -rf /tmp/domain.txt
INSTALL_PATH="${DOMAIN}"
}
function ask_db_name {
# Ask database name for Wordpress
echo ""
echo "Enter a database name for the wordpress install. E.g domainwp, wordpress, wpdomain"
DB_NAME=""
until [[ "$DB_NAME" =~ [0-9a-zA-Z]+ ]]; do
echo -n "Database name : "
read DB_NAME
done
}
function get_latest_wordpress {
# Downlod latest wordpress version to tmp and extract
mkdir /tmp/wordpress
wget -O - http://wordpress.org/latest.tar.gz | tar zxf - -C /tmp/wordpress &> /dev/null
# Create new path for wordpress and copy files to it
mkdir $INSTALL_PATH &> /dev/null
mv /tmp/wordpress/wordpress/* $INSTALL_PATH
# Create wp-config.php file
cp $INSTALL_PATH/{wp-config-sample.php,wp-config.php}
chown -R $DOMAIN_OWNER:$DOMAIN_OWNER $DOMAIN
# Edit wp-config.php file with mysql data
sed -i 's/database_name_here/'${DB_NAME}'/' $INSTALL_PATH/wp-config.php
sed -i 's/username_here/'${DB_USER}'/' $INSTALL_PATH/wp-config.php
sed -i ' s/password_here/'${DB_USER_PASS}'/' $INSTALL_PATH/wp-config.php
rm -rf /tmp/wordpress
} # End function get_latest_wordpress
find_available_domains
choose_domain
ask_db_name
echo ""
echo "Downloading latest version of wordpress..."
get_latest_wordpress
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment