我---7号斋主人

已婚
爱老婆
I wanna be a happy fish...

星期二, 十月 10, 2006

fastcgi+apache的重要资料

摘自:
http://wiki.rubyonrails.com/rails/pages/Fast+CGI+and+Apache2+for+Windows+XP

Fast CGI and Apache2 for Windows XP
Home Page | All Pages | Recently Revised | Feed
Rails Prod Win

If you want to get Rails on XP using Apache 2.2 + FastCGI/SCGI support with the MySQL C binding download the Rails Prod Win setup kit and follow the instructions…you should be up and running in no time.
http://sourceforge.net/projects/rails-prod-win/
Other Method (longer…)

This has been a pretty big thorn in my side (and it seems others have gone through the same headaches and all the info out there is just plain wrong, wrong, wrong) and I’ve finally made it work for me, so I thought I’d share.
Things you’ll need

(This is my development machine, though I suspect this should work under different configurations)

* Windows XP SP2
* Apache 2.0.54
* MySQL
* Ruby one-click installer
* FastCGI 2.4.2
* and Ruby for Apache
* Rails 0.13.1

Initial intallation

* Install the basic stuff: Apache, \MySQL, Ruby, gem install rails. Easy.

* Rename mod_fastcgi-2.4.2-AP20.dll to mod_fastcgi.dll (just to make life easy), and copy it to the Apache modules folder (default is c:\programs files\Apache Group\Apache2\modules).

* Install Ruby for Apache. Important: only install mod_fastcgi. Do NOT install mod_ruby or mysql.so because these directions will not work with them.
Note also that the install may complain that it cannot open c:\xppro\system32\msvcp71.dll for writing. Just rename it and retry.

* Create your Rails app. In the console do something like


rails yourappname

Configure Apache

Now you’ll need to configure Apache. Don’t worry, this is easy-easy. Go to your start menu, find the Apache programs group. In there you’ll see a sub-group called something like “Configure Apache Server”. Click on “Edit the Apache httpd.conf Configuration File”

Now you’re looking at a text file with all kinds of confusing stuff, right? Look for a line that looks like


#LoadModule rewrite_module modules/mod_rewrite.so

and change it to


LoadModule rewrite_module modules/mod_rewrite.so


(i.e., uncomment the line)

Then add a new line like this


LoadModule fastcgi_module modules/mod_fastcgi.dll

If you use Apache 1.3.x you also need to make corresponding changes in the AddModule section below the LoadModule section.

Find the line


#AddModule mod_rewrite.c

and uncomment that:


AddModule mod_rewrite.c

Then add a new line:


AddModule mod_fastcgi.c

Now you need to set up a root directory for your application. Look for a line that starts with DocumentRoot and change the path to your Rails application folder.

Next look for a line that starts with Directory and change that path to your Rails application folder (same as above)

(Remember for both paths above to use forward slashes not the normal Windows backslashes)

Almost done with Apache configuration…

(see Fast CGI and Apache2 for Windows without VirtualHosts for an alternative to using VirtualHost directives)

at the bottom of the file add this:



ServerName rails
DocumentRoot "c:/path/to/your/rails/app/public"

Options ExecCGI FollowSymLinks
AllowOverride all
Allow from all
Order allow,deny
AddHandler cgi-script .cgi
AddHandler fastcgi-script .fcgi



(set paths above accordingly)
Configure your Rails app

Go to the public folder in your Rails application and open .htaccess

Look for the rewrite rule:


RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

and change it to:


RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

Comment out the Apache section in your .htaccess as you’ve already referenced these in your Apache httpd.conf file earlier.

Open dispatch.fcgi and change the first line to


#!c:/ruby/bin/ruby

to accomodate Windows
Add a new host

Last little bit…

Open up your host file (c:\windows\system32\drivers\etc\host)

and add this line:


127.0.0.1 YourRailsAppName

Cool, you’re done

Note from porter.ea:
One additional edit that I found necessary was to add ”#!c:/pathto/rubybin/rubyw” (note the “w”) at the top of the “C:\appname\public\dispatch.fcgi” file.

This was purely a guess on my part based on perusal of the /ruby/bin directory, but it caused my Apache to go from hanging for a long time, then displaying: “Application error Rails application failed to start properly”, to displaying the correct output in better time.
:End Note

Now start Apache, open a browser and type in ”\YourRailsAppName”

you should load the default Welcome to Rails page with blazing \FastCGI speeds.

Like I said easy-easy.
Another Note:

if the Above instructions still do not work for you, please try reversing the slashes:


#!c:\ruby\bin\ruby

It has worked for me.
Additional Notes

1) You might need to add the following line to httpd.conf (particularly on a dev machine)

IfModule mod_fastcgi.c>
FastCgiServer {rails_app_dir}/public/dispatch.fcgi\
-initial-env RAILS_ENV=development \
-processes 3 -idle-timeout 120
/IfModule


This prevents premature timeouts and limits the FASTCgi processes to 3.

2) If using mysql and get ‘uninitialize constant mysql’ error, you may have to copy libmysql.dll to ruby bin (e.g. c:\ruby\bin).

3) Take special care to run your rails-application in “production”-mode. See environment.rb (msc)
Another Note about apache port

If you are on windows, you may probably use the virtual host with a non-80 port (since port 80 used by IIS defaultly). Thus don’t forget add “Listen ${port_number}” in the httpd.conf, otherwise you will never see the rails welcome page.
I forgot this and it wasted a lot of my time to find the problem.
Questions

Does anyone know how I could setup multiple rails apps on the same machine using virtual hosts?

没有评论: