Freigeben über


Migrating Drupal to a Windows Azure VM

Last week, as part of my preparation for DrupalCon Munich, I investigated how to migrate a simple Drupal website to Windows Azure Websites. While Windows Azure Websites makes it easy to get a website up and running quickly and is great for many applications, it doesn’t allow you full access to servers. This may be a non-starter if you are considering alternative hosting options for a large and/or complex Drupal website. Fortunately, with the June 7th update to Windows Azure, durable VMs are now an option in the Windows Azure cloud. You can choose from any of the images in the Image Gallery (which has multiple distributions of Linux and multiple versions of Windows Server), or you can upload your own image. In this post, I’ll walk you through the steps of migrating a Drupal website (running on CentOS) to a Linux VM (also CentOS) provisioned from the Windows Azure VM Image Gallery.

Before diving in, I’ll point out that most of the steps described in the post are completely independent of Windows Azure. In the end, most of what I describe here is how to move Drupal from one Linux machine to another. Once you have created a Linux VM in Windows Azure, the migration steps differ little from any other migration. In fact, if you have migrated a Drupal installation before, once you get past the Azure-specific steps you may have suggestions for better ways of doing things than I do (which I’d be happy to hear in comments).

For this tutorial, I’ll assume you have a Windows Azure account. If you don’t, you can get one by signing up for the Windows Azure 90-day free trial.

Create a Windows Azure VM

Create a VM from the command line

The easiest way to create a Windows Azure VM is to use the Windows Azure Command-Line tools for Mac and Linux (that tutorial has instructions for installing the tools). (As you can see from my screenshots, these tools also work on Windows.) Once the tools are installed, you can view the available images with the azure vm image list command:

azurevmimagelist

I’ll use the available CentOS image to create a virtual machine with the azure vm create command (my VM name is bswantestvm, the username for the VM is bswan, and the password for that user has been shaded out):

azurevmcreate

Next, you’ll need to open up some ports on the VM with the azure vm endpoint create command. You will need to open port 22 for SSH access, port 80 since this will be a web server, and port 3306 for copying your MySQL database. Here’s how to open port 22 for the bswantestvm VM, but you will need to repeat this for ports 80 and 3306:

image

Create a VM from the management portal

If you prefer a user-friendly UI to the command line, you can create a VM from the Windows Azure portal. This tutorial, Create a Virtual Machine Running Linux, will walk you through all the necessary steps. The only additional steps that are required are that you create an endpoint for port 3306 (just repeat the steps shown for creating an endpoint for port 80 – port 22 should be open by default for SSH access).

That’s the Azure-specific stuff. As I mentioned earlier, the steps from here are the same as they would be for moving Drupal from one Linux machine to another.

Install Apache-MySQL-PHP

There are lots of good tutorials out there that cover how to install Apache, MySQL, and PHP on a Linux server, so I won’t write another one. I will, however, point you to a tutorial that I found helpful, How to install LAMP on CentOS 6 server (skip the section on creating virtual hosts), and add a few tips:

  • Login to your Azure VM via ssh using the user name/password you created earlier:
 ssh user-name@vm-name.cloudapp.net
  • Since your user is not the root user, you will have to run commands using sudo. (Your user has super user privileges.)
  • When running mysql_secure_installation, allow the root user to login remotely (or create a user that can).
  • While you are logged into MySQL, create the database for Drupal:
 create database drupal;
  • The tutorial doesn’t mention this, but I believe you need to restart Apache after you install PHP:
 sudo /etc/init.d/httpd restart
  • To test that Apache and PHP are working properly, create an  info.php file in the /var/www/html directory that calls phpinfo(). The easiest way to to this is to use vi. When it’s created, browse to https://your_vm_name.cloudapp.net/info.php.
  • Modify permissions on /var/www/html so that your user can write to that directory without super-user privileges.
 sudo chmod 777 /var/www/html

You will likely want to change those privileges back after you have copied your Drupal files.

Tip: Now that you’ve done the work to create a complete LAMP image, you can capture this image for use later. You can use azure vm image capture <vm-name> <target-image-name> from the command line, or for step-by-step instructions on how to capute an image using the Azure portal, see How to Capture an Image of a Virtual Machine Running Linux.

Copy Drupal database

If you followed my tips above, you have created the Drupal database on the Azure VM and you have allowed root to login remotely. Now, from your existing Drupal server, you can use mysqldump to copy the database to your Azure VM (this assumes that both local and remote database names are drupal):

 mysqldump -u root --password=root_password drupal | mysql -h vm_name.cloudapp.net -u root --password=root_password drupal

Depending on the size of your database, this will take a few minutes.

Note: This command will fail unless you opened port 3306 on your VM.

Copy Drupal installation

Again, if you followed by tips above, you modified the permissions on the /var/www/html directory so that your user could write to it without super user privileges. With that in place, you can use scp to securely copy your local Drupal directory to your Azure VM over ssh:

 scp -r root@localhost:/var/www/html/drupal your_user@vm_name.cloudapp.net:/var/www/html/drupal

Depending on the size of your local Drupal directory, this will take several minutes.

The last thing to consider before you are finished is database access on your Azure VM. You may need to edit the /drupal/sites/default/settings.php file or create a MySQL user/password that matches the settings in that file.

That’s it. Of course, you may want to modify your Apache, MySQL, or PHP installations to match those on your local server, but there is nothing particular to doing this in a Windows Azure VM. Creating a Windows Azure VM is easy…the migration path is then the same as it would be with any other Linux-to-Linux migration.

Thanks.

-Brian

Comments

  • Anonymous
    March 22, 2013
    Very helpful post.  I'm glad others have tried this. I'm interested in possibly doing this for a corporate, public-facing website.  I've tried installing the Acquia Drupal 7 release from the Management Portal, but it seems to run slow. My assumption is because its hitting the Drupal content DB externally.... Does installing Linux on the VM alleviate this lag time bc the mysql database is "local"?  I would think this might fix my performance problem?

  • Anonymous
    April 01, 2013
    In general, putting all the things your service relies on in the same VM/Machine is going to speed things up. To a point. As your service gets more users, you eventually reach a point where scaling back out to multiple services/VMs gives better performance. There's no hard and fast rule here; you have to test under the expected load and see which gives the best performance. Another possible approach would be to upgrade to a paid version of MySQL. The MySQL Database instance you get with Windows Azure Web Sites is provided by ClearDB. The default instance provided for web sites is the free/low performance instance, and their paid options offer higher performance. www.cleardb.com/.../azure has information on their offerings. Hope this helps.