How to Setup a Localhost WordPress (Sub-domain Multisite) Environment

By August 30, 2013Tutorial, WordPress

Heads up that I work on a Mac so, therefore, this tutorial is for Mac OS. However, I’m sure the majority of this process can easily be applied to Windows.

So you want a localhost WordPress sub-domain multisite network for testing? Great! But, per WordPress Settings Requirements, “You cannot choose Sub-domain Install (for a domain-based network) if the WordPress address (URL) is localhost or 127.0.0.1.” Eh. No worries. The requirements also point out that “you can create a domain-based network on your local machine by using your hosts file to map another hostname [to your local IP address], so that you never have to use the hostname localhost.”

In other words, unlike http://localhost, your localhost URL needs to have a domain name, e.g. http://localhost.com, so you’ll have to tell your local machine to use http://localhost.com instead.

This is a pretty extensive tutorial that, in the end, will have walked you through the entire process of creating a localhost WordPress sub-domain multisite environment. However, if you don’t need multisite, or don’t need sub-domain multisite, just skip sections 3 and 5.

  1. Setup Your Local Server
  2. Place The WordPress Files
  3. Change Localhost To Localhost.com – required if you want a sub-domain multisite environment
  4. Install WordPress
  5. Create A WordPress Multisite Network – required if you want a multisite environment

Prerequisites

Make sure you have the following setup, and downloaded, before moving forward.

  • The latest version of WordPress
    • It’s kind of important.
  • MAMP or WAMP
    • Allows you to install a local server environment on your Macintosh or Windows system.
    • Stands for Macintosh or Windows, Apache, MySQL, and PHP.
    • A server with Apache, MySQL, and PHP are all required in order to run WordPress. MAMP/WAMP allows you to run such a server on your computer!
    • At publication, I am running version 2.1.3, which is the most up-to-date version.
  • A code editor or text editor
    • I use Coda 2. It’s pretty nice. Anything from Panic is pretty nice.
    • There are a lot of free options. TextWrangler being one.
    • If you still don’t know, Google is your best friend.

1. Setup Your Local Server

Per WordPress requirements, in order to run WordPress you need a server running PHP and MySQL. MAMP, which stands for Macintosh, Apache, MySQL, and PHP, allows you to install such a server on your local system.

Configure MAMP

  1. Open MAMP.
  2. Click “Preferences” – I’ve included screenshots of my preferences below.
    • Start/Stop – These are really personal preferences. I like to check “Start Servers when Starting MAMP” and uncheck everything else.
      • I’m starting MAMP usually because I want to start the servers so why not automatically start the engines.
      • Not stopping the servers when you quit MAMP means you don’t have to keep MAMP open.
      • Not checking for MAMP PRO means I don’t get an annoying message every time I open MAMP.
      • My start page url is set to “/MAMP/”.
    • Ports – In order to have a sub-domain multisite environment, you must click “Set to default Apache and MySQL ports”. This will set the Apache port to 80 and the MySQL port to 3306.
    • PHP – I recommend selecting the most recent PHP version. My PHP extension is set to “XCache”.
    • Apache – Select a directory that will serve as your document root, i.e. the location where you will keep your WordPress files.
      • My preference? The “Sites” folder which, for Mac, is usually located in your Users folder. Mine is located at “/Users/rachelcarden/Sites”. But it really doesn’t matter. Just try to keep it simple and yet choose what you deem an important location. This could potentially hold a lot of important files.
  3. Click “OK” – to save and close your preferences.
  4. Click “Start Servers” – you might need to enter your administrator password.
  5. If you didn’t check “Stop Servers when quitting MAMP”, you can quit MAMP now. If not, you have to keep MAMP open for the servers to run.

Create Your Localhost WordPress Database

There’s just one more step to setting up your server. Your local machine is now running MySQL but you’ll need to create a a database for WordPress to use.

Create a database in phpMyAdmin

Create a database in phpMyAdmin

Create a database in Sequel Pro

Create a database in Sequel Pro

If you have a MySQL database management program, now’s the time to open that baby up. I use Sequel Pro. It’s pretty good and it’s free. If not, no worries. MAMP comes with phpMyAdmin, which is a web interface for MySQL management. All you have to do is open your MAMP start page, which should be http://localhost/MAMP/, and click on, you guessed it, “phpMyAdmin” in the top menu. Another way to open the start page? Open MAMP -> Click “Open start page”. Now just a few simple steps:

If Using phpMyAdmin

  1. Click the “Databases” tab.
  2. Enter the database name into the blank field. I’m gonna call mine WPDEV.
  3. Click “Create”.

