Changing a Drupal Site’s Domain
I recently needed to change a Drupal installation from www.example.com to subdomain.example.com. Here’s how I did it. There is probably a shorter way, but these steps leave the current site untouched until you are sure the new one works.
- Get ready
- Clear Drupal cache
- Dump current Drupal database to a backup file
- Create new database and grant privileges
- Run stream editor against db backup file to fix paths
- Load new database
- Ensure current Drupal site directory is updated in svn
- Export current site from repository to new directory
- Edit files/default/settings.php to point to new db
- Configure Apache to use new directory
- Enter a new A record in DNS
- Test
To get ready, turn off css cache and make sure $base_url is commented out in your files/default/settings.php file. I also disabled clean URLs, not sure if it’s necessary. Your Drupal directory should already be under subversion control. Put up your maintenance page since we are going to clear cache.
The next step is to clear Drupal cache and dump the database to a backup file.
Put the following code in a page, and set up a menu to access it. Make sure you restrict access to admin for this menu. Don’t be lazy and do it from the command line - it’ll be faster this time and slower next.
db_query("DELETE FROM {cache} WHERE 1");
db_query("DELETE FROM {cache_filter} WHERE 1");
db_query("DELETE FROM {cache_menu} WHERE 1");
db_query("DELETE FROM {cache_page} WHERE 1");
Ok, for the command line purists, it’s like this
mysql> DELETE FROM cache WHERE 1;
Then dump the database to a backup file
$ /usr/bin/mysqldump dbname > mysite-backup.sql
Make the current site accessible again by taking down your maintenance page.
Create a new database and grant privileges
mysql> create database newdbname; mysql> grant all on newdbname.* to 'drupaluser'@'localhost';
Before loading the new database we need to run a stream editor against the backup file and change any hard coded paths.
Check the backup file by grep’ing for the previous subdomain and path of the current install directory
$ /bin/grep -r mysite * $ /bin/grep -r www *
Then edit something like this
$ /usr/bin/perl -pi.bak -e's/http:\/\/www.example/http:\/\/subdomain.example/g; > s/\/html\/mysite/\/html\/mynewsite/g;' mysite-backup.sql
And load the new database
$ /usr/bin/mysql newdbname < mysite-backup.sql
My Drupal installation was in /var/www/html/mysite under subversion control. After cleaning up the working directory and checking in all changes, I exported mysite into a new working directory.
$ cd /var/www/html $ /usr/bin/svn export file:///var/svn/repos/mysite mynewsite
Make sure the files/ directory is writable by the web server process owner.
Edit files/default/settings.php and change the name of the database, and make sure $base_url is commented out.
Configure a new virtual host in /etc/httpd/conf/httpd.conf for subdomain.example.com. Copy the <virtualhost> entry for www.example.com and change the ServerName, DocumentRoot, and log file name. If you have any Directory entries, change the path to your new directory. Check your configurations changes
$ /usr/sbin/apachectl configtest
And restart
$ /usr/sbin/apachectl graceful
Make sure you have an A record in DNS for subdomain.example.com
Finally test every page.
Thanks so much!!! Had a similar problem and by tweaking your steps I was able to fix mine. Just wanted to let you know. Good Work :-)
Comment by Richard — September 6, 2007 @ 6:04 pm
You’re welcome Richard. Let us know what tweaks were required for your situation.
Comment by trekr — September 6, 2007 @ 10:14 pm