Automating my local dev environment with Laravel Valet and WP-CLI

in WordPress

When I set up my new machine, I decided to get rid of MAMP and Vagrant, my previous local development tools of choice. While they worked well for me, I needed something a bit different. I need to be able to spin up a fresh installation of WordPress, run a few tests, and burn it all down a few minutes later.

A few months ago, I discovered Laravel Valet. Valet is a tool relying on Caddy and Dnsmasq, allowing you to spin up PHP projects in a few seconds. You can learn more about it here. It seemed like the perfect tool for me, so I decided to combine it with WP-CLI to quickly launch WordPress sites on my laptop.

A few words of warning before we start:

  • This works for me. It might not be ideal for you. Make sure to adapt the scripts below to fit your needs.
  • I won’t walk you through installing Valet and WP-CLI on your Mac. You can check the docs here and here.
  • Since we’re creating WordPress sites, we’ll also need a MySQL database.

To speed things up even more, I created 2 shell scripts, and, that I use to create and delete sites. I created aliases for both scripts:

# Manage local WP sites created and handled by Valet.
alias createsite="sh ~/Sites/tools/site-management/"
alias deletesite="sh ~/Sites/tools/site-management/"

Once Valet is installed, it’s very easy to use. To provision a new site, create a new directory, cd into that directory, and valet park. That’s really all there is to it.

Since I’m working with WordPress, I need a couple more steps before to have a running site:

  1. I need to create a database WordPress will be able to use;
  2. I need to download WordPress;
  3. I need to create a wp-config.php file pointing to the database I just created;
  4. I also want that config file to enable debugging;
  5. I need to install WordPress;
  6. I need some dummy content on that site;
  7. I want the development version of Jetpack to be installed and activated on the site.
  8. I want to activate all Jetpack modules.

The first step can be achieved with a simple MySQL query. The 7 steps that follow can all be handled with a bit of WP-CLI magic ☺️

See the script below to see how I did this:


# A shell script that will create a new WordPress site for us.

echo "What do you want your site to be called? No Spaces, just lowercase letters please."
read site_name

cd ~/Sites/dev/
mkdir $site_name
cd $site_name

echo Launch a new Valet site first.
valet park

# We will need a database for that WordPress site.
echo "CREATE DATABASE $site_name" | mysql -uroot

echo "Now let's install WordPress"
wp core download
wp core config --dbname=$site_name --dbuser=root --dbpass='' --extra-php <<PHP
define( 'WP_DEBUG', true );

if ( WP_DEBUG ) {
        @error_reporting( E_ALL );
        @ini_set( 'log_errors', true );
        @ini_set( 'log_errors_max_len', '0' );

        define( 'WP_DEBUG_LOG', true );
        define( 'WP_DEBUG_DISPLAY', false );
        define( 'CONCATENATE_SCRIPTS', false );
        define( 'SAVEQUERIES', true );

		define( 'JETPACK_DEV_DEBUG', true );

wp core install --url=$site_name.dv --title=$site_name --admin_user=jeherve --admin_password=mypassword [email protected]

echo "Generate some posts"
curl | wp post generate --post_content --count=10

echo "we'll need the Jetpack plugin. We will download the development version from GitHub, then switch to the master-stable branch, and activate the plugin."
cd wp-content/plugins/
git clone [email protected]:Automattic/jetpack.git
cd jetpack
git co master-stable
wp plugin activate jetpack

echo "Now that Jetpack is activated, let's enable all the Jetpack modules."
# Jetpack includes a few WP-CLI commands allowing you to manage modules from the command line.
wp jetpack module activate all

echo "Excellent. Your new site is ready!"
open http://$site_name.dv/wp-admin/admin.php?page=jetpack

exit 0;

That’s all there is to it! I also created a small script to delete those sites and their database once I’m done testing. Like for the site creation script, we’ll need to know the site name, and we’ll then delete everything.


# A shell script to delete a test site.

echo "Which site do you want to delete?"
read site_name

cd ~/Sites/dev/
rm -rf $site_name

# Delete the matching database table.
echo "DROP DATABASE IF EXISTS $site_name" | mysql -uroot

exit 0;

Done! createsite and deletesite are all you need to remember now!

Questions? Ideas for improvements? Let me know in the comments.

Comments are closed.