Installing and configuring memcached and PHP on Windows

After upgrading the CSFBL forums to vBulletin 4.0, I noticed that performance was slightly worse than in the previous version. A little searching revealed that vBulletin supports memcached (an in-memory distributed caching system). Since I’ve got RAM to spare, I figured this is worth a shot.

Unfortunately, getting memcached running on the server (Windows Server 2008 R2 64-bit) took a few tricks, and getting memached running through IIS/PHP was another. To help other people through the same process (and to remind myself in the future), I’ll share the installation and configuration steps that worked for me below.

Downloading and configuring memcached

The official distributions of memcached are written for Linux systems, so the first task is finding Windows binaries. The memcached project site, fortunately, has links to Windows binaries, which are hosted by NorthScale. Both 32-bit and 64-bit versions are available.

(Note that NorthScale also offers their own free distribution of memcached, but I was unable to get this to run on my system.)

Versions of memcached prior to 1.4.5 supported a command-line option that would register memcached as a Windows service (as in memcached -d install), but this option was removed in version 1.4.5. The simple alternative is to schedule memcached.exe to run using the Task Scheduler service (Windows 2008/Vista/7).

You can create a task to run memcached on system startup using the following command line:

schtasks /create /sc onstart /tn memcached /tr "'c:\dev\utils\memcached-amd64\memcached.exe' -m 128"

Note the -m 128 argument; this tells memcached to use up to 128MB of RAM. There are other command line arguments available; most useful aside from -m are -l (to specify what IP addresses to bind to) and -vv (to add verbose logging to the console, useful for testing).

Integrating memcached with PHP

In order for PHP to use memcached, you must download the PHP memcached library and add it as an extension to PHP.

PHP extensions can be downloaded from http://downloads.php.net/pierre. Many different extensions are in here; the one I used was php_memcache-5.2-nts-Win32-vc6-x86-20090408.zip. This extension matches two key requirements:

Getting the right version of the extension is important; download the thread-safe version, or the PHP 5.3 version, and it simply won’t work.

Once downloaded, take the php_memcache.dll and put it in the ext folder in your PHP directory (for me, c:\Program Files (x86)\PHP\ext). Then, open the php.ini file (in your PHP directory) and add the following line to the end:

extension=php_memcache.dll

Restart IIS (from the command line, type iisreset), and if you did everything right, memcached should now be available to PHP. If you want to check, you can create a phpinfo page; if php_memcache is listed in the output, the extension is registered correctly.

Other links

To find out more about PHP, memcached, and Windows, check out the following links.

Can’t install TFS, or the .Net Framework, or almost anything? Check your security policies!

On a newly-rebuild Windows 2003 server, I set out to install TFS 2008. After installing SQL 2005, and SQL Reporting Services, and SQL Analysis Services, and SQL 2005 Service Pack 3, I fired up the TFS installer, only to ultimately get the dreaded “Send Report/Don’t Send Report” dialog box.

Team Foundation Server encountered a problem during setup

Nice! Looking at the install log was so much more revealing.

[09/14/09,13:33:33] Microsoft .NET Framework 3.5: ***ERRORLOG EVENT*** : Error code 1603 for this component means "Fatal error during installation."
[09/14/09,13:33:33] Setup.exe: AddGlobalCustomProperty
[09/14/09,13:33:33] Microsoft .NET Framework 3.5: ***ERRORLOG EVENT*** : Setup Failed on component Microsoft .NET Framework 3.5

Odd, why won’t the .Net Framework 3.5 install? Shouldn’t be hard to fix by downloading the .Net 3.5 installer and installing it manually. Or should it? That didn’t work, either. Again from the install log.

[09/14/09,13:42:31] WIC Installer: [2] Error code 1603 for this component means "Fatal error during installation."
[09/14/09,13:42:31] WIC Installer: [2] Setup Failed on component WIC Installer
[09/14/09,13:42:33] WapUI: [2] DepCheck indicates WIC Installer is not installed.

What does Windows Imaging Component have to do with anything? Probably nothing, but Windows Installer does, so let’s take that route. I download the latest Windows Installer installer (!) and attempt to install (!!) manually. Too bad that didn’t help, either — but at least this time I got an error message seemed to point me in the right direction.

Setup Error: You do not have permissions to update Windows Server 2003.

Now we’re getting somewhere. Googling that exact error message brought me to a Microsoft knowledge base article (KB888791) which told me:

Update.exe version 5.4.1.0 and later versions require that the user who installs the software update is an administrator with certain user rights.

A quick look at the policy settings on the server showed me that the Administrators group didn’t have the “Back up files and directories” right, as shown below.

A quick request to IT to grant the Administrators group the missing right, and viola! TFS, and other software, is finally installing.

Apparently, this may have been the root cause issue for software not installing or uninstalling properly a week or so ago, when I put in the original request to have the server rebuilt, which leads me to wonder. If Update.exe knows what rights it requires, why doesn’t it check for them, why doesn’t it provide a clear error message indicating what is missing, and why doesn’t this information bubble up appropriately to MSI installers that use Update.exe?

The world may never know.

A free replacement for the Windows defragment utility

Defragmenting hard drives is something that is often unnecessary, but when it is necessary, most people run the built-in Windows “Disk Defragmenter” utility. It’s serviceable, but there is a better option: JkDefrag.

There’s a few things that make JkDefrag an improvement over what Windows offers:

  • It runs on anything that mounts like a disk drive — including USB drives and memory sticks.
  • You can run it from Windows, from a command line, or as a screen saver.
  • It offers several different optimization strategies.
  • It can be configured to defragment specific drives, files, or folders, or to exclude defragmenting specific drives, files, or folders.
  • You can run it in the background and tell it to run at less than full speed.
  • It’s continually developed by a person who you can actually talk to via an online forum.
  • There’s no installer — just extract files from a ZIP archive into a directory and run the executable.
  • It’s free, as in free beer, and open source.

Hats off to Jeroen Kessels for writing a fine utility and making it available for free. He doesn’t even ask for donations (too bad, because I’d have sent him a few bucks if he did!).

Separating SQL script files generated by Microsoft SQL (by type)

There’s one great feature in SQL: the “Generate Scripts” command. Unfortunately, it has one limitation: the default filenames of scripts look something like this:

dbo.fnc_PlayerValue.UserDefinedFunction.sql
dbo.UserSelect.StoredProcedure.sql

I’d much prefer the filenames to match the object name, without the owner (‘dbo’) or object type. In other words, I’d prefer the above two files to look like this:

UserDefinedFunction\fnc_PlayerValue.sql
StoredProcedure\UserSelect.sql

How do we get from point A to point B without a lot of manual file copies and renames? We use the FOR command!

First, create a subdirectory for each object type (Table, StoredProcedure, UserDefinedFunction, View, Schema, Trigger, and User), then run the following from a command prompt.

for %i in (*.User.sql) do for /f "delims=., tokens=1-3" %j in ("%i") do move %i %k\%j.%l
for %i in (*.Schema.sql) do for /f "delims=., tokens=1-3" %j in ("%i") do move %i %k\%j.%l
for %i in (*.Trigger.sql) do for /f "delims=., tokens=1-3" %j in ("%i") do move %i %k\%j.%l
for %i in (*.sql) do for /f "delims=., tokens=1-4" %j in ("%i") do move %i %l\%k.%m

The command above will look for each file with a *.sql extension in the current directory. For each of those files, it copies it to a directory based on the type of file, and ensures the new file only includes the object name and the .sql extension. So much cleaner now!

Note that this will not work if any folder in the full path to the SQL files has a period in it!

Updated 2009.11.10 to support Trigger, User, and Schema scripts.

Solving distorted sound issues in Boot Camp

I’ve been running a rather expensive Windows Vista computer lately, thanks to Boot Camp. However, two problems continue to plague me:

  1. The latest Boot Camp update from Apple will not install successfully.
  2. Sound is distorted when playing games, typically those using multiple sound channels (i.e. pretty much all games).

Fortunately, I finally found the fix for #2. It wasn’t Vista SP1, and it wasn’t figuring out a way to install the latest Boot Camp drivers (still can’t figure that one out).

The solution: Download and install the latest Realtek HD drivers. Thanks to a post on mac-forums.com which pointed me to the Realtek High Definition Audio drivers page.

One odd quirk: It took two installations of the Realtek drivers before it worked. After the first installation (which removed the old drivers) and a restart, there was no sound at all in Windows. A re-installation of the same Realtek drivers (and another restart), and it’s all working fine…

Just in time for Age of Conan, which should be arriving in a day or two! (It’s for work, really… I’m doing research for the soon-to-be-released web site, www.aochub.com).