ubuntu10.10 下部署 apache2 + fastcgi + rack/sinatra/rails3
nginx + passenger用了蛮久的,突然对fastcgi感兴趣,因为有robbin这篇blog在Linux平台上安装和配置Ruby on Rails详解
http://robbin.iteye.com/blog/43228
以及rails部署性能比较的文章。
但是rails3在官网上已经没有关于fastcgi的配置建议了,同时rails3也没有dispatch.fcgi这样类似的东东了。
注定我按照robbin的blog是不会安装成功的了。
但是rack告诉我可以使用fastcgi,
那么sinatra,rails3使用rack肯定也可以使用fastcgi,本着由浅入深的原则开始了我的探索之旅。
而我查到的几篇文章都跟apache相关,那就先从apache开始,然后再尝试lighttpd
安装apache2
sudo aptitude install apache2
tail -f /var/log/apache2/error.log
apachectl configtest
sudo aptitude install libapache2-mod-fastcgi libfcgi-dev
gem install fcgi rack
<VirtualHost *:80>ServerAdmin webmaster@localhostDocumentRoot /var/www<Directory />Options FollowSymLinksAllowOverride None</Directory><Directory /var/www/>Options Indexes FollowSymLinks MultiViewsAllowOverride NoneOrder allow,denyallow from all</Directory>ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/<Directory "/usr/lib/cgi-bin">AllowOverride NoneOptions +ExecCGI -MultiViews +SymLinksIfOwnerMatchOrder allow,denyAllow from all</Directory>ErrorLog ${APACHE_LOG_DIR}/error.log# Possible values include: debug, info, notice, warn, error, crit,# alert, emerg.LogLevel warnCustomLog ${APACHE_LOG_DIR}/access.log combined Alias /doc/ "/usr/share/doc/" <Directory "/usr/share/doc/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.0/255.0.0.0 ::1/128 </Directory></VirtualHost>
#!/home/zc/.rvm/rubies/ree-1.8.7-2011.03/bin/ruby#!/usr/bin/env rubyrequire 'rubygems'require 'rack'app = Proc.new do |env| Rack::Response.new.finish do |res| res.write "Hello, Rack!" endendRack::Handler::FastCGI.run app
ps aux|grep apache2
root 22351 0.0 0.1 73780 2856 ? Ss 20:10 0:00 /usr/sbin/apache2 -k startwww-data 22352 0.0 0.0 73512 1972 ? S 20:10 0:00 /usr/sbin/apache2 -k startwww-data 22354 0.0 0.1 297392 3124 ? Sl 20:10 0:00 /usr/sbin/apache2 -k startwww-data 22355 0.0 0.1 297208 2412 ? Sl 20:10 0:00 /usr/sbin/apache2 -k start
ps aux|grep cgi
www-data 22353 0.0 0.1 73512 2108 ? S 20:10 0:00 /usr/sbin/fcgi-pm -k startwww-data 22418 0.4 0.6 57272 13724 ? S 20:10 0:00 /home/zc/.rvm/rubies/ree-1.8.7-2011.03/bin/ruby /usr/lib/cgi-bin/hello-world.fcgi
gem install sinatra
<VirtualHost *:80> DocumentRoot /home/zc/Sites/sinatra-fcgi <Directory /home/zc/Sites/sinatra-fcgi> Options ExecCGI FollowSymLinks AllowOverride all Order allow,deny Allow from all </Directory></VirtualHost>
cd /etc/apache2/sites-enabledsudo ln -s ../sites-available/sinatra 001-sinatra
RewriteEngine OnRewriteBase / DirectoryIndex dispatch.fcgi RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ dispatch.fcgi/$1 [L]
cd /etc/apache2/mods-enabledsudo ln -s ../mods-available/rewrite.load rewrite.load
#!/home/zc/.rvm/rubies/ree-1.8.7-2011.03/bin/ruby#!/usr/bin/env rubyrequire 'rubygems'#require 'rack'require 'sinatra'require 'start'Rack::Handler::FastCGI.run Sinatra::Application
require 'rubygems'require 'sinatra'get '/' do '你好世界Hello, World!'end
<VirtualHost *:80> DefaultInitEnv RAILS_ENV production DocumentRoot /home/zc/Sites/rails3_fcgi/public <Directory /home/zc/Sites/rails3_fcgi/public> Options ExecCGI FollowSymLinks AllowOverride all Order allow,deny Allow from all </Directory></VirtualHost>
sudo ln -s /etc/apache2/sites-available/rails3_fcgi /etc/apache2/sites-enabled/001-rails3_fcgi
sudo aptitude install libapache2-mod-fcgid
<IfModule mod_fcgid.c> AddHandlerfcgid-script .fcgi FcgidConnectTimeout 20</IfModule>
sudo ln -s /etc/apache2/mods-available/fcgid.conf /etc/apache2/mods-enabled/fcgid.confsudo ln -s /etc/apache2/mods-available/fcgid.load /etc/apache2/mods-enabled/fcgid.load
#!/home/zc/.rvm/rubies/ree-1.8.7-2011.03/bin/ruby#!/usr/bin/ruby#require '../config/environment'require ::File.expand_path('../../config/environment', __FILE__)class Rack::PathInfoRewriter def initialize(app) @app = app end def call(env) env.delete('SCRIPT_NAME') parts = env['REQUEST_URI'].split('?') env['PATH_INFO'] = parts[0] env['QUERY_STRING'] = parts[1].to_s @app.call(env) endendRack::Handler::FastCGI.run Rack::PathInfoRewriter.new(Rails3Fcgi::Application)
SetEnv RAILS_RELATIVE_URL_ROOT /rails3_fcgiRewriteEngine OnRewriteRule ^(stylesheets/.*)$ - [L]RewriteRule ^(javascripts/.*)$ - [L]RewriteRule ^(images/.*)$ - [L]RewriteRule ^$ index.html [QSA]RewriteRule ^([^.]+)$ $1.html [QSA]RewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^(.*)$ rails3_fcgi.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
chmod 666 log/production.log