Over the years, I’ve written about my development environment. I’ve gone from using the internal LAMP stack that comes with Mac OS X to using MAMP, and now back again. Some of my recent work has forced me back out of MAMP and into the internal LAMP stack of Mac OS X. Of course there’s a ton of tutorials out there to get the LAMP stack running, so I’m not going to cover that here. It’s not important to what I do what to talk about today.
Instead, what I thought I’d share with you today is a script I wrote recently to help speed up getting WordPress up and running locally. There’s a couple of assumptions it makes before you get started though.
- You need to be running Mac OS X’s internal Apache.
- PHP needs to be running locally
- MySQL needs to be installed and running
After you’ve got these running and you can successfully get to http://localhost (or better yet, http://example.dev!) then we’re ready to use this script.
Getting WordPress Downloaded
Alright, so what does this code do?
Let’s break it down. First, we’re going to move into the directory where I keep all of my sites I’m working on. I always like to name the folder something relating to the site I’m working on, so the next step is to ask me what I’d like to name the folder. I then go to Github (because who doesn’t use Git these days?!) to grab WordPress. I clone WordPress down and put it in the folder (which the git command will create). I then check out a version. Right now this has to be released versions, which they tag. I do this because sometimes working on a theme/plugin, I’ve found an issue with a certain version, or maybe I haven’t updated WordPress yet, and want to make sure it works on that version. The last step here is to remove the .git directory, since I’m not concerned about updating WordPress through it.
Setting Up WordPress
Now that we’ve got WordPress downloaded, we need to set some things up. This part of the script is really the meat. We’ll break this down now.
The first couple of lines are all about getting the database information, so mysql username and password, and what you need for database host (e.g. localhost or 127.0.0.1). The script then takes those responses, creates the database (one more step I don’t have to do! :)), and then adds them to the wp-config.php file. I also grab secret keys, in the event that I use this whole site in production, it’s a step I don’t have to do later.
Add the Site to Apache
The final step in this script is to add the site to Apache and restart. A little easter egg to myself is to open the install WordPress page so that I just need to fill it out and I’m set. So what I’m doing in this part is adding the site to the http-vhosts.conf file. And restarting Apaches so it sees the new site.
Putting it All Together
In the event you don’t want to copy/paste the sections I broke down, go ahead and grab the full script below.
This was heavily influnced from Cliff Seal’s script for doing it with MAMP. You can check out his if you use MAMP.
Using the Script
This script is pretty great if you like working in the command-line, but I didn’t want to have to be in the command-line to run it. I use Alfred for a lot of things. One thing he’s really good at is running shell commands. So I thought what better than to train Alfred to run this for me. Here’s what I cam up with.
And that’s all there is to it! You can then use Alfred to start the script, open a command prompt, and run through the script. The result will then be a new development site for you to start working. I’m always looking to improve scripts I use often like this, so please offer any feedback you can think of for this.