in Automation

Automagically create a WordPress instance using Alfred

Sometimes you have to create a WordPress instance on the fly for test purposes.
In this article I’ll show you how to create an Alfred extension that let you do this job with only one command. The only assumption is that you have a full working XAMP (Unix Apache Mysql Php) instance on your machine where to publish WordPress.
All the tutorial is based on OsX 10.10 and XAMPP.

Let’s go!

Alfred is a very popular launcher (and more!) for OsX, and with the use of its powerpack you can easily launch also custom-made script with a bunch of keys.
We want to realize an extension that using a syntax like “wpinstall sitename” will create an instance of WordPress in the htdocs directory of the local instance of Apache.
Something like this:

Ingredients and recipe

All we need to do that is a working instance of Apache with Mysql and Php and a script capable of doing the job. Here I will not discuss how to setup the Apache side of the environment, but I’ll suppose that you have it up and running and know:

  1. The full path of your document (htdocs) folder;
  2. The web address of your Apache instance;
  3. A valid mysql user with grants for creating a database and a user;

If so, configuring this parameters into the script, you’ll be able to do the magic in a snap! We’ll also assume that the new database created and the new user for this database will have the same name of the instance, while the password for the user will be always the instance name followed by “.123”.

Let’s do it!

Creating an extension in Alfred is easy: open it and write ‘alfred’ (or click on the little gear in the top right corner), then choose ‘extensions’ on the top bar. In the bottom left corner you have a button with a ‘+’: click it and choose shell script, because our extension will be a bash script. At this point you have to input the name of the extension (choose one you prefer), and if you want the author and the author’s web site. Next you’ll be prompted with the plugin window:

Choose ‘title’ and ‘description’ you prefer. ‘keyword’ is the word you have to type in Alfred to run this extension, ‘wpinstal’ is my choice but you can pick the one you prefer. If you don’t want to see the script running command-by-command, thick ‘silent’. In the end, copy the following script into the command field.

# Insert here your www directory
# Insert here your www base url
# Insert here your mysql address
# Insert here the name of a user capable of creating users
# … and his password


cd $basepath && mkdir $instance && cd $instance

curl -sO

unzip -qq && cp -R wordpress/ .
rm -fr wordpress/

/Applications/XAMPP/xamppfiles/bin/mysql -u $mysqluser -p$mysqlpassword -e "CREATE DATABASE $dbname;"

/Applications/XAMPP/xamppfiles/bin/mysql -u $mysqluser -p$mysqlpassword -e "GRANT ALL PRIVILEGES ON $dbname.* TO $dbusername@localhost IDENTIFIED BY ‘$dbpassword';"

/Applications/XAMPP/xamppfiles/bin/mysql -u $dbusername -p$dbpassword -e "use $dbname;"

/Applications/XAMPP/xamppfiles/bin/mysql -u $mysqluser -p$mysqlpassword -e "CREATE USER ‘$dbusername’ IDENTIFIED BY ‘$dbpassword';"

mv wp-config-sample.php wp-config.php

sed "s/database_name_here/$dbname/g" wp-config.php >> wp-config.temp.php && mv wp-config.temp.php wp-config.php

sed "s/username_here/$dbusername/g" wp-config.php >> wp-config.temp.php && mv wp-config.temp.php wp-config.php

sed "s/password_here/$dbpassword/g" wp-config.php >> wp-config.temp.php && mv wp-config.temp.php wp-config.php

sed "s/localhost/$dbhost/g" wp-config.php >> wp-config.temp.php && mv wp-config.temp.php wp-config.php

open -a Safari $baseurl$instance

Done that, remember to change ‘basepath’, ‘basehost’, ‘dbhost’, ‘mysqluser’ and ‘mysqlpassword’ to fit your setup. And… that’s all! Now if you call Alfred and type ‘wpinstall myinstance’ it will create:

  1. An instance of WordPress (downloading each time the last version) under ‘myinstance’ directory in your htdocs folder;
  2. A new database in your mysql instance called ‘myinstance’, with user ‘myinstance’ and password ‘myinstance.123’;

As last action, the script will open Safari pointing at the configuration page of WordPress, where you have to input a username, password and email for the administrative account. Now, if you go to ‘http://localhost/myinstance’, you’ll see your WordPress setup up-and-running!