WPMU Sub Domain Test Environment on the Mac

By August 19, 2011Tutorials


I have been using a Macbook pretty heavily for the last few months.  Anyway, I recently had to do some major upgrades to a WPMU or WordPress multi site installation.  So, I wanted to set up a test environment on my Mac.  Well, it became evident soon thereafter that doing sub domaining is something a lot of people want to do – and yet there isn’t a great tutorial for it.  Well, here is a tutorial.  And actually, I’m going to show you how to easily pull down your site, make changes, test, and upload to your production environment.  Now, there are better tutorials that allow for quick syncing, etc.  So, if you are doing this a lot, you will probably want to go that route.  Let’s get going.

As always, I say, if you know of a better way of doing something – let us know.  Remember, always be learning – it keeps you young!

So, the first thing you are gonna want to do is make a back up of your WordPress database.  WPMU databases tend to be very large.  That’s the nature of the beast.  By far the easiest, fastest, and simplest way to do this is by using mysqldump.  If you are not familiar with it – get familiar with it.  To do this, you will need ssh access to your server.  Don’t know ssh – you better learn if you’re going to be doing using WordPress multi site installations.

Dumping Your Database Using Mysqldump

So, jump on your server with ssh and enter this command:

mysqldump -u username -ppassword databasename > sqlfilename

Okay, so let me give you an example so you know what the heck this looks like.  Let’s say that we have a WordPress database, called WordPress, with the username of wordpressuser and the password of wordpresspassword and we want to dump it to the file wordpress.sql.  You will want to use the .sql file extension.  This is how this would look:

mysqldump -u wordpressuser -pwordpresspassword WordPress > wordpress.sql

I hope that makes sense.  This will take a while.  Go get a drink.  When it’s finished, you will see the wordpress.sql file in the directory.  Okay, we now have our content and our dumped database file.  Now we need to get it to our local Mac.  Please, don’t use ftp.  It takes waaaaay too long.  Start using rsync to move files around.  You’ll love it!

Moving files from the remote server to the local computer using Rsync

Okay, so now we are going to use rsync on the Mac.  You will want to use the terminal app.  If you have never used it, no worries – it’s a breeze.  Simply click on Applications and then go to Utilities.  You will see the terminal app in there.  At the command prompt, we are going to enter this form of command:

rsync -avz -e ssh remoteusername@domain:/remotedirectory/ /localdirectory/

Note: if your are using Hostgator or another hosting provider that uses a non-standard ssh port, you can input it like this:

rsync -avz -e “ssh -p portnumber” remoteusername@domain:/remotedirectory/ /localdirectory/

All this is doing is telling rsync to utilize ssh to sync files from that remote directory to the local one.  So, let’s say that you have your WordPress website in the following remote directory /home/users/dilbert/public_html.  Okay, let’s also assume that you want to put this on your local computer under the current user’s directory, like this /currentuserdirectory/website.  Let’s assume that our username here is dilbert and the domain is workingforafool.com.  Here is the example command:

rsync -avz -e ssh dilbert@workingforafool.com:~/public_html/  ~/website/

Just so you know, the ~ symbol denotes the home directory for that user.  When you issue that command, you will be asked for the remote server’s ssh password.  Go ahead and enter it.  This will dowload your website’s files to your local computer.  If your sql file is in that folder, it will dowload this as well.  Ohhhhh, I almost forgot.  You will also want to put the progress option in that command.  This way, you know how long the transfer will take.  simply do the command like this:

rsync -avz –progress -e ssh dilbert@workingforafool.com:~/public_html/  ~/website/

Now it tells you how much time you have to read a book, or exercise, etc.  After that is done, we will set up our local testing environment.

Setting up the MAMP testing environment

You will need apache and mysql running on the Mac.  Now, there are a couple ways of doing this.  I’ve done both.  Mac has apache and mysqld natively.  You could get that all going if you want.  Honestly, it’s just easier to install MAMP as it also includes phpmyadmin and a nice admin screen for it all.  So, go download and install MAMP now.  Once you have this up and running, you should be able to see the admin website for it.  First things first, go ahead and move all your WPMU files to the /Applications/MAMP/htdocs folder.

Before we go any further, we will want to make MAMP use the default ports for apache and mysql.  That’s because WordPress will have issues if you don’t do this.  Simply go to your applications and then MAMP.  This should bring up the configuration panel.  Select “Preferences…”, like this:

MAMP panel

