配置apache以fastcgi运行php

apache默认是用自带的mod_php模块运行php,现在我们介绍使用fastcgi来执行php脚本。先说下fastcgi的优点:
Fastcgi的优点:

  • 从稳定性上看, fastcgi是以独立的进程池运行来cgi,单独一个进程死掉,系统可以很轻易的丢弃,然后重新分 配新的进程来运行逻辑.
  • · 从安全性上看,Fastcgi支持分布式运算. fastcgi和宿主的server完全独立, fastcgi怎么down也不会把server搞垮.
  • · 从性能上看, fastcgi把动态逻辑的处理从server中分离出来, 大负荷的IO处理还是留给宿主server, 这样宿主server可以一心一意作IO,对于一个普通的动态网页来说, 逻辑处理可能只有一小部分, 大量的图片等静态
  • IO处理完全不需要逻辑程序的参与.
  • · 从扩展性上讲, fastcgi是一个中立的技术标准, 完全可以支持任何语言写的处理程序 (php,java,perl,ruby,c++,python…)
  • · 适用操作系统,可在任何平台上http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz使用
  • 安装apache

    1. wget http://apache.ziply.com//httpd/httpd-2.2.21.tar.gz
    2. tar xzf httpd-2.2.21.tar.gz
    3. cd httpd-2.2.21
    4. ./configure --prefix=/usr/local/apache
    5. make && make install

    安装fastcgi

    1. wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz
    2. tar xzf mod_fastcgi-2.4.6.tar.gz
    3. cd mod_fastcgi-2.4.6
    4. cp Makefile.AP2 Makefile
    5. make top_dir=/usr/local/apache
    6. make top_dir=/usr/local/apache install

    完成之后编辑httpd.conf配置文件,加入fastcgi模块装载代码:

    1. LoadModule fastcgi_module modules/mod_fastcgi.so

    安装php5.2

    1. wget http://us2.php.net/get/php-5.2.17.tar.gz/from/am.php.net/mirror
    2. tar xzf php-5.2.17.tar.gz
    3. cd php-5.2.17
    4. ./configure --prefix=/usr/local/php --enable-fastcgi --disable-cli
    5. make && make install

    配置apache支持php

    编辑httpd.conf文件,加入如下代码:

    1. ### fastcgi ###
    2. ScriptAlias /fcgi-bin/ "/usr/local/php/bin/"
    3. AddHandler php-fastcgi .php
    4. Action php-fastcgi /fcgi-bin/php-cgi
    5. AddType application/x-httpd-php .php
    6.  
    7. <IfModule mod_fcgid.c>
    8.     AddHandler fcgid-script. .php .fcgi   ### 暂时只配置支持.php
    9.     IdleTimeout 300
    10.     ProcessLifeTime 1800
    11.     MaxProcessCount 100
    12.     DefaultMinClassProcessCount 3
    13.     DefaultMaxClassProcessCount 8
    14.     IPCConnectTimeout 15
    15.     IPCCommTimeout 300
    16.     MaxRequestsPerProcess 100
    17. </IfModule>
    18. ### fastcgi ###

    建立虚拟主机可以这样配置:

    1. <VirtualHost *:80>
    2.  DocumentRoot /usr/local/apache/htdocs
    3.  ServerName localhost
    4.  Options +ExecCGI
    5.  AddHandler fastcgi-script .fcgi
    6.  AddType application/x-httpd-php .php
    7.  Action application/x-httpd-php /fcgi-bin/php-cgi
    8.  <Directory /usr/local/apache/htdocs>
    9.  Options Indexes ExecCGI
    10.  Order allow,deny
    11.  allow from all
    12.  </Directory>
    13. </VirtualHost>

    详细的fastcgi指令配置请看:http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html

    标签:PHPApache 发布于:2019-11-24 19:09:08