Site iconIndigo Tree Digital

Automated WordPress Installation With Bash & WP CLI

Like most developers, we tend to start all our WordPress sites the same way. Add various plugins, install a starter theme, create pages and update various options. It’s rarely any different.

After manually installing & configuring WordPress for the 5th time in a week I decided that was it. I needed a better, less time consuming method that required little effort on my part. So I started working on an automated installation bash script to automate the entire process. It’s written for Mac, but may work on Linux (and if you’re using Linux you probably know how to do this anyway). It won’t work on Windows, so sell it and buy a Mac. You’ll also need to install WP CLI. You can find an installation guide on their website.

#!/bin/bash -e

wpuser='exampleuser'

clear

echo "================================================================="
echo "Awesome WordPress Installer!!"
echo "================================================================="

# accept user input for the databse name
echo "Database Name: "
read -e dbname

# accept the name of our website
echo "Site Name: "
read -e sitename

# add a simple yes/no confirmation before we proceed
echo "Run Install? (y/n)"
read -e run

# if the user didn't say no, then go ahead an install
if [ "$run" == n ] ; then
exit
else

# download the WordPress core files
wp core download

# create the wp-config file
wp core config --dbname=$dbname --dbuser=root --dbpass=root

# parse the current directory name
currentdirectory=${PWD##*/}

# generate random 12 character password
password=$(LC_CTYPE=C tr -dc A-Za-z0-9_\!\@\#\$\%\^\&\*\(\)-+= < /dev/urandom | head -c 12)

# create database, and install WordPress
wp db create
wp core install --url="http://localhost/$currentdirectory" --title="$sitename" --admin_user="$wpuser" --admin_password="$password" --admin_email="user@example.org"

# install the _s theme
wp theme install https://github.com/Automattic/_s/archive/master.zip --activate

clear

echo "================================================================="
echo "Installation is complete. Your username/password is listed below."
echo ""
echo "Username: $wpuser"
echo "Password: $password"
echo ""
echo "================================================================="

fi

Save this as a .sh file somewhere on your machine such as ~/Scripts/wpinstall.sh, then add an alias to this within your ~/.bash_profile and you’re good to go. I use the alias wpinstall, but you can of course use anything you like.

alias wpinstall="~/Scripts/wpinstall.sh"

Either source your ~/.bash_profile file or re-open terminal and try running wpinstall in an empty directory.

Taking It Further

The script above is a stripped down version of the current script I’m running for our projects. This was generally just to leave you with a script that installs WordPress without the added “awesomeness” that you’ll find below.

A couple of things to note before blindly copying and pasting the script below. I’ve updated my ~/.wp-cli/config.yml configuration file with the following code to add support for mod_rewrite:

apache_modules:
  - mod_rewrite

I’m building the majority of sites using MAMP, and expecting the URL format to match the directory name when prepended with http://localhost/. For example, if I run wpinstall when in the /Applications/MAMP/htdocs/newproject directory, I would expect the website URL to be http://localhost/newproject.

If you run into trouble getting WP CLI working with MAMP (as out of the box, it won’t work), then checkout my previous article: getting WP CLI working with MAMP.

This is my full installation script:

#!/bin/bash -e

wpuser='exampleuser'

clear

echo "================================================================="
echo "Awesome WordPress Installer!!"
echo "================================================================="

# accept user input for the databse name
echo "Database Name: "
read -e dbname

# accept the name of our website
echo "Site Name: "
read -e sitename

# accept a comma separated list of pages
echo "Add Pages: "
read -e allpages

# add a simple yes/no confirmation before we proceed
echo "Run Install? (y/n)"
read -e run

# if the user didn't say no, then go ahead an install
if [ "$run" == n ] ; then
exit
else

# download the WordPress core files
wp core download

# create the wp-config file with our standard setup
wp core config --dbname=$dbname --dbuser=root --dbpass=root --extra-php <<PHP
define( 'WP_DEBUG', true );
define( 'DISALLOW_FILE_EDIT', true );
PHP

# parse the current directory name
currentdirectory=${PWD##*/}

# generate random 12 character password
password=$(LC_CTYPE=C tr -dc A-Za-z0-9_\!\@\#\$\%\^\&\*\(\)-+= < /dev/urandom | head -c 12)

# copy password to clipboard
echo $password | pbcopy

# create database, and install WordPress
wp db create
wp core install --url="http://localhost/$currentdirectory" --title="$sitename" --admin_user="$wpuser" --admin_password="$password" --admin_email="user@example.org"

# discourage search engines
wp option update blog_public 0

# show only 6 posts on an archive page
wp option update posts_per_page 6

# delete sample page, and create homepage
wp post delete $(wp post list --post_type=page --posts_per_page=1 --post_status=publish --pagename="sample-page" --field=ID --format=ids)
wp post create --post_type=page --post_title=Home --post_status=publish --post_author=$(wp user get $wpuser --field=ID --format=ids)

# set homepage as front page
wp option update show_on_front 'page'

# set homepage to be the new page
wp option update page_on_front $(wp post list --post_type=page --post_status=publish --posts_per_page=1 --pagename=home --field=ID --format=ids)

# create all of the pages
export IFS=","
for page in $allpages; do
wp post create --post_type=page --post_status=publish --post_author=$(wp user get $wpuser --field=ID --format=ids) --post_title="$(echo $page | sed -e 's/^ *//' -e 's/ *$//')"
done

# set pretty urls
wp rewrite structure '/%postname%/' --hard
wp rewrite flush --hard

# delete akismet and hello dolly
wp plugin delete akismet
wp plugin delete hello

# install lt-tables plugin
wp plugin install https://github.com/ltconsulting/lt-tables/archive/master.zip --activate

# install antispam plugin
wp plugin install antispam-bee --activate

# install the company starter theme
wp theme install ~/Documents/lt-theme.zip --activate

clear

# create a navigation bar
wp menu create "Main Navigation"

# add pages to navigation
export IFS=" "
for pageid in $(wp post list --order="ASC" --orderby="date" --post_type=page --post_status=publish --posts_per_page=-1 --field=ID --format=ids); do
wp menu item add-post main-navigation $pageid
done

# assign navigaiton to primary location
wp menu location assign main-navigation primary

clear

echo "================================================================="
echo "Installation is complete. Your username/password is listed below."
echo ""
echo "Username: $wpuser"
echo "Password: $password"
echo ""
echo "================================================================="

# Open the new website with Google Chrome
/usr/bin/open -a "/Applications/Google Chrome.app" "http://localhost/$currentdirectory/wp-login.php"

# Open the project in TextMate
/Applications/TextMate.app/Contents/Resources/mate /Applications/MAMP/htdocs/$currentdirectory/wp-content/themes/lt-theme

fi

And my ~/.bash_profile contains the following aliases:

alias theme="cd ./wp-content/themes/lt-theme"
alias wpinstall="~/Scripts/wpinstall.sh;theme;npm install;grunt dev"
alias wpinstallquick="~/Scripts/wpinstall.sh;theme"

There are a whole load of other commands available to WP CLI that you could use within an automated installation script, or a deployment script, or even just to do mundane tasks. A full list can be found on the wp cli website along with some code examples and usage instructions.

If you find the script useful, or have any ideas on how I can improve it please leave a comment. I’d love to make the installation even faster.

If there’s anyone on Windows that has a similar script, please let me know in the comments and I’ll add a link to it within the article.

Exit mobile version