Sunday, April 10, 2011

Installing Windows Server 2008 EE with Ruby on Rails and LDAP (Part Five)

At some point in our application we'll be automating a few things like our server start.  As we're running an apache load balancer and a fast HTTP server in Thin, it might be easier if we can create a service for our Thin server since Apache already has one for itself.  With Windows Server 2008, unfortunately there's no resource kit currently published by Microsoft.  In addition, if you try to run the SC command to create a service, it will point to a missing srvany.exe file.  We can get around this by downloading and installing the windows server 2003 resource kit locally, and copying the srvany.exe file into our C:\Windows\System32 directory.  Let's do that now.

First, grab a copy of the windows server 2003 resource kit here:

If the download link becomes outdated, just google or bing a search for Windows Server 2003 Resource Kit and you'll find the right one.  Once you download and install it (I'll install mine into C:\Tools, go ahead and copy the srvany.exe file into your system32 directory.  Now that we have that part done, let's move right along.

First, the nice thing about working with Windows is that you can create some automated services.  If something goes wrong, or a server needs to be restarted, the service can do it automatically.  This makes things easier.  In terms of what we'll be doing, we're going to create a development service just to test the functionality for later on.

Before launching a command prompt, right-click on the command prompt and run as an administrator.  This ensures we have elevated permissions when performing our command.  The command for creating a service will use the following protocol format:

sc <server> create [service name] [binPath= ] <option1> <option2>...

So, we'll use the following command:

sc create RubyThinServer binPath= "c:\windows\system32\srvany.exe" DisplayName= "Ruby Thin Server"

Also, note that there must be a space between the "=" sign and the value.  In the case for the statement above, the space comes after binPath= and also after DisplayName= .  There's no need in supplying the server name here.  Once you apply the command you should have a success message.

Now we need to edit the registry settings for our service and add our parameters.  Use regedit and go to the following key:

Windows Registry Editor Version 5.00
"AppParameters"="C:\\Ruby192\\bin\\thin start -p 3000 -e development"

The above snip can be copied and pasted into a .reg key and added automatically to your registry if you are using the exact same settings I am.  If you aren't, then you'll need to do the following:

Create a Parameters key for your RubyThinServer registry setting.  In the Parameters key, create strings for Application, AppDirectory, and AppParameters as specified above.

Once this has been completed, we can now start our service by going to services.msc and right-clicking our new service and choosing start. 

Once the service has been started, open a browser and navigate to http://localhost or http://ror-devapp01 (the name of my server) and the page should open as thin has been automatically started. 

It has..  Nice! 

You can change the parameters later on to make the service automatic and only allow it to work with production mode by putting in: 

"AppParameters"="C:\\Ruby192\\bin\\thin start -p 3000 -e production"

As you can see from this example, we're now going to make things easier for us by simply starting our service.  But, what if I don't want to run this manually, or I would rather be able to stop or restart the service via a quick executable?  Well, we could just use DOS for this and create a .bat (batch) file:

@echo Stopping the Ruby Thin Development Server
sc stop RubyThinServer
@echo Starting the Ruby Thin Development Server
sc start RubyThinServer

You would then store the batch file on the desktop and right-click and run it as an administrator so you have elevated permissions.  It will stop the server, and when you press any key, it will start the server.  If you changed the service to start automatically with retries in place, you could just perform a stop command and the service would automatically restart on its own.  You could also go into PowerShell and create a nifty service cycle, or even go one step further and write a system tray GUI in Ruby and make your service start up whatever way you'd like to do so using the Win32 library.  I'll leave this customization up to you.

One more thing before we move on.  It might be prudent to set a dependency in the service to ensure that the apache service is started and running.  Since we're depending on Apache first, it should be running before Thin starts.  Again, I'll leave this customization up to you.

Active Directory

Well it looks like we'll finally get to Active Directory today.  One thing I'd recommend for any of you is to try to closely mirror how AD looks on your company site so that you can make sure everything aligns properly when working with LDAP authentication.  We're not going to do a lot in AD, except create an administrative user and a regular user for testing.

If you are unfamiliar with AD, then I'm not sure why you are reading this article. :)  So, I'm going to assume that you have a basic understanding of how to create users and groups in Active Directory.  Go ahead and create two users.  For your administrative user, make sure he/she has full rights to all administrator groups (domain admin.. etc.).

In my example, my admin user will be Fred (flintstonef), and my normal user will be Barney (rubblem).  Barney will be a normal domain user.  If you like the flintstones then you'll like these two users. 

Once you have these two users created and assigned to the correct groups, we're ready to get into adding our ldap gem and creating a test for our ldap connection.  See you in Part Six.


  1. Nice writeup! But how do you handle logging of the thin-service?