Skip to content

Instantly share code, notes, and snippets.

@ajithrn
Created August 3, 2026 21:44
Show Gist options
  • Select an option

  • Save ajithrn/86fd25a86c62a3a923b5b70f3f2db50d to your computer and use it in GitHub Desktop.

Select an option

Save ajithrn/86fd25a86c62a3a923b5b70f3f2db50d to your computer and use it in GitHub Desktop.
Quick-create WordPress sites locally with Laravel Valet + WP-CLI + MariaDB. Interactive or pass arguments. macOS.
#!/bin/bash
# =============================================================================
# wp-valet-quickstart.sh
# Quick-create a WordPress site locally using Laravel Valet + WP-CLI + MariaDB
#
# Run interactively (prompts for everything):
# ./wp-valet-quickstart.sh
#
# Or pass arguments to skip prompts:
# ./wp-valet-quickstart.sh --name=mysite --dir=~/Sites \
# --db-user=root --db-pass=secret --db-host=127.0.0.1 \
# --wp-user=admin --wp-pass=secret \
# --wp-email=you@example.com --wp-title="My Site" \
# --secure
#
# Prerequisites: Homebrew, PHP, Composer, Laravel Valet, WP-CLI, MariaDB/MySQL
# Full Valet setup guide: https://gist.github.com/ajithrn/8f23dec6b162448c992b38f61f5afcb7
# =============================================================================
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# -----------------------------------------------------------------------------
# Dependency checks
# -----------------------------------------------------------------------------
check_dependency() {
if ! command -v "$1" &> /dev/null; then
echo -e "${RED}Error: '$1' is not installed or not in PATH.${NC}"
echo "Please install it before running this script."
echo "See: https://gist.github.com/ajithrn/8f23dec6b162448c992b38f61f5afcb7"
exit 1
fi
}
check_dependency wp
check_dependency valet
check_dependency mariadb
# -----------------------------------------------------------------------------
# Defaults
# -----------------------------------------------------------------------------
SITE_NAME=""
SITES_DIR=""
DB_USER="root"
DB_PASS=""
DB_HOST="127.0.0.1"
WP_USER="admin"
WP_PASS=""
WP_EMAIL="admin@example.com"
WP_TITLE=""
SECURE="false"
# -----------------------------------------------------------------------------
# Parse command-line arguments
# -----------------------------------------------------------------------------
for arg in "$@"; do
case $arg in
--name=*)
SITE_NAME="${arg#*=}"
;;
--dir=*)
SITES_DIR="${arg#*=}"
;;
--db-user=*)
DB_USER="${arg#*=}"
;;
--db-pass=*)
DB_PASS="${arg#*=}"
;;
--db-host=*)
DB_HOST="${arg#*=}"
;;
--wp-user=*)
WP_USER="${arg#*=}"
;;
--wp-pass=*)
WP_PASS="${arg#*=}"
;;
--wp-email=*)
WP_EMAIL="${arg#*=}"
;;
--wp-title=*)
WP_TITLE="${arg#*=}"
;;
--secure)
SECURE="true"
;;
--help|-h)
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Quick-create a WordPress site with Laravel Valet, WP-CLI, and MariaDB."
echo ""
echo "Options:"
echo " --name=NAME Site folder name (used for domain: NAME.test)"
echo " --dir=PATH Parent directory where site folder will be created"
echo " --db-user=USER Database user (default: root)"
echo " --db-pass=PASS Database password"
echo " --db-host=HOST Database host (default: 127.0.0.1)"
echo " --wp-user=USER WordPress admin username (default: admin)"
echo " --wp-pass=PASS WordPress admin password"
echo " --wp-email=EMAIL WordPress admin email (default: admin@example.com)"
echo " --wp-title=TITLE WordPress site title (default: site name)"
echo " --secure Enable HTTPS via Valet"
echo " --help, -h Show this help message"
echo ""
echo "If options are not provided, the script will prompt interactively."
echo ""
echo "Full Valet setup guide:"
echo " https://gist.github.com/ajithrn/8f23dec6b162448c992b38f61f5afcb7"
exit 0
;;
*)
echo -e "${RED}Unknown option: $arg${NC}"
echo "Use --help for usage information."
exit 1
;;
esac
done
# -----------------------------------------------------------------------------
# Interactive prompts for missing values
# -----------------------------------------------------------------------------
echo -e "${CYAN}========================================${NC}"
echo -e "${CYAN} WordPress Site Creator (Valet + WP-CLI)${NC}"
echo -e "${CYAN}========================================${NC}"
echo ""
if [ -z "$SITE_NAME" ]; then
read -p "Site folder name (used as domain, e.g. mysite -> mysite.test): " SITE_NAME
fi
if [ -z "$SITE_NAME" ]; then
echo -e "${RED}Error: Site name is required.${NC}"
exit 1
fi
# Validate site name (only lowercase letters, numbers, hyphens)
if [[ ! "$SITE_NAME" =~ ^[a-z0-9][a-z0-9-]*[a-z0-9]$|^[a-z0-9]$ ]]; then
echo -e "${RED}Error: Site name should only contain lowercase letters, numbers, and hyphens.${NC}"
exit 1
fi
if [ -z "$SITES_DIR" ]; then
read -p "Parent directory for the site [$(pwd)]: " SITES_DIR
SITES_DIR="${SITES_DIR:-$(pwd)}"
fi
# Expand ~ in SITES_DIR
SITES_DIR="${SITES_DIR/#\~/$HOME}"
# Check if directory already exists
if [ -d "$SITES_DIR/$SITE_NAME" ] && [ "$(ls -A "$SITES_DIR/$SITE_NAME" 2>/dev/null)" ]; then
echo -e "${RED}Error: Directory '$SITES_DIR/$SITE_NAME' already exists and is not empty.${NC}"
exit 1
fi
read -p "Database user [$DB_USER]: " input_db_user
DB_USER="${input_db_user:-$DB_USER}"
if [ -z "$DB_PASS" ]; then
read -sp "Database password (leave empty if none): " DB_PASS
echo ""
fi
read -p "Database host [$DB_HOST]: " input_db_host
DB_HOST="${input_db_host:-$DB_HOST}"
# Test database connection before proceeding
echo -e "${CYAN}Testing database connection...${NC}"
if [ -z "$DB_PASS" ]; then
if ! mariadb -u "$DB_USER" -h "$DB_HOST" -e "SELECT 1;" &> /dev/null; then
echo -e "${RED}Error: Cannot connect to database. Check your credentials.${NC}"
exit 1
fi
else
if ! mariadb -u "$DB_USER" -p"$DB_PASS" -h "$DB_HOST" -e "SELECT 1;" &> /dev/null; then
echo -e "${RED}Error: Cannot connect to database. Check your credentials.${NC}"
exit 1
fi
fi
echo -e "${GREEN}Database connection OK.${NC}"
echo ""
read -p "WordPress admin username [$WP_USER]: " input_wp_user
WP_USER="${input_wp_user:-$WP_USER}"
if [ -z "$WP_PASS" ]; then
read -sp "WordPress admin password: " WP_PASS
echo ""
fi
if [ -z "$WP_PASS" ]; then
echo -e "${RED}Error: WordPress admin password is required.${NC}"
exit 1
fi
read -p "WordPress admin email [$WP_EMAIL]: " input_email
WP_EMAIL="${input_email:-$WP_EMAIL}"
if [ -z "$WP_TITLE" ]; then
read -p "Site title [$SITE_NAME]: " WP_TITLE
WP_TITLE="${WP_TITLE:-$SITE_NAME}"
fi
if [ "$SECURE" = "false" ]; then
read -p "Enable HTTPS/SSL? (y/n) [y]: " secure_input
secure_input="${secure_input:-y}"
if [[ "$secure_input" =~ ^[Yy]$ ]]; then
SECURE="true"
fi
fi
# Derive database name from site name (replace hyphens with underscores)
DB_NAME=$(echo "$SITE_NAME" | tr '-' '_')
# Determine URL scheme
if [ "$SECURE" = "true" ]; then
SITE_URL="https://$SITE_NAME.test"
else
SITE_URL="http://$SITE_NAME.test"
fi
# -----------------------------------------------------------------------------
# Confirm before proceeding
# -----------------------------------------------------------------------------
echo ""
echo -e "${YELLOW}--- Configuration Summary ---${NC}"
echo -e " Site name: ${GREEN}$SITE_NAME${NC}"
echo -e " Site directory: ${GREEN}$SITES_DIR/$SITE_NAME${NC}"
echo -e " Site URL: ${GREEN}$SITE_URL${NC}"
echo -e " Database: ${GREEN}$DB_NAME${NC}"
echo -e " DB user: ${GREEN}$DB_USER${NC}"
echo -e " DB host: ${GREEN}$DB_HOST${NC}"
echo -e " WP admin user: ${GREEN}$WP_USER${NC}"
echo -e " WP admin email: ${GREEN}$WP_EMAIL${NC}"
echo -e " WP title: ${GREEN}$WP_TITLE${NC}"
echo -e " SSL/HTTPS: ${GREEN}$SECURE${NC}"
echo ""
read -p "Proceed with installation? (y/n) [y]: " confirm
confirm="${confirm:-y}"
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
echo -e "${RED}Installation cancelled.${NC}"
exit 0
fi
# -----------------------------------------------------------------------------
# Create site
# -----------------------------------------------------------------------------
echo ""
echo -e "${CYAN}[1/6] Creating directory...${NC}"
mkdir -p "$SITES_DIR/$SITE_NAME"
cd "$SITES_DIR/$SITE_NAME"
echo -e "${CYAN}[2/6] Downloading WordPress...${NC}"
wp core download
echo -e "${CYAN}[3/6] Creating database '$DB_NAME'...${NC}"
if [ -z "$DB_PASS" ]; then
mariadb -u "$DB_USER" -h "$DB_HOST" -e "CREATE DATABASE IF NOT EXISTS \`$DB_NAME\`;"
else
mariadb -u "$DB_USER" -p"$DB_PASS" -h "$DB_HOST" -e "CREATE DATABASE IF NOT EXISTS \`$DB_NAME\`;"
fi
echo -e "${CYAN}[4/6] Creating wp-config.php...${NC}"
wp config create \
--dbname="$DB_NAME" \
--dbuser="$DB_USER" \
--dbpass="$DB_PASS" \
--dbhost="$DB_HOST"
echo -e "${CYAN}[5/6] Installing WordPress...${NC}"
wp core install \
--url="$SITE_URL" \
--title="$WP_TITLE" \
--admin_user="$WP_USER" \
--admin_password="$WP_PASS" \
--admin_email="$WP_EMAIL"
echo -e "${CYAN}[6/6] Linking to Valet...${NC}"
# valet link/secure may require sudo password — macOS will prompt if needed
valet link "$SITE_NAME"
if [ "$SECURE" = "true" ]; then
echo -e "${CYAN} Securing with SSL (may require sudo password)...${NC}"
valet secure "$SITE_NAME"
wp option update siteurl "https://$SITE_NAME.test"
wp option update home "https://$SITE_NAME.test"
fi
# -----------------------------------------------------------------------------
# Done
# -----------------------------------------------------------------------------
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} Site ready!${NC}"
echo -e "${GREEN}========================================${NC}"
echo -e " URL: ${CYAN}$SITE_URL${NC}"
echo -e " Admin: ${CYAN}$SITE_URL/wp-admin${NC}"
echo -e " User: ${CYAN}$WP_USER${NC}"
echo -e " Email: ${CYAN}$WP_EMAIL${NC}"
echo -e " Database: ${CYAN}$DB_NAME${NC}"
echo -e " Path: ${CYAN}$SITES_DIR/$SITE_NAME${NC}"
echo ""
echo -e "For more Valet options, see:"
echo -e " ${CYAN}https://gist.github.com/ajithrn/8f23dec6b162448c992b38f61f5afcb7${NC}"
echo ""
@ajithrn

ajithrn commented Aug 3, 2026

Copy link
Copy Markdown
Author

wp-valet-quickstart.sh

Quick-create WordPress sites locally on macOS using Laravel Valet + WP-CLI + MariaDB.

Features

  • Fully interactive — prompts for all values when run without arguments
  • Non-interactive mode — pass all options as flags for scripting
  • Mix and match — provide some args, get prompted for the rest
  • Validates site name and checks for existing directories
  • Tests database connection before proceeding
  • Optionally secures site with HTTPS/SSL via Valet
  • Dependency checks on startup (wp, valet, mariadb)

Prerequisites

  • macOS
  • Homebrew
  • PHP (brew install php)
  • Composer (brew install composer)
  • Laravel Valet (composer global require laravel/valet && valet install)
  • WP-CLI (brew install wp-cli)
  • MariaDB or MySQL (brew install mariadb && brew services start mariadb)

For full setup instructions, see: Laravel Valet on macOS: Complete Setup Guide

Usage

Interactive (prompts for everything)

./wp-valet-quickstart.sh

Non-interactive (pass all options)

./wp-valet-quickstart.sh \
  --name=mysite \
  --dir=~/Sites \
  --db-user=root \
  --db-pass=secret \
  --wp-user=admin \
  --wp-pass=secret \
  --wp-email=you@example.com \
  --wp-title="My Site" \
  --secure

