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:
- Moving WordPress to a new domain or server
- Running WordPress locally on Mac OS X Lion
- Installing WordPress Multisite with MAMP on OS X
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
Having setup MAMP which is quite straightforward you want to reset the Apache port to 80.
- 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
- 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/hostsand 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 hoststo circumvent hostile permissions) flush the DNS cache with
$ dscacheutil -flushcache.
- 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 firstname.lastname@example.org 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>
- ServerName should exactly match what you used in the hosts file
- DocumentRoot is the full path to where the code is located
- Logs can be customized for this particular WP install. This is nice because you can have different logs for different installs.
- 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
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 restarton the command line or use the OS-X MAMP app to stop and start the Apache & MySQL servers.
- 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;
Where testbp is the arbitrary database name you choose, <username> is the name you choose to operate the database (obviously leave out the
>) and ‘****’ it’s password.
- 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.phpfile. 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.
- 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; .... ;gof 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
- Create a fresh database as above (use existing name, username, etc)
- 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.
- 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 ………