Dreaming of Code

How to run WordPress locally on Mac OS X

March 27, 2015

The purpose of this tutorial is to provide an alternative solution for running WordPress in a development environment without having to install MAMP. It uses the built in php command already installed on Mac OS X to run a server locally.

Prerequisites

Download WordPress and Start the Server

The first step is to download and extract the tarball from https://wordpress.org/latest. You can now open up a terminal and run a server from the root of the extracted WordPress package like so:

$ cd ~/downloads/wordpress # or move the wordpress folder to a directory of your choosing
$ php -S 127.0.0.1:8080 # starts the server

If you open up a browser and go to http://localhost:8080, you should now see a message that says "Error establishing a database connection". This means that we have not setup our database, which we will do in the next step.

Creating a MySQL Database

This step assumes that you have already installed MySQL and it is running on your Mac. If you are having trouble with MySQL, then here's a link to a good youtube video on how to install MySQL- https://www.youtube.com/watch?v=hqPpczFhqXU. Once you're ready, open up a new terminal and enter the following command:

$ mysql -u root

This will start a mysql command prompt where each new line will display mysql >. You are now ready to create a database and assign a user to that database. The following commands will create a database called wordpress_db, create a user called wordpress_user with a password and grant all privileges to that user on the specified database.

mysql > CREATE DATABASE wordpress_db;
mysql > GRANT ALL PRIVILEGES ON wordpress_db.* TO "wordpress_user"@"localhost" IDENTIFIED BY "password";
mysql > FLUSH PRIVILEGES;
mysql > EXIT

Create wp-config.php file

Now that the database is created, you can now go back to the root of the WordPress directory. There should be a file called wp-config-sample.php. Open up the file with your favorite text editor and enter in the proper database credentials that we created in the previous step.

<?php
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress_db');

/** MySQL database username */
define('DB_USER', 'wordpress_user');

/** MySQL database password */
define('DB_PASSWORD', 'password');

/** MySQL hostname */
define('DB_HOST', '127.0.0.1');

Save the file and copy or rename the file to wp-config.php. The php server should still be running, but if not run the php command in the first step in the root of your WordPress installation. Refresh your browser and you will see the steps to install WordPress and set your admin credentials. It's that simple!

After Thoughts

I enjoy this approach becuase you could potentially have multiple instances of WordPress running on different ports simualtaneously. Just change 8080 to any port you wish when you start the server. You will have to create a separate database for each instance. The steps to create the installation are very simple and you don't need to install and configure apache. The php server will run in a terminal and you can simply stop it by hitting CTRL C.

One thing that I did notice is that I had trouble connecting to the databse if I defined it using define('DB_HOST', 'localhost');. For some reason it would only accept connections on 127.0.0.1.