Quick WordPress install with WP-Cli & Shell Script

I’ve recently been trying to automate more of development process. As a developer you know that you have these powerful skills and you should use them to a greater level to make your life easier. The problem is that it’s often hard to justify taking the time to do this if you’re already on a tight timeline. Needless to say I’ve finally created something that is helping remove 20-30 minutes from my developments.

I’ve been using the Vagrant repository Scotch Box for a while now. It is amazing! It’s a full LAMP stack with heaps of necessary included software. This includes Grunt, Gulp, Bower, Mailcatcher and WP-Cli (plus heaps more without being bloated). WP-Cli a great tool that allows you to run command line queries at your WP installs. I’d began using this plus a good old fashion text file of frequent install commands to speed up my process. It was finally this week that I put it into a script that I could easily run and have an install up in under 1 minute (it could be less if you have better download speeds).


#!/usr/bin/env bash

#  Take User Inputs
read -p "Database name: " db
read -p "Site URL: " url
read -p "Site title: " title
pass=$(date +%s | sha256sum | base64 | head -c 32 ; echo)

# Start Install
mkdir /var/www/public/$url
cd /var/www/public/$url

# Download WP and Config
wp core download
wp core config --dbname=$db --dbuser=root --dbpass=root
wp db create
# Run WP Install
wp core install --url=$url --title="$title" --admin_user=my_admin_user --admin_password="$pass" --admin_email="[email protected]"

# Add and Remove Base Plugins
wp plugin install admin-menu-tree-page-view contact-form-7 akismet advanced-custom-fields
wp plugin delete hello

# Delete installed posts and create homepage
wp post delete $(wp post list --post_type='page' --format=ids) --force
wp post delete $(wp post list --post_type='post' --format=ids) --force
wp post create --post_type='page' --post_title="Home" --post_status="publish" 
wp option update page_on_front 3
wp option update show_on_front page

# Replace Uncategorized with a new Category as default
wp term create category News
wp option update default_category 2
wp term delete category 1

# Set Your Timezone - Most of you will want to change this
wp option update timezone_string Australia/Perth
wp option update blogdescription ""

# Options checkboxes the way I like them
wp option update default_pingback_flag 0
wp option update default_ping_status 0
wp option update default_comment_status 0
wp option update comment_registration 1

# Update rewrite (You'll still need to resave the Settings > Permalinks Page)
wp rewrite structure '/%year%/%monthnum%/%postname%'

# Copy base theme to site
cd wp-content/themes/
git clone https://github.com/morganleek/scm-wp-base.git
mv scm-wp-base 6cm
wp theme activate 6cm

# Create necessary Apache configs
cd /etc/apache2/sites-available/
sudo cp default.conf $url.conf
sudo sed -i "s/localpress/$url/g" $url.conf 
cd ../sites-enabled/
sudo ln -s ../sites-available/$url.conf $url.conf
sudo service apache2 restart

# Update /etc/hosts - You'll need to do this manually for your own machine
sudo sed -i "s/#addmore/$url #addmore/g" /etc/hosts

# Spit out username and password details
echo ""
echo ""
echo "CMS"
echo "---"
echo "User: my_admin_user"
echo "Pass: $pass"
echo ""

In addition you’ll also need to update your /etc/hosts file once to allow the script to add the new domain to your list of local sites. This usually just means adding #addmore to the end of your localhost domains list. localhost mysite anothersite #addmore

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts scotchbox scotchbox