Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

MacOS X Local Mirror

Here's what I did to set up a local mirror of my WordPress blog on Mac OS X.

Motivation and Assumptions

Firstly, why do this? Well I can think of a few reasons:

  • keeping an up-to-date and working backup
  • using it for testing changes to the blog before you inflict them on the world. Like formatting, software updates, or anything really.
  • It's fun (except for the MySQL setup, which we'll get to)

The basic idea is to take a snapshot of the database and file system your hosting provider, download it and extract it locally, then be able to view the result.

So I'm assuming that you have a blog at http://example.com (no subdirectory). Substitute your own blog name for this in the instructions below. For the sake of this HOWTO I'm assuming that you are logged in as alastair (MacOS calls this your "short name").

I'm assuming also that you're running Mac OS X 10.3 and for the sake of simplicity want to install the minimum software. Or in other words use the standard OS distribution as much as possible. This takes advantage of patches and other updates, amongst other benefits. Mac OS X 10.3 comes with Apache and PHP pre-installed, all we need is MySQL and WordPress. (This HOWTO was written when 10.3 was current, but the procedure is identical on 10.4, as I have verified personally).

Configure Apache and PHP

First let's make sure that Apache and PHP are configured. Check that Apache is started in the Sharing Preference Pane:

Personal Web Sharing

Then go into /etc/httpd/httpd.conf and uncomment (remove the # at the start of) the following lines:

LoadModule php4_module        libexec/httpd/libphp4.so
...   
AddModule mod_php4.c

You will need to edit this as root. One way to do this is open a terminal session and type sudo pico /etc/httpd/httpd.conf, but I'm sure there are others.

Now let's restart Apache...

sudo apachectl graceful

...and see if it worked. Create a file in your Sites directory (within your Home directory) named info.php. Edit it to contain the following:

<?php phpinfo(); ?>

Then open up http://localhost/~alastair/info.php in your browser (remember to substitute for your own name) and marvel at the result. You should get a detailed listing of all information that PHP has to hand. Congrats, now on to...

Create a example.mirror static site

If you're running a blog at the top-level of a host (like http://example.com and not like http://example.com/blog) you probably have your intra-site links set up to use absolute paths. In fact I don't think WordPress gives you a choice in the matter for certain links like stylesheets.

So the problem with creating a local mirror is that you need to create a new hostname for it, so as not to conflict with any other content you're running on your MacOS system. If you don't have this need, feel free to skip to the end of this section, and just mirror your blog's filesysem into /Library/WebServer/Documents.

What we're going to do is create a new, bogus, hostname and have it served locally. The .local top-level domain is taken already (it's used for Rendezvous), so we'll pick .mirror. The local mirror will be at http://example.mirror

The first thing to do is to add this host name to your /etc/hosts file. Just append the following:

127.0.0.1 example.mirror

You should be able to ping this address now:

alastair $ ping example.mirror  
PING example.mirror (127.0.0.1): 56 data bytes  
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.159 ms  
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=5.078 ms  
^C  
--- example.mirror ping statistics ---  
2 packets transmitted, 2 packets received, 0% packet loss  
round-trip min/avg/max = 0.159/2.618/5.078 ms

Now go and add this as a VirtualHost in your Apache configuration. What you may want to do is edit /private/etc/httpd/users/alastair.conf (or your username) instead of the main httpd.conf file. This minimises changes to the main Apache configuration file and simplifies upgrades. In any case you need to add something like the following:

NameVirtualHost *:80
<VirtualHost *>
    DocumentRoot /Users/alastair/Sites/example.com
    ServerName example.mirror
    <Directory "/Users/alastair/Sites/example.com">
        Options FollowSymLinks
        AllowOverride All   
    </Directory>
</VirtualHost>

This creates a new virtual host at the specified document root. This directory is where we're going to store the mirrored files.

Restart Apache again: sudo apachectl graceful

Now is as good a time as any to actually copy the files from your hosting provider to this local mirror directory. You can use whatever tool you like. If your provider supports cPanel, you could just get a home directory backup, and extract the relevant files (probably everything in public_html).

I use Interarchy, which allows you to do a true mirror (ie only copy the files that need to be copied). If you use Interarchy too, please make sure that it is set to use UNIX line endings and not Mac.

Now you should be able to view the static portion of your blog. A good test for this is the WordPress README file, which you will now find at http://example.mirror/readme.html. If all has gone well so far you should be reading the WordPress readme.

Install and Configure MySQL

Now the un-fun bit. Well it was for me anyway. There are several different flavors of MySQL for Mac OS X, and I tried a few of them, but the standard distribution from MySQL AG seemed fine to me. The question is: which version? The answer depends on the version of MySQL you wish to mirror. In other words: which version does your hosting provider use?

As a general rule, you should probably use the same (ie x.y) version that your hosting provider uses. For version 4.1 and later, we will be using a workaround for a change to the password hashing algorithm that was introduced in 4.1. Not that that should worry you.

All you need to do is download the chosen version (ie 4.0, or 4.1 or what-have you) and install it. The instructions for installation, post-installation configuration and securing the initial accounts are long and detailed, so here is the PowerPoint Slide version:

  • Run the installer for both the mysql software and the startup item
  • (Mac OS X 10.4.4+ only) Update your /etc/my.cnf file to point to the new secure location for the socket. See note below.
  • Start the database server with sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
  • cd /usr/local/mysql
  • sudo scripts/mysql_install_db --user=mysql
  • bin/mysqladmin -u root password a-secure-password

Mac OS X 10.4.4+ users need to set up the MySQL server to listen on a socket other than the default one. To do this, create a /etc/my.cnf file with the following contents:

[mysqld]
socket=/var/mysql/mysql.sock

Then create the directory if it doesn't exist:

$ sudo mkdir /var/mysql
$ sudo chown mysql /var/mysql

This is to match the new location expected by Apple's PHP installation. Again, you only need this stuff for Mac OS X 10.4.4+ (although it probably won't hurt on earlier releases). See here for (slightly) more information.

Note that the root password set in the last step above doesn't have to match the WordPress password we use later. In fact it's probably a good idea if it doesn't. Pick something you can remember. At this point it's probably a good idea to verify that you can access the database as root using the mysql tool:

mysql $ bin/mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3 to server version: 4.1.15-standard

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases;
+----------+
| Database