Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash script to deploy a development WordPress to a local path on MacOS
#!/bin/bash
# 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:
# https://firxworx.com/blog/it-devops/sysadmin/setting-up-a-local-development-environment-on-macos-for-lamp-wordpress-projects/
# 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 `admin@example.com`.
# Feel free to change these values to suit your preferences by editing the line with `wp core install`
WWW_PATH=/usr/local/var/www
echo "Input project url (e.g. for input 'example.com' the local domain 'example.com.test' will resolve)"
read PROJECT_URL
echo "Input title for wordpress site"
read WP_TITLE
WP_PATH="$WWW_PATH/$PROJECT_URL/public_html"
DB_PASS="wordpress"
if [ -d $WP_PATH ]
then
echo "Directory $WP_PATH already exists, aborting..."
#exit 1
else
echo "Creating $WP_PATH and installing WordPress..."
fi
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
fi
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='contact@bitcurve.com'
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\
// FX_SCRIPT FS_METHOD \
define( "FS_METHOD", "direct" ); \
\
// FX_SCRIPT WP_DEBUG \
define( "WP_DEBUG", true ); \
define( "WP_DEBUG_LOG", true ); \
\
// FX_SCRIPT DISABLE_WP_CRON \
define( "DISABLE_WP_CRON", true ); \
\
' wp-config.php
sudo -u _www wp rewrite structure '/%postname%/'
sudo -u _www wp rewrite flush
fi
# 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
You can’t perform that action at this time.