Bash script to automatically create wordpress projects

Scribbled the 30 December 2009 in Linux

Creating a wordpress project in the webroot is something which is a really static task. And when you have done that task several times you really fed up with it. That’s why I thought “Why not automate this task?”.

Planning

I started out thinking about doing a complete solution with django, but decided to just do a quick bash script which gets executed by a php script which is situated in a htpasswd protected folder.

Here’s a basic description for the algorithm:

  1. Input webroot, project name and uid/gid
  2. Download latest wordpress tar and unpack
  3. Create the folder with the project name in the webroot
  4. Copy wordpress files into the project and change ownage of all the files

What the script looks like

So here’s what the bash script looks like:

#!/bin/bash

function quit {
    echo "Usage: $0 'path' 'projectname' 'uid/gid'"
    exit
}

if [ -z "$1" ]; then
    quit
else
    httpRoot="$1"
fi

if [ -z "$2" ]; then
    quit
else
    projectName="$2"
fi

if [ -z "$3" ]; then
    quit
else
    userGroup="$3"
fi

echo "Downloading latest wordpress... "
wget http://wordpress.org/latest.tar.gz
echo "Unpacking... "
tar -xf latest.tar.gz
mkdir $httpRoot/$projectName -p
cp wordpress/* $httpRoot/$projectName -R
sudo rm latest.tar.gz wordpress -rf
chown "$3:$3" $httpRoot/$projectName -R
echo "Wordpress project created!"

Usage

Really simple and straight-forward. The script it executed in the terminal like this:

sudo sh create-project.sh /srv/http test.paralyzed.se 33

Where the first argument is my webroot, the second is the name of what I want the project to be called and third is my http uid.

When you want to use with php just use the exec function:

exec("sudo sh create-project.sh /srv/http test.paralyzed.se 33");

Conclusion

A really simple way to save some minutes of your work day (and sanity). And also a practical example of not trying to reinvent the wheel and how to be efficient. Why spend a couple of hours creating a complete solution in django when you can do it with a little bash and php scripting.


Enlighten me with your commentary

Submit comment


Categories

Flash/Flex development
Freelancing
Linux
Web development

Archive


About


My name is Richard Zetterberg and I'm an flash/flex developer with a lot of creative ideas and a designer background.

The purpose of this blog is to write about experiments I've done or about solutions to problems I've dealt with over the time I've been programming.


Search


Contact

richard [at] paralyzed [dot] se


Social networking