Your cart is currently empty!
Local WordPress development via Homebrew
Share this:
August 2024 update: Switched the below from Apache and MariaDB to Nginx and MySQL to match SpinupWP setup.
Here’s a follow-up to my March 2022 article on LocalWP power usage. LocalWP has gotten too slow and eats-up loads of precious storage space. Some sites won’t shut down. The ones that do shut down take time to save backups each time. Starting up sites takes much longer than it used to. It’s become a pain!
I’ve used lots of different local hosting environments over the years. I describe some of those in the above article and also in my 2020 local development update article as well.
Enter Homebrew package manager for macOS. Homebrew, or your package manager of choice for your operating system, can be used to install and maintain a high efficiency local hosting environment.
Here’s technical details describing how to set this up:
Install web services
brew install nginx [email protected] wp-cli mysql phpmyadmin;
brew services start nginx;
brew services start php;
brew services start mysql;
Setup and secure the database service
mysql_secure_installation;
Make some PHP adjustments
Disable the attempted sending of email through PHP:
nano /opt/homebrew/etc/php/8.3/php.ini;
Edit line: sendmail_path = /dev/null
Tune your PHP-FPM for bigger WordPress sites:
nano /opt/homebrew/etc/php/8.3/php-fpm.d/www.conf;
Edit line: pm = ondemand
Edit line: pm.max_children = 50
Edit line: php_admin_value[memory_limit] = 512M
Setup the web service
Create a self-signed SSL certificate:
openssl req -x509 -newkey rsa:4096 \
-sha256 -days 3650 -nodes \
-keyout /opt/homebrew/etc/nginx/selfsigned.key \
-out /opt/homebrew/etc/nginx/selfsigned.crt \
-subj "/CN=localhost";
Fix permissions issue by setting your default user and group:
sudo chown -R USERNAME /opt/homebrew/var/run/nginx;
sudo chgrp -R staff /opt/homebrew/var/run/nginx;
Create the PhpMyAdmin site for database administration:
mkdir /opt/homebrew/etc/nginx/servers;
nano /opt/homebrew/etc/nginx/servers/phpmyadmin.conf;
Paste in the following for the file contents:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name phpmyadmin.local;
ssl_certificate /opt/homebrew/etc/nginx/selfsigned.crt;
ssl_certificate_key /opt/homebrew/etc/nginx/selfsigned.key;
location / {
root /opt/homebrew/share/phpmyadmin;
index index.html index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
root /opt/homebrew/share/phpmyadmin;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Add the site domain name into your computer’s /etc/hosts
file:
sudo nano /etc/hosts;
Add line: 127.0.0.1 phpmyadmin.local
Restart all services
brew services restart --all;
brew services list;
Change links to the desired version
brew unlink [email protected]
brew link [email protected] --force --overwrite
Navigate to https://phpmyadmin.local with your browser for database administration.
Setup a sandbox site
For each sandbox site /opt/homebrew/etc/nginx/servers/sandbox.conf
where sandbox
represents the site name;
Add the site to your computer’s /etc/hosts
file:
sudo nano /etc/hosts;
Add line: 127.0.0.1 sandbox.local
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name sandbox.local;
ssl_certificate /opt/homebrew/etc/nginx/selfsigned.crt;
ssl_certificate_key /opt/homebrew/etc/nginx/selfsigned.key;
location / {
root /opt/homebrew/var/www/sandbox;
index index.html index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
root /opt/homebrew/var/www/sandbox;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ ^/wp-content/uploads/[^\/]*/.*$ {
log_not_found off;
try_files $uri $uri/ @production;
}
location @production {
rewrite ^/wp-content/uploads/(.*)$ https://production-url.com/wp-content/uploads/$1;
}
}
Restart the web server:
brew services restart nginx;
Install and configure WordPress
Download WordPress and unzip it into /opt/homebrew/var/www/sandbox/
.
Edit /opt/homebrew/var/www/sandbox/wp-config.php
with the following changes:
define( 'DB_NAME', 'sandbox' );
define( 'DB_USER', 'root' );
define( 'DB_PASSWORD', 'password-that-you-set' );
define( 'DB_HOST', 'localhost' );
Import your site’s database:
mysql -u root -p dbname < import.sql
Navigate to your website https://sandbox.local
Setup Cron
crontab -e
Type the lowercase letter i
to begin Insert mode, then paste the below:
PATH=/opt/homebrew/bin/:/usr/local/bin:/usr/bin:/bin
* * * * * /opt/homebrew/bin/wp --path=/opt/homebrew/var/www/sandbox cron event run --due-now >/dev/null 2>&1
Then type :wq
and press Enter to save your changes.
Mail trapping
I recommend using the WP Mail Logging plugin to be able to view blocked emails. Always disable any SMTP or email service plugins so that the dev environments can’t email out accidentally.