So you created a WordPress site and unleashed it on the world, and now you want to start making some changes. There are a lot of things you can do using the admin dashboard, but eventually, you’ll probably want to make changes that involve changing or adding actual files and code. While there are ways you can make these kinds of changes on a live site, this is ill-advised for obvious reasons. Fortunately, you can set up your WordPress site locally on your computer, using it to test new ideas and make sure they work properly before deploying them. This guide was created with MacOS Mojave 10.14.5 beta, but should still apply for most older MacOS versions. I’ll be writing another guide for Linux in the near future.

Requirements

You’ll need these things in order to get your local WordPress site up and running. I’m not going to go into detail on how to set them up, but feel free to follow the links for install guides as needed.

Homebrew. Billed as “the missing package manager for MacOS”, it is a must-have if you develop on MacOS. Get it here.

PHP/Nginx. MacOS ships with PHP, with version 7.1 currently being bundled with Mojave. I recommend using the latest version (7.3 at the time of writing). You’ll also need Nginx coupled with the php-fpm extension for serving content. Fortunately all of this can be installed with Homebrew; refer to this guide for instructions.

MySQL. You’ll need this for your WordPress database. Use Homebrew for this as well, as per this guide. For the purposes of this guide, you won’t need any previous MySQL experience, but if you don’t have any and want to learn then this is a good introduction.

Valet. In simple terms, Valet is what lets you easily access your local site in your web browser, and makes it behave just like it does on the live site. It’s a powerful tool and can do all sorts of cool things! You’ll need Composer (get it here) first, and then you can set up Valet by following the instructions here.

WP-CLI. The WordPress command-line-interface is both useful and powerful, allowing you to tweak all sorts of things on your site straight from the terminal. It can be especially handy if your test site is completely borked up, allowing you to do things like deactivating problematic plugins. Get it here.

Terminal. You can certainly use MacOS’ built-in terminal, but I recommend going with a more robust alternative like iTerm2.

Get the Site Directory Files

First things first, you need to decide on a local directory where you will keep the files for your website. This can be anywhere, but your future self will thank you if you decide on some sort of organization. Make a new directory, and name it so that it matches your website domain minus the TLD (for example, “twirltech.solutions” becomes just “twirltech”.

Next, you need to copy the files for your website into this new directory. There are lots of ways to do this, including:

  • Rsync. Fast, powerful, and easy to use, rsync transfers data over SSH (many other functions). You’ll need to install rsync if you don’t have it yet, using (you guessed it) Homebrew as per this guide. Since this will require SSH, you’ll also need an SSH key. You’ll need to allow this SSH key on the server hosting your website, and if you have no idea how to do that you’ll need to talk to your hosting provider or administrator. If you get all this set up, you’ll run something like this command from your local website directory:
rsync -avz -e 'ssh -p <port number>' <remote username>@<remote server>:<path to site directory> .
  • FTP. While generally considered slow and insecure, FTP is still a viable option if your hosting provider doesn’t allow you SSH access. For instructions on how to connect via FTP you’ll need to contact your hosting provider. I recommend installing and using FileZilla using the credentials from your provider.

If you did everything correct, then your local site directory should contain all the files for your site’s WordPress install (so you should see wp-config.php, the wp-content directory, etc).

Getting the Database

You’ll need a dump of your website’s live database. As with the files, there are a lot of ways to get this file. I recommend making a separate local directory and using it to house all your database dump files.

  • From your hosting provider. Many hosting providers will have tools for downloading a database backup.
  • From phpMyAdmin. If your hosting provider gives you access to CPanel, you should be able to launch phpMyAdmin from the main CPanel interface. Then, select your site’s database in the left-hand column, click on the Export tab, and click Go – this will download the database dump as a .sql file in your browser. (Note: This works great for smaller databases, but can be unreliable with very large websites).
  • SSH + Rsync. Again, lots of ways to do this, and if you know how you probably won’t be reading this guide. In a nutshell, you’ll first connect to your web server with SSH, then export the database (using either mysqldump or wp db export), then use Rsync from your local machine to copy the file down.

Setting up the Database

Now that you’ve gotten your hands on your database file, it’s time to set that up in MySQL. (At this point, you should have already created a database user. Since this is a local setup, you can safely use just one database user for all of your locally staged sites). You’ll need to run some MySQL commands to set up the database. Important: the semicolons are a part of the commands.

Start the MySQL command line interface:

mysql -u <username> -p

Create a new database for your site, naming it whatever you want.

create database <name>;

Select the database you just created.

use <name>;

Import the database file that you downloaded in the previous section. Replace <path> with the full path to the database file.

source <path>

We’re done here, so just type exit to get out of the MySQL interface.

Next, in your terminal, cd to the directory where your site’s files are (this is the directory that contains wp-config.php). First, open wp-config.php and update it with the database user, database name, and database password for your setup. Save and close the file. Now you have a few terminal commands to run from this directory.

Update your local database to use the new .test TLD that Valet will create. replace your-site with whatever your site domain is. Of course, if your site doesn’t use a .com ending, then replace .com with whatever your TLD is.

wp search-replace your-site.com your-site.test

Tell Valet where your site files are located, and what the site domain is. Do not include .com, .test, or any sort of ending.

valet link your-site

Secure your local site so that it will load over HTTPS.

valet secure your-site

Did it Work?

Fire up your web browser and navigate to your-site.test. Hopefully, your website loads, looking the same as it does live!