If Using Sequel Pro

  1. Connect to your localhost server.
    • Name: Localhost
    • Host: 127.0.0.1
    • Username: root
    • Password: root
  2. Select “Database” from the top menu and click “Add Database…”.
  3. Enter the database name into the blank field. I’m gonna call mine WPDEV.
  4. Click “Add”.

2. Place The WordPress Files

Remember the document root you just selected in your MAMP preferences? Place the WordPress files in your document root directory. Mine is my “Sites” folder, located at “/Users/rachelcarden/Sites”.

3. Change Localhost To Localhost.com
required if you want a sub-domain multisite environment

Opening my hosts file in the terminal

Opening my hosts file in the terminal

What my hosts file looks like

What my hosts file looks like

As I pointed out at the beginning, per WordPress Settings Requirements, “You cannot choose Sub-domain Install (for a domain-based network) if the WordPress address (URL) is localhost or 127.0.0.1.” You can, however, “create a domain-based network on your local machine by using your hosts file to map another hostname [to your local IP address], so that you never have to use the hostname localhost.”

In other words, unlike http://localhost, your localhost URL needs to have a domain name, e.g. http://localhost.com. It’s now time to map http://localhost.com to our IP address so it can be used for our network domain.

Now, don’t freak out but the hosts file is locked so you’re going to have to use the Terminal. It’s really not that difficult. I promise. Just follow this nice, simple tutorial and, when you get to the part that says “simply append your new mappings”, append the following:

127.0.0.1            localhost.com

Note: Unfortunately, you cannot cover ALL of your sub-domains with the above line which means you will have to repeat this process for each site on your sub-domain multisite network. If you already know of a sub-domain you wish to use, I recommend you go ahead and add it to the list. For example, I will use http://cptonomies.localhost.com so I will also append the following:

127.0.0.1            cptonomies.localhost.com

Once you’ve finished the tutorial, you should now be able to visit http://localhost.com/. Do you see a WordPress Error screen that says “There doesn’t seem to be a wp-config.php file. I need this before we can get started.”? You do? Awesome! This means everything worked so click “Create A Configuration File” and let’s get configuring!

4. Install WordPress

A word to the wise going forward for sub-domain multisite setups: Make sure you’re on http://localhost.com, and not just http://localhost, while you’re installing WordPress because WordPress will save your domain in its settings.

With the server set up, and the WordPress files in place, you are now ready to visit http://localhost.com, which should initiate the WordPress installation script. WordPress provides a very helpful walkthrough to help you get everything setup properly.

Enter Your WordPress Database Connection Data

My WordPress database connection data

My WordPress database connection data

The first screen you’ll come across, that requires data, will be for the database connection. Enter the following information:

  • Database Name: the name of the database you just created – I used WPDEV
  • User Name: root
  • Password: root
  • Database Host: localhost
  • Table Prefix: wp_

Submit your information, finish the “famous five minute WordPress installation process”, and login to your WordPress admin/dashboard.

5. Create A WordPress Multisite Network
required if you want a multisite environment

Create a multisite network

Create a multisite network

The WordPress Create A Network codex page makes the process look pretty complicated so let me break it down for you:

  1. Allow for Multisite – follow step 2 on the Create A Network codex page.
  2. Go to your Admin. Click Tools -> Network Setup.
  3. Select “Sub-domains”. Enter network details. Click “Submit”.
  4. Don’t worry about the “Warning! Wildcard DNS may not be configured correctly!” warning.
  5. Follow the instructions on the page and click “Log In” at the bottom of the screen.
  6. Log back in to your admin.

Congratulations! You are now running a localhost WordPress Multisite! Now it’s time to add a second site.

Add a site to your WordPress network

Add a site to your WordPress network

Create A New Multisite Site

  1. Click “My Sites” at the top left (in your admin bar) and click “Network Admin”.
  2. Click “Sites”. You should now see that the only site listed is your primary site “localhost.com”.
  3. Click the “Add New” link at the top, fill out the form, and click the “Add Site” button.

Now, remember how I mentioned back when we were editing the hosts file that the hosts file has to list ALL of your sub-domains? Well now’s the time to make sure your new sub-domain is listed in your hosts file because, otherwise, you will not be able to view your new site.

If you’re able to view your site then your multisite setup is complete!!! Hooray!!! Time for a happy dance! I hope this tutorial was helpful. If you have any questions or issues, and a quick Google search didn’t help you out, feel free to comment and I’ll try to help you out as soon as possible.

If you want to take your local testing environment a step further, then be on the lookout for my next tutorial: “How To Turn Your Localhost WordPress Environment Into A SVN Repository”.

About Rachel Carden

Rachel Carden is a High Ed Web Developer with a fondness for WordPress. She's also a puzzle fiend who gets way too excited about programming and problem solving. And you thought you were a nerd.