bio blog

How to Bootstrap Wordpress Projects with gitinitwp.sh

8/14/2016

'Automating' comes from the roots 'auto-' meaning 'self-', and 'mating', meaning 'screwing'.

Another weekend… another weekend project!

I wanted to automate some tasks common to bootstrapping a new git-tracked WordPress project on a local development environment. Using git as a version control system for WordPress development allows you to track changes to your code as well as to deploy your project using ssh instead of ftp. Paired with GitHub, your repo can also serve as a makeshift cloud backup. This workflow is nothing new and opinions abound on doing it right. There are also many products that entirely automate git-based deployment.

I don’t need all that, though, and my workflow is comparatively simple. I’ve written a shell script called gitinitwp.sh to simplify it even further. Gitinitwp assumes a LAMP environment and bootstraps projects with the latest copy of WordPress, creates a new wp-config.php file based on user presets or command line arguments, creates a new MySQL database, and does chown and chmod tings so that WordPress and Apache work without 403ing or asking for FTP credentials. You don’t need to do anything before you run the script, and as soon as it’s done running, you can point your browser to http://localhost/path/to/your/project to finish the install (and don’t forget to add your GitHub repo as origin and push).

Gitinitwp usage is simple and will be familiar to anyone using my JavaScript bootstrap script, gitinitjs.

usage: gitinitwp.sh [[[-a author_name] [-e author_email] [-g github_user_id] [-l local_wp_archive] [-m mysql_host] [-p project_name] [-q mysql_password] [-r remote_wp_url] [-t target_directory] [-u author_url] [-v mysql_user_id]] | [-h]]

 -a author_name: the author's name (used to attribute ownership in various files)
 (the default value can be configured in \$AUTHOR_NAME and is currently set to \"$AUTHOR_NAME\")

 -e author_email: the author's email address (used to set contact information in various files)
 (the default value can be configured in \$AUTHOR_EMAIL and is currently set to \"$AUTHOR_EMAIL\")

 -g github_user_id: the github.com user id to associate this project with (used to generate links in various files)
 (the default value can be configured in \$GITHUB_USER_ID and is currently set to \"$GITHUB_USER_ID\")

 -l local_wp_archive: the local WordPress archive to use (if this is not specified, the latest archive will be downloaded)
 (the default value can be configured in \$WORDPRESSARCHIVELOCAL and is currently set to \"$WORDPRESSARCHIVELOCAL\")

 -m mysql_host: the MySQL host address (required to generate wp-config.php)
 (the default value can be configured in \$MYSQLHOST and is currently set to \"$MYSQLHOST\")

 -p project_name: the project name (used as the project directory name as well)
 (the default value is the current directory's name, \"${PWD}\")

 -q mysql_password: the MySQL password (required to generate wp-config.php)
 (the default value can be configured in \$MYSQLPASS and is currently set to \"$MYSQLPASS\")

 -r remote_wp_url: the remote WordPress archive to download (will be fetched if a local archive is not present)
 (the default value can be configured in \$WORDPRESSARCHIVEREMOTE and is currently set to \"$WORDPRESSARCHIVEREMOTE\")

 -t target_directory: the target directory under which the project directory should be created
 (the default value can be configured in \$TARGET_DIRECTORY and is currently set to \"$TARGET_DIRECTORY\")

 -u author_url: the author's URL (used to set contact information in various files)
 (the default value can be configured in \$AUTHOR_URL and is currently set to \"$AUTHOR_URL\")

 -v mysql_user_id: the MySQL user id (required to generate wp-config.php)
 (the default value can be configured in \$MYSQLUSERNAME and is currently set to \"$MYSQLUSERNAME\")

 -h: display this message

The script assumes you’ve set sane defaults so simply calling it is enough to get you rolling (though you’ll be asked to confirm that you want to use the current directory, and its name, as the root directory of a new project).

gitinitwp.sh

Calling with -p weareuoft would bootstrap a project called “weareuoft” in a directory called “weareuoft” (provided the directory doesn’t already exist). By default, this project directory is placed wherever the script is invoked from but you can alter the behavior by changing the “TARGET_DIRECTORY” variable to a path of your choice.

gitinitwp.sh -p weareuoft

You can force the script to completely override all defaults by explicitly providing each parameter:

gitinitwp.sh -a "Mo Ismailzai" -e "moismailzai@gmail.com" -g "moismailzai" -u "http://www.moismailzai.com" -p weareuoft -m localhost -q localpassword -r https://wordpress.org/latest.tar.gz -v mo

To set project defaults, just edit the top of the script:

##### User Defaults (change these to set project defaults) #####################
AUTHOR_NAME="Your Name"
AUTHOR_EMAIL="your@email.com"
AUTHOR_URL="http://www.yourwebsite.com"
GITHUB_USER_ID="youruserid"
TARGET_DIRECTORY="${PWD}/"
LOCALUSER="${USER}"
WEBSERVERGROUP="http"
MYSQLHOST="localhost"
MYSQLUSERNAME="${USER}"
MYSQLPASS="local-password"
WORDPRESSARCHIVELOCAL="" # eg. /some/path/to/latest.tar.gz
WORDPRESSARCHIVEREMOTE="https://wordpress.org/latest.tar.gz"
WORDPRESSDOWNLOADCOMMAND="wget $WORDPRESSARCHIVEREMOTE" # eg. wget $WORDPRESSARCHIVEREMOTE
WORDPRESSUNPACKCOMMAND="tar xvzf" # eg. tar xvzf
##### END OF CONFIGURABLE DEFAULTS #############################################

Check out the project on GitHub and let me know if you find it useful. Also, I’ve got $5 for anyone who can remind me what I was working on before I got sidetracked by this script…