Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Bash script to deploy a development WordPress to a local path on MacOS
# Create a new local WordPress development site on MacOS
# This script is intended for users who have configured MacOS Apache + a DNS server such as dnsmasq to automatically
# serve up folders created in a special directory with the URL pattern: "http://FOLDER_NAME.test"
# See my blog for how to set that up:
# Dependencies:
# - MySQL or MariaDB with access for your user
# - WP-CLI installed and available as command `wp` (for example -- wp-cli.phar installed at/as: `/usr/local/bin/wp`)
# Usage:
# 1) edit the var WWW_PATH to the path where your local sites will automatically be served from
# 2) this script assumes there is a .my.cnf or similar setup to provide local mysql root credentials (to create the db)
# Notes:
# - you will be prompted for DB_NAME; script will create database + user with that name, and use password 'wordpress'
# - the permalink structure /%postname%/ will automatically be applied
# - WordPress will be deployed in DEBUG_MODE with the DEBUG_LOG on and DISABLE_WP_CRON on
# - this setup is scrictly for local development only; it is not suitable for production
# The WordPress username + password will be `wpdev` and the admin email ``.
# Feel free to change these values to suit your preferences by editing the line with `wp core install`
echo "Input project url (e.g. for input '' the local domain '' will resolve)"
echo "Input title for wordpress site"
if [ -d $WP_PATH ]
echo "Directory $WP_PATH already exists, aborting..."
#exit 1
echo "Creating $WP_PATH and installing WordPress..."
echo "Database name and database username - the same value will be used for both"
read DB_NAME
echo "Creating mysql database and user, granting all privileges to user"
mysql -u root -e "create user if not exists \`${DB_NAME}\`; set password for \`${DB_NAME}\` = password('${DB_PASS}');"
mysql -u root -e "create database if not exists \`${DB_NAME}\`;"
mysql -u root -e "use \`${DB_NAME}\`; grant all on \`${DB_NAME}\`.* to '${DB_USER}' identified by '${DB_PASS}';"
mysql -u root -e "flush privileges;"
mkdir -p $WP_PATH
echo "You will be prompted for sudo..."
sudo chmod -R g+w $WWW_PATH/$PROJECT_URL
sudo chown -R `whoami`:_www $WWW_PATH/$PROJECT_URL
cd $WP_PATH && (
if [ ! -f $WP_PATH/wp-config.php ]; then
wp core download
sudo -u _www wp core config --dbname="${DB_NAME}" --dbuser="${DB_NAME}" --dbpass="${DB_PASS}" --dbhost='localhost' --dbprefix='wp_'
sudo chmod -R g+w $WP_PATH
if [ ! $(wp core is-installed) ]; then
echo "Installing WP core with admin account wpdev"
sudo -u _www wp core install --url="http://$PROJECT_URL.test" --title="$WP_TITLE" --admin_user='wpdev' --admin_password='wpdev' --admin_email=''
echo "Setting FS_METHOD direct, WP_DEBUG, and DISABLE_WP_CRON in wp-config.php"
sed -i '' '/\/\* That.s all, stop editing! Happy blogging. \*\// i\
define( "FS_METHOD", "direct" ); \
define( "WP_DEBUG", true ); \
define( "WP_DEBUG_LOG", true ); \
define( "DISABLE_WP_CRON", true ); \
' wp-config.php
sudo -u _www wp rewrite structure '/%postname%/'
sudo -u _www wp rewrite flush
# wp cli `wp config set` only works if the entries *already exist* hence
# the sed solution above
#sudo -u _www wp config set FS_METHOD 'direct'
#sudo -u _www wp config set DISABLE_WP_CRON true
#sudo -u _www wp config set WP_DEBUG true
#sudo -u _www wp config set WP_DEBUG_LOG true
sudo chmod -R g+w $WWW_PATH/$PROJECT_URL
sudo chown -R `whoami`:_www $WWW_PATH/$PROJECT_URL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment