Sunday, April 3, 2011

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

So, we've made a lot of headway through the last three parts.  But, we haven't really checked to ensure that Apache (our load balancer) is working.  In addition, we need to configure it to accept proxying to THIN so that our fast HTTP server is working together with it.  Let's do that now.

If you notice on our server, apache is already running as a default service.  All we need to do is check and see if it's running by opening a browser and pointing the URL to http://localhost/ .  Notice that we're not pointing anything to a specific port, nor are we running our rails application here. 


As you can see here, it says that "It works!".  So, apache is working fine.  Let's look at how we are going to configure it to work with Thin.

If you drive down into the following directory:

C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf

In this directory, you'll find httpd.conf which in windows looks like a text file.  If you open this, you'll find all of the apache configurations for your local server.  Before you dive into this, there are a few ways we could go about setting this up.  If your server is going to be using apache and has a lot of virtualization, you could include this file and make your changes specific to this vm in httpd-vhost.conf, or similar.  But, since we're trying to make this as simple as possible, we'll make our changes in here.

We need to first uncomment the following two lines:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

We'll be using these two modules to proxy requests coming into apache and force them to go to Thin.  In addition, we'll need to add some information to our virtualhost.  Here's the additional information we'll be adding to httpd.conf at the very bottom of the file:

<VirtualHost localhost:80>
      ServerName ror-devapp01
      DocumentRoot "C:/web/ldap/public"
      ProxyPass / http://localhost:3000/
      ProxyPassReverse / http://localhost:3000/
      ProxyPreserveHost On
</VirtualHost>

<VirtualHost ror-devapp01>
      ServerName ror-devapp01
      DocumentRoot "C:/web/ldap/public"
      ProxyPass / http://localhost:3000/
      ProxyPassReverse / http://localhost:3000/
      ProxyPreserveHost On
</VirtualHost>

So, let's look over the information above and determine why it's being set this way.  The ServerName is normally the live address of the server.  The documentroot is the location of the rails public directory in our rails application.  If you open a web browser and go to http://localhost or http://ror-devapp01 (the latter remember, is the name of my server), you'll see it is just pointing to "It works!".  What this says is that apache doesn't know how to handle incoming requests and send them over to Thin.  By adding the virtualhost for localhost, we are saying to pass this proxy over to localhost:3000 which happens to be the address and port of our Thin server.  In addition, we also have to add a virtual entry for our server name because otherwise the http://ror-devapp01 would also fail to pass over to Thin.  By adding both of these entries we accomplish this.

However, we still need to two more things after saving this modified httpd.conf file.  We need to start our Thin server by going into our root rails application and typing "rails s thin", and we also need to restart the apache server, which can be accomplished either by restarting it from the apache tool in your system tray, or going to services and restarting the apache service.  We need to restart apache services because we made changes to the httpd.conf file.

Let's open a web browser and test both URLs now:

http://localhost
http://ror-devapp01 (or the name of your current server)

Look at that, they pass right over to the Rails Thin server!

With this new information in place, apache can now handle incoming requests to localhost and to our server directly, passing them over to the Rails Thin server which acts as our fast HTTP server for this application.  Now that we have this part done, we can come back to apache later on for further configurations and modifications.  As a quick review, we have everything setup to start working with our base application.  Apache has been initially configured, Ruby and RoR is up and running, we have a good understanding of how all of the configuration is working together.  The only thing we haven't decided on is what type of application we will be creating.

Before we jump into some programming in our new environment, it is going to be appropriate to decide how we want to work with our application as far as an editor.  I could make this very simplistic and go with a simple text editor, but I like a fully functioning IDE as my preference, and Netbeans works wonderfully on Windows. Why not take advantage of that?

We can find the latest netbeans here:

http://www.oracle.com/technetwork/java/javase/downloads/jdk-netbeans-jsp-142931.html

I'm downloading Netbeans 6.9.1 with JDK 6.24.

Before we go and get it, we'll also need to make sure we install the latest JDK library as well.  The good news is that netbeans comes with a bundled install that houses JDK, and we can just grab the bundled version and begin our installation.

After installing it, and launching it, Netbeans takes you to a generic home page.  The first thing I want to do is set my netbeans up so it looks a little friendlier. 

If you go to Tools --> Plugins and go to the available plugins tab, you can type the word Ruby in the search box and you will find two plugins that are essential for ruby development.  These are Extra Color Themes and Ruby and Rails.



Place a checkmark in both boxes, click install, accept the license agreement, and install the plugins. Once finished, restart Netbeans.

Now that we have our plugins installed, let's perform some configurations for our programming environment.

Go to Tools --> Options, and select Fonts and Colors.  Change the profile to "Aloha" and click OK.  This will make our programming code look very similar to textmate. 

Now that we have our fonts and colors setup, let's configure our Ruby platform and open our default ldap project.

Go to File --> New Project, and select Ruby, and then select Ruby on Rails Application with Existing Sources.  Click Next.


On the next screen, change your project folder to C:\Web\ldap and name your project LDAP.  Click the drop down next to Ruby Platform and choose the Ruby 1.9.2-p180 platform (or your existing ruby platform).  Leave the server set to webrick.

Don't worry about the Server type.  We're not going to use Netbeans to start our server.  We're just going to use it for everything else.  Go ahead and click finish and once completed, you should see your rails LDAP project now.  Just for fun, go ahead and open up the Gemfile and everything should look similar to the screenshot below:


Terrific!  So, I would go ahead and browse around the interface and get familiar with the directory structure of our new application.  We've accomplished quite a bit today and we're ready to start working on some further configurations within rails, and adding some more important gems to our project.

Again, we haven't really started coding yet so you can see how extensive it can be sometimes with just trying to get your project started.  However, as you have a fully functioning environment setup, you won't have to take quite as much time to setup the next app.

See you again in part five.

1 comment:

  1. Great guide! Helped me a lot getting Rails 3.x up and running on Windows as I was stuck with mongrel_service and old Rails 2.x installations.

    However, static assets were not being served by apache by default. Rails would get the request and it would decline it, as specified in env/production.rb.

    It is important to add these directives to your VirtualHost block:

    ProxyPass /images !
    ProxyPass /stylesheets !
    ProxyPass /javascripts !

    Also, make sure that we have read access on the static assets:


    Order allow,deny
    allow from all


    Hope this helps!

    ReplyDelete