Auto deploying WordPress on Symbiosis

If you have ever had to set up, manage, migrate or otherwise look after more than about three WordPress installs you will know it gets more than a little bit dull to look after them manually.

Thank you to the people behind wp-cli, as it makes life so so much easer.

mkcdir scripts
wget //raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
wp --info</code>

Now we have wp-cli installed and running, we can move our attention to making a WordPress deployment script. I found Christopher Geary’s post which will nicely act as a base.

When we are in /srv/example.co.uk/public/htdocs and want to find the current domain we can use pwd | cut -d/ -f3 to return example.co.uk

We use the domain to make the name of the database user, and the database its self (wp-cli likes to run the database with the root user).

# get the domain name
domain=$(pwd | cut -d/ -f3)
echo "Domain Name:"
echo " $domain"

# use the domain name to make database name
# swap . for _ and limit the dbname to 16 chars (maximum username length)
dbnamea=${domain//./_}
dbname=${dbnamea:0:16}

mkcdir is in my .bash_profile (see here) because its a very nice to have.

mkcdir ()
{
    mkdir -p -- "$1" &&
      cd -P -- "$1"
}

Other things added to my .bash_profile

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

in the file ~/.wp-cli/config.yml we add the following:

apache_modules:
- mod_rewrite

Here is my complete script:

#!/bin/bash -e
clear

# based on the script by Christopher Geary - http://www.ltconsulting.co.uk/automated-wordpress-installation-with-bash-wp-cli/
# Edited by Skippy - //skippy.org.uk - because he doesn't like installing wordpress!

# New site username
wpuser='webmaster'

# MySQL root password
rootpw='yourpassword'

echo "================================================================="
echo " Symbiosis WordPress Installer!"
echo " try not to break things! "
echo "================================================================="
echo ""

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

# get the domain name
domain=$(pwd | cut -d/ -f3)
echo "Domain Name:"
echo " $domain"

# use the domain name to make database name
# swap . for _ and limit the dbname to 16 chars (maximum username length)
dbnamea=${domain//./_}
dbname=${dbnamea:0:16}

echo "Database:"
echo " $dbname"

# 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 (locale codes can be found here - http://wpcentral.io/internationalization/)
wp core download --locale=en_GB

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

# 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://$domain" --title="$sitename" --admin_user="$wpuser" --admin_password="$password" --admin_email="[email protected]$domain"

# 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 some plugins
wp plugin install 404-to-Start --activate
wp plugin install Anti-Spam --activate

# 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 navigation to primary location
wp menu location assign main-navigation primary

# Clean up and fixes

# Sort out the Database username and password

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

touch temp.sql

# echo "CREATE DATABASE $dbname;" >> temp.sql
echo "CREATE USER $dbname;" >> temp.sql
echo "grant all on $dbname.* to '$dbname'@'localhost' identified by '$dbpass';" >> temp.sql

MYSQL_HISTFILE=/dev/null mysql -u root -p$rootpw < temp.sql
rm temp.sql

sed -i 's/root/'$dbname'/g' wp-config.php
sed -i 's/'$rootpw'/'$dbpass'/g' wp-config.php

# move wp-config down one and save the username and password

mv wp-config.php ../
cd ..
# So wordpress can deal with its own updates, media uploads and the ilk
sudo chown www-data htdocs/ -R


touch details.txt

echo "=================================================================" > details.txt
echo "Installation is complete. Your username/password is listed below." >> details.txt
echo "" >> details.txt
echo "Username: $wpuser" >> details.txt
echo "Password: $password" >> details.txt
echo "Domain: $domain" >> details.txt
echo "" >> details.txt
echo "=================================================================" >> details.txt

cd htdocs

clear

echo "================================================================="
echo "Installation is complete. Your username/password is listed below."
echo ""
echo "Username: $wpuser"
echo "Password: $password"
echo "Domain: $domain"
# if you have iTerm2 installed ⌘ click on the below to open the wp-admin page, See I am nice
echo "WP-admin: http://$domain/wp-admin/"
echo ""
echo "================================================================="

fi

At some point I will get around to making a bit of this site where these scripts and the ilk are stored for easy download

Leave a Reply

%d bloggers like this: