Skip to content

Instantly share code, notes, and snippets.

@gaby-mg
Last active February 19, 2016 16:57
Show Gist options
  • Save gaby-mg/4943895 to your computer and use it in GitHub Desktop.
Save gaby-mg/4943895 to your computer and use it in GitHub Desktop.
WP: Basic Install Sript with wp-cli
#!/bin/bash
# Usage: basic-install [OPTIONS] file
#
# Index:
#
# 0.- Initial Setup and Functions
# 1.- Set your server correctly
# 2.- Install latest WordPress version
# 3.- Set up anti-spam and security measures
# 4.- Install theme of your choice
# 5.- Install a SEO plugin
# 6.- Install contact form plugin
# 7.- Setup your emails and show you how to connect
# 8.- Resources
#####################################################################
# 0.- Initial Setup and Functions
#####################################################################
## 1. Functions
# Shows a help message
usage()
{
cat << EOF
Usage: basic_install [OPTIONS]
-h Show this message
-------------
-m Set MultiSite
-l <LANG> Set language
-------------
-H <HOST> Host
-u <USER> MySQL user
-p <PASS> MySQL password
-------------
-F <NAME> User First Name
-L <NAME> User Last Name
-E <EMAIL> User Email
-------------
-U <URL> URL
-t <TITLE> Title
-T <THEME> Theme
EOF
}
# Generates a random password
mkpw(){ < /dev/urandom tr -dc '[:alnum:]' | head -c${1:-16};echo;}
# 2. Global variables
USER_FIRST_NAME=
USER_LAST_NAME=
USER_LOGIN=
USER_EMAIL=
USER_PASS=$(mkpw 8)
DB_NAME=$(mkpw 8)
DB_USER=$(mkpw 8)
DB_PASS=$(mkpw 8)
DB_HOST=
MYSQL_USER=
MYSQL_PASS=
WP_ADMIN_NAME=$(mkpw 8)
WP_ADMIN_PASSWORD=$(mkpw 8)
WP_PREFIX=$(mkpw 4)
URL=
TITLE=
THEME=
LANGUAGE=
while getopts ":hml:u:p:H:F:L:E:U:t:T:" opt; do
case $opt in
h)
usage
exit 0
;;
m)
MULTISITE='yes'
;;
l)
LANGUAGE=$OPTARG
;;
u)
MYSQL_USER=$OPTARG
;;
p)
MYSQL_PASS=$OPTARG
;;
H)
DB_HOST=$OPTARG
;;
F)
USER_FIRST_NAME=$OPTARG
;;
L)
USER_LAST_NAME=$OPTARG
;;
E)
USER_EMAIL=$OPTARG
;;
U)
URL=$OPTARG
;;
t)
TITLE=$OPTARG
;;
T)
THEME=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# 3. Initial setup
echo "
[client]
user=$MYSQL_USER
password=$MYSQL_PASS" > /tmp/mysql-opts
#####################################################################
# 1.- Set your server correctly
#####################################################################
#####################################################################
# 2.- Install latest WordPress version
#####################################################################
# 1. Download WordPress and extract with wp-cli
if [ $LANGUAGE != "" ]
then wp core download --locale=$LANGUAGE
else wp core download
fi
###########
# 2. Create the Database and User
echo 'Creating the Database and User...'
Q1="CREATE DATABASE IF NOT EXISTS $DB_NAME;"
Q2="GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'$DB_HOST' IDENTIFIED BY '$DB_PASS';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}"
# Run SQL script
mysql --defaults-file=/tmp/mysql-opts -e "$SQL"
# Remove mysql-opts file
rm /tmp/mysql-opts
###########
# 3. Set up wp-config.php
echo 'Setting up wp-config.php...'
mv wp-config-sample.php wp-config.php
sed \
-i \
-e "s/\(define('DB_NAME', '\)[a-z_]*\(');\)/\1"$DB_NAME"\2/" \
-e "s/\(define('DB_USER', '\)[a-z_]*\(');\)/\1"$DB_USER"\2/" \
-e "s/\(define('DB_PASSWORD', '\)[a-z_]*\(');\)/\1"$DB_PASS"\2/" \
-e "s/\(define('DB_HOST', '\)[a-z_]*\(');\)/\1"$DB_HOST"\2/" \
-e s/\'wp_\'/\'$WP_PREFIX\_\'/ wp-config.php
if [ $MULTISITE = 'yes' ]
then sed -i '/Feliz/ i \
define('\'ALLOW_WP_MULTISITE\'', true)' wp-config.php
fi
###########
# 4. Run the Install Script
echo 'Running the Install Script...'
if [ ! $(wp core is-installed) ]
then
wp core install \
--url=$URL \
--title=$TITLE \
--admin_name=$WP_ADMIN_NAME \
--admin_email=$USER_EMAIL \
--admin_password=$WP_ADMIN_PASSWORD
fi
#####################################################################
# 3.- Set up anti-spam and security measures
#####################################################################
# 1. Add a user with author privileges
#wp user create $USER_LOGIN $USER_EMAIL2 --user_pass=$USER_PASS --role=author
# 2. Choose a strong password (see section 0.2 and 2.4)
# .... done
# 3. Secure Your Password
# : Search Brute Force Attack Plugin
# 4. Always update WordPress (see section 8.1)
# .... done
# 5. Hide WordPress Version
# 6. Change File Permisions
# 7. Whitelist
# 8. Backup
# 9. Hide Your Plugins
# 10. Analyze Server Logs
#wp plugin activate akismet
#####################################################################
# 4.- Free Theme Bundle
#####################################################################
if [ $THEME != ""]
then
wp theme install $THEME --activate
fi
#####################################################################
# 5.- Install All-in-one SEO Pack
#####################################################################
#wp plugin install all-in-one-seo-pack --activate
#####################################################################
# 6.- Install Contact Form 7
#####################################################################
#wp plugin install contact-form-7 --activate
#####################################################################
# 7.- Setup your emails and show you how to connect
#####################################################################
#####################################################################
# 8.- Finsihing up
#####################################################################
# 1. Removing wp-admin/install.php
# K.Viswanathan => Step #6
rm wp-admin/install.php
# 2. Complete User Profile
# K.Viswanathan => Step #7
wp user update 1 \
--first-name=$USER_FIRST_NAME \
--last-name=$USER_LAST_NAME
# 3. Prepare document for the user
# 2. Prepare document for admin
cat > results.txt <<EOF
= WP Admin =
WP Admin Login: $WP_ADMIN_NAME
WP Admin Password: $WP_ADMIN_PASSWORD
EOF
#####################################################################
# 9.- Resources
#####################################################################
# - Karthnik Viswanathan. 20 Steps to a Flexible and Secure WordPress
# installation. WpTuts+
# - 10 Steps to Securing Your WordPress Installation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment