Skip to content

Instantly share code, notes, and snippets.

@petertwise
Created October 23, 2019 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petertwise/b2e41ec7c8bdd9f4cf3586e14fe26c43 to your computer and use it in GitHub Desktop.
Save petertwise/b2e41ec7c8bdd9f4cf3586e14fe26c43 to your computer and use it in GitHub Desktop.
A small script for quickly whipping up a new WordPress site on localhost
#!/bin/bash
# Don't forget to enter your own email on line 19 and user Sites dir path on line 20
# set the slug
read -p "Enter the slug for the new site:" SLUG
# set the mysql password
read -s -p "Enter a MySQL password:" MYSQLPASS
# setup the directory and basic settings
printf "Setting up a new local site at $SLUG.localhost\n\n"
mkdir ~/Sites/$SLUG.localhost
cd ~/Sites/$SLUG.localhost
# add the virtual host to Apache
VHOST="\n\n# $SLUG Local\n
<VirtualHost *:80>\n
ServerAdmin your@email.com\n
DocumentRoot \"/Users/yourmacusername/Sites/$SLUG.localhost\"\n
ServerName $SLUG.localhost\n
ErrorLog \"/usr/local/var/log/httpd/$SLUG.localhost-error_log\"\n
CustomLog \"/usr/local/var/log/httpd/$SLUG.localhost-access_log\" common\n
</VirtualHost>\n"
echo -e $VHOST >> /usr/local/etc/httpd/extra/httpd-vhosts.conf
echo "echo '127.0.0.1 $SLUG.localhost' >> /etc/hosts" | sudo bash
sudo apachectl restart
# create the mysql database and user
mysql --user=root -p -e "CREATE DATABASE ${SLUG}_local; CREATE USER '${SLUG}_user'@'localhost' IDENTIFIED BY '${MYSQLPASS}'; GRANT USAGE ON *.* TO '${SLUG}_user'@'localhost' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0; GRANT ALL PRIVILEGES ON \`${SLUG}_local\`.* TO '${SLUG}_user'@'localhost';"
# basic WordPress setup
wp core download
wp config create --dbname=${SLUG}_local --dbuser=${SLUG}_user --dbpass=${MYSQLPASS} --extra-php <<PHP
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_HOME', 'http://$SLUG.localhost' );
define( 'WP_SITEURL', 'http://$SLUG.localhost' );
PHP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment