Apache2配置FastCGI(mod-fcgid)

Debian下爲Apache2配置FastCGI(mod-fcgid),PHP5,Perl,Python,ROR
本文主要介紹在Debian/Etch版本下將Apache對PHP/Perl/Python/Ruby On Rails的支持,由單獨的apache模塊統一改爲FastCGI支持,對全新安裝Apache也可作參考

注意:如果升級到Apache2.2,注意apache2.2與2.0的一些區別,尤其是授權認證部分,分成了幾個module,所以可能需要自己enable,如果您直接裝apache2.2,無須擔心.

mod_imap 被更名爲 mod_imagemap
mod_auth 被拆分爲 mod_auth_basic、mod_authn_file、mod_authz_user、mod_authz_groupfile
mod_access 被更名爲 mod_authz_host
mod_auth_ldap 被更名爲 mod_authnz_ldap

FastCGI的好處
1)可以支持在一個系統上支持同一種腳本不同版本的解釋器,如PHP4, PHP5
2)只要安裝一個apache 的module後,就可同時支持PHP, Python, Perl等,沒有必要爲它們安裝各自的module
3)獲得更好的權限控制,比PHP運行在安全模式更安全,國外大的虛擬主機供應商如DreamHost,BlueHost, Godaddy等都是採用
mod-fcgid或者mod_fastcgi來實現對PHP的支持

0.這裏用的mod-fcgid,符合GPL條款的,而不是那個mod_fastcgi(它在debian的non-free) 並且mod-fcgid與apache2配合得更好

1.如果你用fastCGI 來統一支持這些腳本的話,先將
libapache2-mod-php5,
libapache2-mod-perl2,
libapache2-mod-python2.4
等模塊去掉,當然你也可以按照你的需要保留部分模塊

apt-get remove libapache2-mod-php5 libapache2-mod-perl2 libapache2-mod-python2.4 2.安裝mod-fcgid
apt-get install libapach2-mod-fcgid
3.檢查PHP是否支持FastCGI
一般而言,如果你以前安裝了mod_php,那麼php5-cgi應該已經裝上了

ls /usr/bin/php5-cgi
或者

/usr/bin/phpt-cgi -v
相信會有
(cgi-fcgi)
的字樣

如果沒有裝上,則先裝上php5-cgi

apt-get install php5-cgi4.檢查fcgid的配置
/etc/apache2/mods-enabled/fcgid.conf (這是個到mods-availabe目錄的符號連接)
應該有一個標準配置
<IfModule mod_fcgid.c>
  AddHandler fcgid-script .fcgi
  SocketPath /var/lib/apache2/fcgid/sock
  IPCConnectTimeout 20
</IfModule>

宜將/etc/apache2/mods-enabled/fcgid.conf改爲

<IfModule mod_fcgid.c>  AddHandler fcgid-script .php .py .pl .fcgi  SocketPath /var/lib/apache2/fcgid/sock  IPCConnectTimeout 20</IfModule>  
以增加對php py pl等擴展名的支持

當然還可以添加其他配置指令(當然這些不是必要的,默認的配置基本上就可以了)來修改默認的一些配置,如
IdleTimeout 600
ProcessLifeTime 3600
MaxProcessCount 8
DefaultMinClassProcessCount 3
DefaultMaxClassProcessCount 3
IPCConnectTimeout 8
IPCCommTimeout 48
 具體的配置指令在後面列出
5.檢查模塊的載入
/etc/apache2/mods-enabled/fcgid.load
應該有
LoadModule fcgid_module /usr/lib/apache2/modules/mod_fcgid.so

6.修改Apache的主機配置,增加對PHP的支持
/etc/apache2/sites-enabled/000-default中添加一句
FCGIWrapper /usr/bin/php5-cgi .php
以增加對php的支持
變成類似於

<Directory /var/www/>        FCGIWrapper /usr/bin/php5-cgi .php        Options ExecCGI SymLinksIfOwnerMatch</Directory>
當然,如果你沒有 移除 mod_php的話,就是說你還想用默認的(標準的)的PHP支持,FCGIWrapper就沒有必要加上了。但如果你使用FCGI的話,注意Option ExeCGI必須添加上,否則給出403錯誤,報告沒有權限訪問 .php等文件

你還可同時增加 對PHP4 PHP5的支持(你必須Php4,php5都裝上)
FCGIWrapper /usr/bin/php4-cgi .php4 .php
FCGIWrapper /usr/bin/php5-cgi .php5
7.增加對Python的支持 在000-default裏或者其他虛擬主機配置文件的段里加上
ScriptAlias /python /var/www/python/index.py

這樣,訪問  你的主機 http://localhost/python就會 將請求轉給 /var/www/python/index.py

注意,你的 index.py必要引用 python的fastcgi接口,建議你使用這個(Trac系統也是使用這個fastcgi接口)fcgi.py
這裏給出我的index.py作爲參考
#!/usr/bin/pythontry:    from fcgi import WSGIServer    def myapp(environ, start_response):        start_response('200 OK', [('Content-Type', 'text/plain')])        return ['Hello World!\n']    WSGIServer(myapp).run()except Exception, e:    print 'Content-Type: text/plain\r\n\r\n',    print 'Oops...'    print    print 'Trac detected an internal error:'    print    print e    print    (注意,必須將fcgi.py放到index.py同一目錄,否則import找不到模塊)

如果上面的index.py能正常 打印 Hello World!就說明基本上配置好了

注意,
1)fcgi.py必須放到 index.py可以找到的地方,比如index.py同一目錄下 或者 site-package 目錄
2)index.py必須有可執行的屬性,否則 會 拋出 內部服務器錯誤(Internal Server Error)

如果你需要配置某個目錄下.py都可執行,可用這段配置文件
        ScriptAlias /python/ /home/gavin/py/
        <Directory "/home/gavin/py">
                DirectoryIndex index.html index.py index.pl index.php
                AllowOverride None
                Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

這樣,/python/下的任何一個.py文件都會執行,比如,將下面這段代碼保存爲hello.py
#!/usr/bin/python
from fcgi import WSGIServer

def myapp(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return ['upsdn.net:Hello World!\n']

WSGIServer(myapp).run()
放到 /home/gavin/py/目錄
訪問/python/hello.py就可看到hello world
8.配置Ruby On Rails

主要就是利用rewrite將請求分發給rails腳本
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L] 其他與標準FastCGI配置類似
下面這個是Typo的一個示範配置

<VirtualHost *:80>ServerName foo.example.com<Location /journal>    RewriteEngine On    # Let apache handle purely static files like images by itself.    RewriteCond %{REQUEST_FILENAME} !-f    # Send Everything else to Typo    RewriteRule ^(.*)$ dispatch.fcgi [QSA,L] </Location><Directory /sites/foo.example.com/public_html/journal>    # ExecCGI is required for mod_fcgid to work.    Options Indexes FollowSymLinks ExecCGI    # Disable .htaccess files.    AllowOverride None    Order allow,deny    Allow from all    # This tells mod_fcgid to run the dispatch.fcgi script as a FastCGI    # AddHandler fcgid-script .fcgi</Directory>當然你也可以安裝通用的fastcgi ruby庫
ap-get install libfcgi-ruby
或者你需要設置

DefaultInitEnv RAILS_ENV production更詳細的東西請參考ROR相關文章
9.配置perl
  首先安裝perl的FastCGI模塊,Debian當然用apt-get來安裝了

apt-get install libfcgi-perl(將安裝FCGI.pm,    CGI::Fast 依賴於這個模塊)

你也可利用CPAN來安裝

# perl -MCPAN -e shell 直接回車,都用默認配置就行了
cpan  install FCGIcpan  install CGI::Fastcpan  quit 
安裝後驗證一下這個hello.pl腳本(用FCGI模塊, 只需要FCGI.pm即可)
#!/usr/bin/perluse FCGI;my $request = FCGI::Request();while($request->Accept() >= 0){  print "Content-type: text/html\n\n";  print "<H1><b>Hello World!</b></H1>";}exit 0;或者試一下這個counter.cgi,   用CGI:Fast模塊(需要FCGI.pm和CGI.pm)
#!/usr/bin/perl -wuse strict;use CGI::Fast qw(:standard);my $counter = 0;my $title = "Fast CGI counter";while (new CGI::Fast) {  print header;  print start_html $title;  print h1 $title;  print "Invocation number: ", b($counter++), ", PID: ", b($$), ".", hr;  print end_html;}  10.好了,現在你的系統應該配置好了mod_fcgid了,並且支持Perl,PHP,Python,Ruby on Rails
提示:發現500 內部服務器錯誤時,先檢查腳本權限,然後在CLI環境運行是否正常,最後再在CGI環境下驗證

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章