Automate your local WordPress setup on Laravel Herd with a single command
Every time I start a new WordPress project, I go through the same steps. Download WordPress, create a database, run the install, enable debug mode, set up pretty permalinks. It’s not difficult, but it takes a few minutes each time, and I always end up forgetting at least one step.
I finally sat down and wrote a bash script that handles all of it. You can run it directly from your terminal:
curl -sL danielpost.com/installwp | bash
Obviously, you should never pipe scripts into your shell without checking what they do first. You can read the full script on GitHub before running it.
The script uses Laravel Herd for the local server and WP-CLI for the WordPress setup. Let me walk you through it.
What it does
The script walks you through the full setup of a new local WordPress site. It prompts for a site title, admin username, password, and email. It then asks you to pick one of your Herd directories and a folder name for the new site.
From there, it does everything automatically: downloads WordPress, creates the wp-config.php, sets up the database, runs the install, empties the default content, enables HTTPS through Herd, sets pretty permalinks, and turns on all the debug constants you’d want during development.
At the end, you get a summary of the site details and the option to open the site in your browser.
Here’s what a full run looks like:
Using it
You can run the script without any arguments and it will prompt you for everything interactively.
bash installwp.sh
If you prefer, you can also pass values as flags to skip the prompts.
bash installwp.sh -t "My Site" -u admin -p secret -e admin@example.com
Any values you don’t pass as flags will be prompted for. The script validates the email address and won’t let you continue until you provide a valid one.
The setup
Once you’ve provided the details, the script takes care of the rest. Here’s the core of it:
wp core download
wp config create --dbname="$DB_NAME" --dbuser="$DB_USER" --dbpass="$DB_PASSWORD" --dbhost="$DB_HOST"
wp db create
wp core install --url="$WP_URL" --title="$WP_TITLE" --admin_user="$WP_ADMIN_USER" \
--admin_password="$WP_ADMIN_PASSWORD" --admin_email="$WP_ADMIN_EMAIL" --skip-email
After the install, it configures the site for local development.
wp site empty --yes
wp search-replace http https
herd secure
wp rewrite structure '/%postname%'
wp config set WP_DEBUG true --raw
wp config set WP_DEBUG_LOG true --raw
wp config set SCRIPT_DEBUG true --raw
wp config set WP_ENVIRONMENT_TYPE local
wp config set WP_DEVELOPMENT_MODE all
The herd secure command sets up a trusted SSL certificate for the .test domain, so you get HTTPS out of the box. The debug constants are all turned on because I always want those during development, and I always forget to add at least one of them.
Requirements
The script checks for three dependencies before doing anything: wp (WP-CLI), mysql, and herd. If any of these are missing, it exits with a clear error message. I use TablePlus for MySQL locally, which pairs nicely with Herd.
The site folder name doubles as both the database name and the .test domain. If you name the folder my-project, you get a database called my-project and a URL of my-project.test. One less thing to think about.
In closing
It’s a simple script, but it saves me a few minutes every time I start a new project. More importantly, it means I don’t forget to set WP_ENVIRONMENT_TYPE or enable SCRIPT_DEBUG and then wonder why something isn’t working the way I expect.
You can find the full script on GitHub.