Automation – setting up local copy of wordpress

Thursday, October 20th, 2011 @ 11:17 am

Automation

I’m not a particularly good programmer, but I do know enough to know that there are aspects of my WordPress development that could benefit from being automated.

In this instance, the usual by-the-numbers setup stuff I do every time I start a new project. In an effort to do that, I’ve ventured into the world of bash scripting to pull together the basic things I do to get a new local copy of my blog going. Here it is, in all it’s glory. Please feel free to comment(constructively) if you feel it could be improved*. My eventual aim is to end up with something that generates dummy content and users etc, but that depends on me finding the time of course.

Points to bear in mind:

It takes a single argument – the name for your dev site and uses that to create a name for the db, password etc. i.e.:

$ sudo wp-setup.sh yoursitename

I do that because it’s simple to do and for dev purposes I’m not too fussy. However, there might be more subtle ideas that would make moving to live easier. Something to think about for the future perhaps.

Note that it also uses sudo, to run the script as root. I do this as I’m editing the apache config files and I have to be root to do that, I think. Maybe there’s a simpler way of doing that, feel free to let me know.

This uses a git clone of the latest stable release from the WordPress svn repo. I do this because I’m familiar with git, but clearly adapting this to svn wouldn’t be too complicated.

*The answer to this is clearly yes.

#!/bin/bash

# Script needs to run as root, is that bad of me? Mainly as it's modifying my apache config.

EXPECTED_ARGS=1
E_BADARGS=65
MYSQL=`which mysql`

#obviously these are just dev settings, I'm not suggesting these would be great for a live blog :)

Q1="CREATE DATABASE IF NOT EXISTS $1_db;"
Q2="GRANT ALL ON *.* TO '$1_u'@'localhost' IDENTIFIED BY '$1_p';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}"
 
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: $0 sitename"
  exit $E_BADARGS
fi

# Install base wp files - this pulls a copy of the latest stable release of wp, which I maintain on my machine.

mkdir $1
git clone ~/src/wp-stable ./$1
chown -R username:staff ./$1

echo "wp installed"

# Setup wp database

$MYSQL -u root -e "$SQL"
sed -e "s/database_name_here/$1_db/g" -e "s/username_here/$1_u/g" -e "s/password_here/$1_p/g" <./$1/wp-config-sample.php >./$1/wp-config.php

echo "db ready"

# Add in Vhost statement

printf %s "\
<VirtualHost *:80>
DocumentRoot \"/Users/samsutton/Sites/$1/\"
ServerName $1.local
ErrorLog \"/private/var/log/apache2/$1-error_log\"
CustomLog \"/private/var/log/apache2/$1-access_log\" common
</VirtualHost>
<Directory /Users/samsutton/Sites/$1/>
AllowOverride All
Options All
</Directory>
" >> /etc/apache2/extra/httpd-vhosts.conf

# using Ghost gem to add hosts reference, 'cause it's easy.

ghost add $1.local 127.0.0.1
  
# Restart apache to use new settings - site won't be visible otherwise!

sudo apachectl -k restart
echo "all done! Now browse to $1.local to finish setup"
view raw wp-setup.sh This Gist brought to you by GitHub.

Tags: , ,
Posted in Uncategorized | No Comments »

Leave a Reply