Once you press “Preferences,” you will want to select the ports tab and set them to the default, like this:

MAMP default ports

Alright, now we need to get the database going.  The first thing we are going to need to do is do a search and replace on the sql file using the vim editor.  Now, I know there are other ways of getting this working.  And, as usual, I am all ears if you want to share your ideas around that.  Go to the handy dandy terminal app.  Browse to the directory that contains your sql file.  Now, type: sudo vim wordpress.sql or whatever you called your sql file.  This will take a while for vim to open the sql file if it is big.  Once it is open, hit “Esc” and type “:%s/domainname/localhost/g”  Obviously, you should input your domain name in the part where I have placed domainname .  What this does is replace all instances of the domain name with localhost.  This will ensure that your site works on the local machine.  Once that is done, hit “Esc” and type “:wq” to write the file and quit.  Now we are ready to import the sql file into the database you created.

Note: Don’t use phpmyadmin to import your database – it has a hard time with large files

Nope, we are going to go back to the command line and use mysql to import the large sql file.  This is the command you will enter:

mysql -u username -ppassword databasename < pathtosqlfile

So, if you have your sql file in your home directory, under a directory called “website” then you would enter the command like:

mysql -u root -proot wordpress < ~/website/wordpress.sql

Remember, MAMP’s default mysql user and password is root root.  Remember, you need to update your wp-config.php file to include the new database information as well!

Okay, once that is done, you should be able to get your site running.  But, we still aren’t done.  That’s because you will not be able to access any subdomains.  Yes, you can enter each sub site into a hosts file, but that’s stupid on a site that has tons of sub domains.  Also, if you plan on updating all those sites, if you are doing upgrades, the updater won’t be able to find these subdomains and will error out.  Anyway, here’s how you get sub domains working.

Getting your WordPress sub domains to work on your Mac

I knew that the answer was hiding in BIND.  That’s because it is a DNS resolver.  You need to get this working if you want to DNS resolution locally.  And this is exactly what you want.  I am indebted to Intridea for their post on getting this working on Mac.  I know how to do all this on different linux distros, but was green on different Mac syntax.  So, this was very helpful – thanx guys!

Note: Don’t set up the appname zone as they did.  You just need the localhost zone for now, so omit step 5 in their outline.  Also, if you want to check the localhost zone, you have to enter named-checkzone localhost /var/named/localhost.zone.  Lastly, the DNS settings can get lost from time to time.  If you find later that you can’t access sub domains, simply re enter them and they will work.

Once you do all that, you should have bind working and be able to browse all of your sub domains!!!!

Now, once you are done with your testing, upgrading, or whatever you need to do; it will be time to upload all this back to the server.  So, how does one go about that?  Here are a few tricks to get you going.

Use htaccess to show a maintenance page to everyone except you

This is a fun trick.  This allows you to work on your site while everyone else sees a maintenance page.  Simply replace your standard .htaccess file with the following:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REMOTE_HOST} !^xxx.xxx.xxx.xxx

RewriteRule $ /maintenance.html [R=302,L]

This will simply redirect all users to a maintenance.html page.  You will want to create this maintenance.html page and upload it to your WPMU root directory.  You will also want to input your IP address where I have xxx\.xxx\.xxx\.xxx  Don’t take out the escape slashes.  Those are needed to escape the dots in the IP address.  Once you do that, you should be ready to upload your improved WPMU site.

Using Rsync to upload your files from the local Mac to the Server

Really, we are just reversing this, but here u go:

rsync -avz -e ssh ~/website/ dilbert@workingforafool.com:~/public_html/

Remember, if you are using a non standard port for ssh, you need to put “ssh -p portnumber” in the command above.  Now, use phpmyadmin on your local Mac to export your database as a sql file. Now use vim search and replace to reinput your domain name into the file.  Use rsync to upload it to the server.

Getting your database imported into your server

Now, if you have full ssh access, this shouldn’t be a problem.  Just use mysql on the server side to import the sql file.  See my notes above that we used on your local Mac.  Now, for some hosts, they have strict security requirements and your sessions might expire.  In this case, even if you use nohup, mysql may still fail.  So, what do you do?

Well, you can use BigDump.  Go ahead and download it and follow the directions.  You have to dump your database to import it – so make sure you have a working copy!!!!!  Sit back, grab a drink and watch your database populate.

Remember, you need to change your wp-config.php back and .htaccess once you verify it’s all working.

I hope that helps!


Related Posts: