Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save vegasgeek/d5660ab6f50575c711c21ba70b88e9e1 to your computer and use it in GitHub Desktop.

Select an option

Save vegasgeek/d5660ab6f50575c711c21ba70b88e9e1 to your computer and use it in GitHub Desktop.
Local Newsite Script
#!/bin/zsh
# Initialize variables for flags
live_url=""
staging_url=""
dev_url=""
flat=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--live=*)
live_url="${1#*=}"
shift
;;
--staging=*)
staging_url="${1#*=}"
shift
;;
--dev=*)
dev_url="${1#*=}"
shift
;;
--flat)
flat=true
shift
;;
-*)
echo "Unknown option $1"
echo "Usage: newsite <site-name> [--live=url] [--staging=url] [--dev=url] [--flat]"
exit 1
;;
*)
if [ -z "$site_name" ]; then
site_name="$1"
else
echo "Multiple site names provided. Only one site name allowed."
exit 1
fi
shift
;;
esac
done
# Check if a site name is provided
if [ -z "$site_name" ]; then
echo "Usage: newsite <site-name> [--live=url] [--staging=url] [--dev=url] [--flat]"
echo "Example: newsite myclientsite --live=example.com --staging=stage.example.com"
echo " newsite mytest --flat"
exit 1
fi
# Step 1: Create a new directory for the site
mkdir "$HOME/Sites/$site_name"
cd "$HOME/Sites/$site_name" || exit
# Step 2-5: WordPress setup (skipped for flat sites)
if [ "$flat" = false ]; then
# Step 2: Download WordPress core files
wp core download
# Step 3: Create a MySQL database
db_name="wp_${site_name//-/_}"
mysql -uroot -pYOUR_ROOT_PASSWORD -e "CREATE DATABASE $db_name;"
# Step 4: Create the wp-config.php file
wp core config --dbname="$db_name" --dbuser="root" --dbpass="YOUR_ROOT_PASSWORD" --dbhost="localhost" --dbprefix="wp_" --extra-php <<PHP
define( 'WP_ENVIRONMENT_TYPE', 'development' );
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', false );
define( 'WP_HOME', 'https://$site_name.test' );
define( 'WP_SITEURL', 'https://$site_name.test' );
define( 'JETPACK_STAGING_MODE', true );
define( 'WP_CRON_LOCK_TIMEOUT', 60 );
define( 'WP_MEMORY_LIMIT', '2G' );
// Optional: add your ACF Pro license key if you use it
// define( 'ACF_PRO_LICENSE', 'YOUR_ACF_LICENSE_KEY' );
// Optional: add your WP Migrate DB Pro license key if you use it
// define( 'WPMDB_LICENCE', 'YOUR_WPMDB_LICENSE_KEY' );
PHP
# Step 5: Run the WordPress installation
wp core install --url="https://$site_name.test" --title="$site_name" --admin_user="YOUR_ADMIN_USERNAME" --admin_password="YOUR_ADMIN_PASSWORD" --admin_email="YOUR_ADMIN_EMAIL"
else
# Flat site: create a basic index.html
cat > "$HOME/Sites/$site_name/index.html" <<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>$site_name</title>
</head>
<body>
<h1>$site_name</h1>
</body>
</html>
HTML
fi
# Step 6: Generate SSL certificate
cd /opt/homebrew/etc/httpd/certs/ || exit
mkcert "$site_name.test"
# Step 7: Add VirtualHost config
vhost_conf="/opt/homebrew/etc/httpd/extra/httpd-vhosts.conf"
echo "
# BEGIN $site_name.test
<VirtualHost *:80>
ServerAdmin YOUR_ADMIN_EMAIL
DocumentRoot \"$HOME/Sites/$site_name\"
ServerName $site_name.test
ErrorLog \"$HOME/Sites/logs/default-error_log\"
CustomLog \"$HOME/Sites/logs/default-access_log\" common
<Directory \"$HOME/Sites/$site_name\">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:443>
DocumentRoot \"$HOME/Sites/$site_name\"
ServerName $site_name.test
SSLEngine on
SSLCertificateFile \"/opt/homebrew/etc/httpd/certs/$site_name.test.pem\"
SSLCertificateKeyFile \"/opt/homebrew/etc/httpd/certs/$site_name.test-key.pem\"
<Directory \"$HOME/Sites/$site_name\">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
# END $site_name.test
" | sudo tee -a "$vhost_conf" > /dev/null
# Step 7.5: Add /etc/hosts entry
echo "127.0.0.1 $site_name.test" | sudo tee -a /etc/hosts > /dev/null
# Step 8: Restart Apache
brew services restart httpd
echo "Done! Visit: https://$site_name.test"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment