Monday, March 1, 2010

Remote command line for Drupal running on Windows Server 2008

Recently I set up a remote command line for Drupal. It has been available since the release of Drush. I have to work with many Drupal sites and I am a command line addict, because I think it’s more efficient than any other technique. Drush seems to the best solution for me to manage multiple sites easily.

This specific case covers Drupal 6.x running on Windows Server 2008 and IIS 7. This is not the “regular” platform for Drupal. It is originally designed to run on Apache, *nix environment - but if you have to integrate with Windows systems, in some cases it’s better to run on Win/IIS environment. For the starting scenario we have a fully installed Windows Server 2008 with IIS 7, PHP installed and one or more Drupal 6.x systems deployed.

If you are new to Windows 2008, there could be many issues installing these prerequisites. I recommend to use the Microsoft Web Platform Installer. You can install PHP and more by a few clicks of the mouse.

Installing Drush:

  1. Download the package from here.
  2. Unpack it to the desired work folder (I used the following path: C:\utils\drush)
  3. If the installation path is not added to your path, add it: right click on My Computer and select Properties. On the window select Advanced Properties then Environment Variables. Find the PATH system variable, and add your path to the end of the string (separated with a semicolon). PHP root must be on the path also (Web Platform Installer places it to Program Files (x86)\PHP on a 64 bit system, like mine).

image

After the installation I made some adjustments.

  1. Changed drush.bat to use a customized php.ini located in c:\utils\drush.
    To do this, I added the php calling parameter –cC:\utils\drush

    @REM See http://drupal.org/node/506448 for more information. @php.exe -cC:\utils\drush\ "%~dp0drush.php" %1 %2 %3 %4 %5 %6 %7 %8 %9
  2. Copied the php.ini from the php installation path to c:\utils\drush and changed some parameters. For example:

    error_log = d:\logs\drush_php_error.log – Use different error log file
    max_execution_time = 3000 – Give it time to run…
    max_input_time = 3000 
    memory_limit = 512M – A great pool    

Let’s see if it’s working. Open command line. Change directory to a Drupal root, and issue the following command: drush status . If it works, it prints something like this:

image

Well done, drush is working fine.

Issuing commands from any folder

Drush is based on the great idea of working with the Drupal installation that is in the current directory. It would however be better for me, if I could issue commands fast from anywhere. So I figured out that I can create a little script for each site:

@echo off set ORIGINALPATH=%cd% cd /D d:\wwwroot\testsite\httpdocs call drush.bat %* cd /D "%ORIGINALPATH%"

This simple cmd script changes the current directory to the required Drupal site, executes drush.bat with all paramters and restores to the current directory to the original.

So, now if I issue testsite status anywhere, I will get the same output as is on the previous screenshot.

Making it remote

It’s great to work on the remote system with Remote Desktop utility, but sometimes it is more trouble that it’s worth. It would be better if I could issue these commands on my local workstation’s command line.

Note: The server in the example is not on our local network. It accessed via a VPN connection. To use the following method, you have to be on local network with the server or use some kind of VPN.

For the remote access we have to configure Windows Remote Management. This is Microsoft’s implementation of the WS-Management protocol. If it’s configured for the first time and you do not need customization, just issue the following command: winrm quickconfig. After it finishes, the Windows Remote Management service starts and will be configured to start as necessary from now on. It also sets the firewall exception as needed and creates the default listeners. The output will be something like this:

WinRM is not set up to allow remote access to this machine for management. The following changes must be made: Create a WinRM listener on HTTP://* to accept WS-Man requests to any IP on this machine. Enable the WinRM firewall exception. Make these changes [y/n]? y WinRM has been updated for remote management. Created a WinRM listener on HTTP://* to accept WS-Man requests to any IP on this machine. WinRM firewall exception enabled.

To test if the WinRM service running fine, I issued the following command (on the server): winrs –r:http://localhost testsite status

image

Great! We have the same output. The following step was to set up the trusted hosts on the server. The system allows to set up single hosts or networks. So, I added the VPN client’s IP range to the Trusted Hosts:

imageWe have to add our server to the trusted hosts on the client workstation as well (Windows 7):

image

After this we can issue commands from the local computer in the following form: winrs –r:servername command params. This form is not too user friendly… To make it more efficient we can create “shortcut” commands for the websites. The following is for my testsite:

@winrs -r:http://10.20.0.10 testsite %*

Finally, we have a working command line to Drupal. I can easily issue commands to my remote sites. Some examples:

Put the site to maintenance mode:

image

Download a new module:

image

And more…

There are many possibilities to expand these functions to get more and more easy to use remote management functions. Drush saves me a lot of time. Thanks for the developer Moshe Weitzman! It’s great!

References:

Drupal – Open Source CMS

Drush – Drupal Shell many thanks goes to Moshe Weitzman

Microsoft Web Platform Installer – Application to install required components, and complete website engines

Windows Remote Management