May 2, 2005

Allowing Apache and IIS to use the same port on a windows machine

If you are setting up Ruby on Rails on windows it is nice to be able to run Rails on Apache rather than all the rigmarole of configuring IIS to do the job.

Multiple IP addresses


First things first. You cannot run Apache and IIS on the same port (80 is standard) if you only have access to one IP address. The first thing is to add another IP address to your machine. If you are developing at home then choose any number in your local range that is not being used by another machine, switch, router etc. If you are setting this up on a production server that will be seen by the outside world then hopefully you have a second IP address to use, otherwise you will have to move either IIS or Apache to a different port. This is for another blog entry....

To add another IP address to your machine open up start > settings > control panel > Network and dial-up connections > Local Area connection, then select properties. Find the 'Internet protocol (TCP/IP)' protocol and double-click to view the properties. Click on the 'Advanced...' button. Under the 'IP addresses' section click the 'Add...' button. Type in your chosen (or given) IP address and subnet mask (usually 255.255.255.0 - your machine should fill it in anyway..) and hit OK. That's that bit done.

Making the web servers play nice


This first thing you will have to do is to stop IIS and apache from trying to hog all the IP address' for themselves. I believe this is called socket pooling.

Apache


In Apache this is simple. Just edit the httpd.conf file. You need to locate the line that says something like:

Listen *:80

and change it so that it uses only your chosen (or given) IP address - 192.168.0.50 for instance:

Listen 192.168.0.50:80

Dont forget to include the :80 for the port.

[update 04-04-06] : You will also have to add

NameVirtualHost 192.168.0.50:80 to the configuration file or else the server only uses the first virtual host in the list.

When you start Apache again, you are done!

IIS

IIS is a different matter. If you are using IIS5 you can follow these steps. Open 'start > control panel > administrative tools > services'. Locate 'IIS Admin service' and the 'World Wide Web Publishing Service' and stop them both. Open a command prompt (start > run.. then type cmd and press return) and change to the '..\inetpub\adminscripts' directory. Once you are in the adminscripts folder type:

cscript adsutil.vbs set w3svc/disablesocketpooling true

You should get a reply back:

disablesocketpooling : (BOOLEAN) True

Setting an IP address with IIS

Possibly what you will have to do now is to set the IP address you want your IIS based websites to answer to. I havent tested to see if this is necessary though it is relatively simple Open the 'Internet Information Services'. Select a website and right click to view properties. Simply enter the IP address in the 'IP Address' box in the 'Web Site Identification' group, then press the OK button. Start the 'IIS Admin service' and 'World wide web publishing service'. Navigate to the website and you should be served from IIS rather than Apache.

UPDATE: Setting IP address for IIS 6.0

With IIS 6.0 (the .0 bit is important if you are doing searches for info on the Microsoft site, I found...) there is a slightly different method to make IIS use only one IP address. You have to use HTTPCFG.exe which is part of a suite of tool sin the 'Windows Support Tools' on the Server 2003 install disk in the SUPTOOLS directory.

If these are installed you can skip this paragraph. To install them, go to the SUPTOOLS directory and double click on the SUPTOOLS.MSI file. The tools will be automatically installed - you shouldnt need a reboot.

Go to 'start > programs > windows support tools > command prompt' which will open a terminal window in the support tools directory. Here you must type the command:

net stop http /y

which will shut down the IIS services. Then run this command:

httpcfg set iplisten -i aaa.bbb.ccc.ddd

You should receive a message with a return code of zero. If you get any other number then something is wrong - do some checking in the event viewer, it may take a while :) If everything is OK then check that this has taken by running:

httpcfg query iplisten

Which should return the IP address you supplied And lastly you will need to restart the IIS services:

net start w3svc

And you should be good to go. Startup the Apache server and there should be no errors. You hope.

Posted by dottie at 12:51 AM | Comments (0)