江边闲话集

09/26/2007

在Debian上部署Rails Apache+fcgi(或FastCgi)

Filed under: 一技之长 — Tags: , — 张太国 @ 15:26

Rails+Apache2+fcgid安装

安装Ruby

tar xzvf ruby-1.8.6.tar.gz
cd ruby-1.8.6
./configure
make
make install

    
检查一下ruby是否安装,输入:

ruby -v

安装zlib

tar xvzf  zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure
make
make install

安装rubygem  

tar xzvf rubygems-0.9.4.tgz
cd rubygems-0.9.4
ruby setup.rb

为了看是否安装上,输入:

gem -v

Rails

gem install rails -y

 有的时候可能因为网速问题装不上,请耐心多试记下.

测试

rails testRails
cd testRails
ruby script/server

 

 

 

 

会有如下显示:

=> Booting WEBrick…
=> Rails application started on http://0.0.0.0:3000=> Ctrl-C to shutdown server; call with

   –help for options
[2007-08-15 08:02:04] INFO  WEBrick 1.3.1
[2007-08-15 08:02:04] INFO  ruby 1.8.6 (2007-03-13) [i686-linux]
[2007-08-15 08:02:04] INFO  WEBrick::HTTPServer#start: pid=16306 port=3000

 

ImageMagick

tar xzvf ImageMagick-6.3.5-6.tar.gz
cd ImageMagick-6.3.5
./configure
make
make install

看是否安装成功,输入:

usr/local/bin/convert logo: logo.gif

 

 

安装RMagick

tar xvzf RMagick-1.15.10.tar.gz
cd RMagick-1.15.10
./configure
make
make install

安装fcgi

由于rubyfcgi支持库需要在编译的时候联接FCGI的系统库,因此我们需要先安装FCGI库,下载FCGI源代码发行包:

http://www.fastcgi.com/dist/

tar xzvf fcgi-2.4.0.tar.gz

cd fcgi-2.4.0

./configure –prefix=/usr/local/fcgi

make && make install

 

 

 

 

 

安装fcgid

apt-get install libapache2-mod-fcgid
/etc/init.d/apache2 force-reload

 安装fcgi for ruby

gem install fcgi

配置apache

可以如下

   NameVirtualHost *
<IfModule mod_fcgid.c>
       AddHandler fcgid-script .fcgi
       SocketPath /var/lib/apache2/fcgid/sock
       DefaultInitEnv  RAILS_ENV production
       IdleTimeout 600
       ProcessLifeTime 3600
       MaxProcessCount 8
       DefaultMinClassProcessCount 3
       DefaultMaxClassProcessCount 3
       IPCConnectTimeout 8
       IPCCommTimeout 48
   </IfModule>

<VirtualHost *>
    ServerAdmin webmaster@localhost
    #ServerName localhost
    DocumentRoot /path/to/app/public/
    ErrorLog /xxx/log/httpd-error_log
    CustomLog /xxx/log/httpd-access_log common
    
   <Directory
/path/to/app/public/>
      Options ExecCGI FollowSymLinks
      AllowOverride all
      Order allow,deny
      Allow from all
   </Directory>
</VirtualHost>

配置应用程序

编辑.htaccess文件

注释 AddHandler cgi-script .cgi以及AddHandler fastcgi-script .fcgi,增加AddHandler fcgid-script .fcgi.

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

可以如下:

# General Apache options

AddHandler fcgid-script .fcgi

Options +FollowSymLinks +ExecCGI

RewriteEngine On

RewriteRule ^$ index.html [QSA]

RewriteRule ^([^.]+)$ $1.html [QSA]

RewriteCond %{REQUEST_FILENAME} !-f

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

ErrorDocument 500 “<h2>Application error</h2>Rails application failed to start properly”

编辑dispatch.fcgi

加入

require ‘fcgi_handler’

require ‘rubygems’

require_gem ‘fcgi’

需要注意的是,我们一般都是在windows平台开发,然后部署到linux上去,如果您属于这种情况,请注意文件的第一行编译器

的设置.windows上可能是:

#! C:\ruby\bin\ruby

需要改成

#! /usr/local/bin/ruby

问题解答

Q:如果C编译器没有安装,应该如何安装呢?  
A:debian上使用如下命令:

apt-get install build-essential

 
Q:如果出现以下错误:

<— lib
/usr/local/lib/ruby/site_ruby/1.8/rubygems/remote_fetcher.rb:4:in `require’: no such file to load — zlib (LoadError)
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems/remote_fetcher.rb:4
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:8:in `require’
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:8
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:504:in `require’
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:504
        from /home/confach/pkgs/rubygems-0.9.4/./post-install.rb:81:in `require’
        from /home/confach/pkgs/rubygems-0.9.4/./post-install.rb:81:in `install_sources’
        from /home/confach/pkgs/rubygems-0.9.4/./post-install.rb:116:in `run_hook’
        from setup.rb:1526:in `exec_task_traverse’
        from setup.rb:1342:in `exec_install’
        from setup.rb:1000:in `exec_install’
        from setup.rb:814:in `invoke’
        from setup.rb:773:in `invoke’
        from setup.rb:1578

 我该怎么办?

A:说明zlib无法加载.解决方案:

cd ruby-1.8.6/ext/zlib/
ruby extconf.rb --with-zlib-include=/usr/include --with-zlib-lib=/usr/lib
make
make install

Q:如果利用查看fcgi for ruby是否已经装上?
A:请调用gem list,看是否fcgi在其中

Q:如果我利用gem install fcgi 安装不上怎么办?
A:很简单,下载其源代码然后进行安装:
   

然后就可以安装rubyfcgi支持库了,下载ruby-fcgi-0.8.7.tar.gz

 

http://rubyforge.org/projects/fcgi/

tar xzvf ruby-fcgi-0.8.7.tar.gz

cd ruby-fcgi-0.8.7

ruby install.rb config — –with-fcgi-include=/usr/local/include –with-fcgi-lib=/usr/local/lib

ruby install.rb setup

ruby install.rb install


 
需要注意的是,还是安装上fastcgi.

Q:如我我遇到如下错误怎么办?

mod_fcgid: can’t apply process slot for “xxx/xxx/public/dispatch.fcgi”

A:这个比较简单,修改一下Apache的日志目录权限,例如chmod 755 /var/log/apache2

Q:如果遇见如下错误怎么办?

Premature end of script headers: dispatch.fcgi

A:请先检查一下该文件的配置以及.htaccess.如果问题依旧,请修改一下dispatch.fcgi的权限,至少是755更多详情,

参考我的另一文章:

“Premature end of script headers: dispatch.fcgi”终极解决方案

.

Q:如果提示RewriteEngine拼写错误,或者没有启用该功能怎么处理?
A:非常简单,那是因为apacherewrite模块没有加载.请先检查rewrite模块是否已经安装.然后执行下面的命令:

cd /etc/apache3/mods-available
     nano -e rewrite.load

输入

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

Ctrl+X保存

cd ..
ln -s mods-available/rewrite.load mods-enabled

然后重启apache

/etc/init.d/apache2 restart

Q: 如何选择fastcgifcgid?
A:很简单,如果是apache1.x,选用fastcgi,如果是apache2.x,请选用fcgid

Q:对系统有什么要求?
A:建议采用2.6版本的内核

  
Q:怎么使用ImageMagick无法显示图片?

A:请先调用

/usr/local/bin/convert logo: logo.gif


 

 

如果没法生成或者生成的gif文件无效,请重新安装ImageMagick.在安装之前请安装jpeg,freetype,png,tiff等库.

  • apt-get install libjpeg62-dev或apt-get install libjpeg62  –>jpeg

  • apt-get install  libfreetype6  –> freetype

对于最新的版本,可能要安装ghostscript。

apt-get install  gs

Q:如果访问网站显示的是dispatch.fcgi的源内容,怎么办?

A:一般来说,有2个理由导致:

  1.    没有安装fcgi module。因为没有安装,访问时没法解析。关于安装的问题,参见上面安装fcgi这一节。
  2. 或者是apache不同版本造成的。

5 Comments

  1. […] 另外,可以参考另一文章:在Debian上部署Rails Apache+fcgi(或FastCgi) […]

    Pingback by 第36个标记 » "Premature end of script headers: dispatch.fcgi"终极解决方案 — 02/29/2008 @ 11:51

  2. […] 在Debian上部署Rails Apache+fcgi(或FastCgi) […]

    Pingback by 第36个标记 » RMagick 2和ImageMagick不能正确显示图片的解决方案 — 06/26/2008 @ 15:18

  3. […] 在Debian上部署Rails Apache+fcgi(或FastCgi) […]

    Pingback by 第36个标记 » 谈解决方案 — 09/10/2008 @ 23:00

  4. Привет!

    бинарные

    http://voyage39.ru/indikator-opcionnyh-urovney.php

    бинарные опционы литература для начинающих
    тема бинарных опционов
    журнал бинарные опционы

    http://vintage-cafe68.ru/strategiya-torgovli-binarnymi-opcionami-tochnyy-vhod.php
    http://hotel-irbis.ru/kak-nauchitsya-rabotat-na-binarnyh-opcionah.php

    Comment by Antonysop — 10/08/2015 @ 02:46

  5. Dukan est devenu au fil des annees une marque. http://lenitsky.com/pobeda-v-programme-raskrutka-na-telekanale-russian-music-box/ C est une tres bonne chose.

    Comment by PipaKr — 10/11/2015 @ 10:44

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress