Running WordPress Multisite with MAMP

Inspired by Migrating WordPress to a New Server on the WPMU.org blog I decided to attempt to move my WP-BP test install from sergius to a MAMP install on my MacBook Pro (OS-X 10.6.8). Using these resources:

especially the last one, as a guide.
I saved the database and code

$ mysqldump testbp -u testbp_user -p >testbp-wordpressdb.sql
Now save the code:
$ tar -cvzf ~/archives/sabb2011/test-bp-code.tgz test-bp/

and copy/ftp to new server location. But where was this to be? All the spiel assumes that WP is the only application that you operate in the domain and so they always show dumping the code into the main public_html directory. But I want to run moodle etc as well, so I’m putting it in a subdirectory of htdocs which is the root documents dir for MAMP. The full location for the document root will be /Applications/MAMP/htdocs/test-bp
Having setup MAMP which is quite straightforward you want to reset the Apache port to 80.

Note:
Make sure that Web Sharing is off — System preferences : Sharing : uncheck Web Sharing service

Having copied and untarred the code into the chosen root directory and created the database with user, there are three configuration files that now need to be modified.

Configuration Files to modify

  1. Hosts. WordPress (and we’re talking multisite here) wants to run directly from a top level domain. So, unlike moodle which can run quite happily from something like domain.org/~markpea/test-moodle-site/1 wordpress wants to have the whole domain to itself with wordpress-test.domain.org . So, since we’re working on a testing system with no real external users we can make up a domain and use that. But how to direct the made up domain name to our local system? Answer: use the old fashioned hosts file located in /private/etc/hosts and associate your made up name with localhost. Here’s what I did
    127.0.0.1	localhost bptest.flyingnematode.org

    . Having edited this (you’ll need to use $sudo vim hosts to circumvent hostile permissions) flush the DNS cache with $ dscacheutil -flushcache.

  2. VHOST — virtual host. Having redirected the domain to the localhost the web server process — Apache — has to be informed where to direct the name next. This is done by mean of virtual host directives which are run indirectly from the Apache configuration file httpd.conf. In MAMP (and also FreeBSD but Linux works slightly differently) user configurable vhost directives are located in MAMP/conf/apache/extra/httpd-vhosts.conf. I merely copied the vhost directives I use on my sandbox and adjusted the paths to suit. Example vhost
    <VirtualHost *:80>
            ServerName bptest.flyingnematode.org
            ServerAdmin markpea@myemailaddress.com
            DocumentRoot "/Applications/MAMP/htdocs/test-bp"
            DirectoryIndex index.html index.php
            ErrorLog "/Applications/MAMP/logs/test-bp_apache_error.log"
            CustomLog "/Applications/MAMP/logs/test-bp_apache_access.log"
    combined
           <Directory /Applications/MAMP/htdocs/test-bp>
                     AllowOverride FileInfo Options
           </Directory>
    </VirtualHost>

    Explanation:

    1. ServerName should exactly match what you used in the hosts file
    2. DocumentRoot is the full path to where the code is located
    3. Logs can be customized for this particular WP install. This is nice because you can have different logs for different installs.
    4. My understanding of the AllowOverride option is that it allows .htaccess files to operate in the given directory. Although this is set as default on the MAMP system it’s well to include just in case

    The final stage is to enable the vhost processing to operate (it’s commented out by default). Just edit the MAMP/conf/apache/httpd.conf to remove the comment marker as follows

    # Virtual hosts
    Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

    Obviously, having edited these files you need to restart Apache which can be done either with sudo apachectl restart on the command line or use the OS-X MAMP app to stop and start the Apache & MySQL servers.

  3. create a new database. At this point for simplicity’s sake I decided not to use the existing database but to start from scratch using the command mysql -u root -p (obviously I’ve already reset the root password!) to create a new database
    mysql> CREATE DATABASE testbp DEFAULT CHARACTER SET utf8
    COLLATE utf8_unicode_ci;
    mysql> GRANT ALL PRIVILEGES ON testbp.* TO <username>@localhost
    IDENTIFIED BY '***********';
    mysql> FLUSH PRIVILEGES;
  4. Where testbp is the arbitrary database name you choose, <username> is the name you choose to operate the database (obviously leave out the < and >) and ‘****’ it’s password.

  5. wp-config.php. So now we’ve configured the web server Apache to look in the right place and hook to index.php and the final piece of the puzzle is configuring the wordpress system itself using the wp-config.php file. In wp-config.php configure the database name, user, password using the parameters from the create database stage and the hostname has to be ‘localhost’ and make sure to configure charset and collate
    /** Database Charset to use in creating database tables. */
    define('DB_CHARSET', 'utf8');
    /** The Database Collate type. Don't change this if in doubt. */
    define('DB_COLLATE', 'utf8_unicode_ci');

    as well. Configuring multisite is not intuitive however. You have to add the following directive just before the end o the file : define('WP_ALLOW_MULTISITE', true); and then save but don’t quit. Now start up your WP with the domain URL you used (mine was bptest.flyingnematode.org) and it’ll ask you to configure an admin username and password. Write this down somewhere. You now logout and then log in with the admin username / password combo you’ve just forgotten! Click on Tools : Network Setup (or it might be Settings : Network Setup) and here you’ll find code to add to the wp-config.php file and .htaccess. Do this while the WP is still running (command line editing with vim makes this so much easier), save wp-config and .htaccess and logout of WP. Now when you login as admin again you’ll see ‘Howdy admin’ at the top right and pulling this down will reveal either Site Admin or Network Admin screens. Yea — it works!

Actually, you’ve edited five files, but there you are.

But I really want to use my current database and code

Here’s what to do, but as yet it is untested by me.
Unfortunately (unlike moodle again) the WP domain use for the site is hard coded into the database records. What this means is that if you want to move the site to a different domain (yes) we’re going to have to change that hard coded URL. This is actually quite straightforward using the command line (in fact on a Unix system everything turns out to be more straightforward this way). So we’re going to edit the dumped database sql code, create the new database with the same settings (name, username, password) as before (for ease of operation only) and then use the same wp-config.php file.

  1. Fix the exported database
    $ sed -e 's;blogs.sergius.earlham.edu;bptest.flyingnematode.org;g'
    testbp-wordpressdb.sql >testbp-wordpressdb-fix.sql

    This uses the stream edit sed to to do a global substitute s; .... ;g of the old domain name blogs.sergius.earlham.edu for the new domain name bptest.flyingnematode.org from the exported database testbp-wordpressdb.sql and outputting to the fixed db testbp-wordpressdb-fix.sql

  2. Create a fresh database as above (use existing name, username, etc)
  3. Import the fixed database
    $ /Applications/MAMP/Library/bin/mysql -u <username> -p -D testbp
    <testbp-wordpressdb-fix.sql

    Where <username> is the username you have created the database with above and testbp is the newly created database name. You will be prompted for the database user password.

Now, having untarred the code into a suitable directory, configure hosts and VHOST as above with the appropriate directory location and configure your wp-config.php file accordingly. Keep the current multisite directives and if the paths match, you should be good to go. I have not tested this yet so I’m not sure of it’s outcome.

Problems

  • Check paths in hosts and httpd-vhosts.conf. Make sure that httpd.conf is calling the vhosts. Check permissions. On MAMP Apache runs as the local user so ownership should not be an issue (normally it runs as user ‘www’ or ‘nobody’).
  • Check for consistency between wp-config.php and the database setup paramters you used (you did write these down?)
  • The error log and custom log that you specified in httpd-vhosts.conf which should be located in /Applications/MAMP/logs can often throw a lot of light on why things are pear shaped. Use tail -20 <log file name> to examine.

Hope this helps someone ………

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s