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, create.sh
and delete.sh
, 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/create.sh"
alias deletesite="sh ~/Sites/tools/site-management/delete.sh"
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:
- I need to create a database WordPress will be able to use;
- I need to download WordPress;
- I need to create a
wp-config.php
file pointing to the database I just created; - I also want that config file to enable debugging;
- I need to install WordPress;
- I need some dummy content on that site;
- I want the development version of Jetpack to be installed and activated on the site.
- 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:
#!/bin/sh
# 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 );
}
PHP
wp core install --url=$site_name.dv --title=$site_name --admin_user=jeherve --admin_password=mypassword [email protected]
echo "Generate some posts"
curl http://loripsum.net/api/5 | 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.
# https://jetpack.com/support/jetpack-cli/
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.
#!/bin/sh
# 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.
One reply on “Automating my local dev environment with Laravel Valet and WP-CLI”
Hi Jeremy,
I too have been enjoying scripting with Laravel Valet. I recorded a short video here (https://pauloaten.com/2016/05/13/local-wordpress-development-with-laravel-valet/) if it would be of help to anyone.
My script is a bit long and unwieldy but it does the job. Interesting – I didn’t think of doing a delete script. Great idea!