Partial (mix args + prompts for the rest)

./wp-valet-quickstart.sh --name=blog --secure

Show help

./wp-valet-quickstart.sh --help

Options

Option Description Default
--name=NAME Site folder name (becomes the .test domain) (prompted)
--dir=PATH Parent directory for the site folder Current directory
--db-user=USER Database user root
--db-pass=PASS Database password (prompted)
--db-host=HOST Database host 127.0.0.1
--wp-user=USER WordPress admin username admin
--wp-pass=PASS WordPress admin password (prompted)
--wp-email=EMAIL WordPress admin email admin@example.com
--wp-title=TITLE WordPress site title Site name
--secure Enable HTTPS via Valet SSL Disabled
--help, -h Show help message

What it does

  1. Creates the site directory
  2. Downloads latest WordPress core via WP-CLI
  3. Creates a MariaDB/MySQL database
  4. Generates wp-config.php
  5. Runs the WordPress install
  6. Links the site to Valet (and optionally secures with SSL)

Notes

  • Site name must be lowercase letters, numbers, and hyphens only
  • Database name is derived from site name (hyphens become underscores)
  • Valet link/secure commands may prompt for your macOS sudo password
  • The script tests the DB connection before proceeding
  • Won't overwrite an existing non-empty directory

Related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment