Feb 27 2009

apache + fastcgi + php 安装要点

Category: 技术ssmax @ 14:32:46

这两天在新机器上面装了apache和php,原来用的是so module的模式运行php,因为php建议使用prefock mpm,这样子每个apache 的进程都会加载php module,造成占用内存过大,如果站点静态内容多的话,个人觉得太耗资源了,apache单线程一般是4-5m,加载了php之后是20M以上,汗啊。。。这样子1G内存也开不了多少进程。。。

最后决定用fastcgi方式执行php了,按需才生成,而且是用fastcgi的动态模式,方便进程管理。

具体步骤如下:

1、编译apache,一般我用的参数是:

./configure –prefix=/home/apache2 –enable-modules=most –enable-mods-shared=most –enable-ssl –enable-so

make

make install

2、编译php,如果要用fastcgi,就不能加–with-apxs2

‘./configure’ ‘–prefix=/home/php’ ‘–enable-fastcgi’ ‘–with-mysql’ ‘–with-openssl’ ‘–with-zlib’ ‘–with-gd’ ‘–with-curl’ ‘–with-bz2’ ‘–with-jpeg-dir’ ‘–with-png-dir’ ‘–with-freetype-dir’ ‘–enable-gd-native-ttf’ ‘–enable-force-cgi-redirect’

make

make install

cp php.ini-recommended /home/php/lib/php.ini

3、编译mod_fastcgi,最新的也是2007年的版本了。。。还好apache 2.2 一样能用。

cp Makefile.AP2 Makefile

vi Makefile 把涉及到apache的目录全部改成/home/apache2,就是上面的apache安装目录

make

make install

4、配置apache的httpd.conf

在Server全局配置:

LoadModule fastcgi_module     modules/mod_fastcgi.so
FastCgiIpcDir /tmp/fastcgi

FastCgiConfig -appConnTimeout 30 -maxClassProcesses 128 -maxProcesses 128 -restart
ScriptAlias /php-go-fcgi-bin/ /home/apache2/cgi-bin/
<Directory “/home/apache2/cgi-bin”>
    SetHandler fastcgi-script
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
</Directory>

Action application/x-httpd-php /php-go-fcgi-bin/php

 

然后在需要支持php的vhost里面加上:

AddType application/x-httpd-php .php .phtml

 

这样就可以支持php了。

 

最后还要制定cgi脚本的位置,上面的配置制定了的是cgi-bin下面,可以ln -s来支持,但是这样如果直接访问这个php文件的话,会造成下载php-cgi执行文件的问题,所以就用了一个shell脚本来执行,这样子直接访问的话fastcgi就只会显示这个shell脚本。

vi /home/apache2/cgi-bin/php

#!/bin/sh

exec /home/php/bin/php-cgi

 

chmod a+x /home/apache2/cgi-bin/php

 

5、启动apache,/home/apache/bin/apachectl start 完事。

 

附上mod_astcgi动态的配置说明:

FastCgiConfig

Syntax: FastCgiConfig option [option ...]
Context: server config

The FastCgiConfig directive defines the default parameters for all dynamic FastCGI applications. This directive does not affect static or external applications in any way.

Dynamic applications are not started at server initialization, but upon demand. If the demand is heavy, additional application instances are started. As the demand fades, application instances are killed off. Many of the options govern this process.

Option can be one of (case insensitive):

-appConnTimeout n (0 seconds)
Unix:  The number of seconds to wait for a connection to the FastCGI application to complete or 0 to indicate a blocking connect() should be used. Blocking connect()s have an OS dependent internal timeout. If the timeout expires, a SERVER_ERROR results. For non-zero values, this is the amount of time used in a select() to write to the file descriptor returned by a non-blocking connect(). Non-blocking connect()s are troublesome on many platforms. See also -idle-timeout, it produces similar results but in a more portable manner.
Windows NT:  TCP based applications work as above. Named pipe based applications (static applications configured without the -port option and dynamic applications) use this value successfully to limit the amount of time to wait for a connection (i.e. it’s not “troublesome”). By default, this is 90 seconds (FCGI_NAMED_PIPE_CONNECT_TIMEOUT in mod_fastcgi.h).
-autoUpdate (none)
Causes mod_fastcgi to check the modification time of the application on disk before processing each request. If the application on disk has been changed, the process manager is notified and all running instances of the application are killed off. In general, it’s preferred that this type of functionality be built-in to the application (e.g. every 100th request it checks to see if there’s a newer version on disk and exits if so). There may be an outstanding problem (bug) when this option is used with -restart.
-flush (none)
Force a write to the client as data is received from the application. By default, mod_fastcgi buffers data in order to free the application as quickly as possible.
-gainValue n (0.5)
A floating point value between 0 and 1 used as an exponent in the computation of the exponentially decayed connection times load factor of the currently running dynamic FastCGI applications. Old values are scaled by (1 - gainValue), so making it smaller weights old values more than the current value (which is scaled by gainValue).
-idle-timeout n (30 seconds)
The number of seconds of FastCGI application inactivity allowed before the request is aborted and the event is logged (at the error LogLevel). The inactivity timer applies only as long as a connection is pending with the FastCGI application. If a request is queued to an application, but the application doesn’t respond (by writing and flushing) within this period, the request will be aborted. If communication is complete with the application but incomplete with the client (the response is buffered), the timeout does not apply.
-initial-env name[=[value]] (none)
A name-value pair to be passed in the initial environment when instances of applications are spawned. To pass a variable from the Apache environment, don’t provide the “=” (if the variable isn’t actually in the environment, it will be defined without a value). To define a variable without a value, provide the “=” without any value. The option can be used repeatedly.
-init-start-delay n (1 second)
The minimum number of seconds between the spawning of instances of applications. This delay decreases the demand placed on the system at server initialization.
-killInterval n (300 seconds)
Determines how often the dynamic application instance killing policy is implemented within the process manager. Smaller numbers result in a more aggressive policy, larger numbers a less aggressive policy.
-listen-queue-depth n (100)
The depth of listen() queue (also known as the backlog) shared by all instances of applications. A deeper listen queue allows the server to cope with transient load fluctuations without rejecting requests; it does not increase throughput. Adding additional application instances may increase throughput/performance, depending upon the application and the host.
-maxClassProcesses n (10)
The maximum number of dynamic FastCGI application instances allowed to run for any one FastCGI application. It must be <= to -maxProcesses (this is not programmatically enforced).
-maxProcesses n (50)
The maximum total number of dynamic FastCGI application instances allowed to run at any one time. It must be >= to -maxClassProcesses (this is not programmatically enforced).
-min-server-life n (30)
The minimum number of seconds a dynamic FastCGI application must run for before its restart interval is increased to 600 seconds. The server will get 3 tries to run for at least this number of seconds.
-minProcesses n (5)
The minimum total number of dynamic FastCGI application instances allowed to run at any one time without being killed off by the process manager (due to lack of demand).
-multiThreshold n (50)
An integer between 0 and 100 used to determine whether any one instance of a FastCGI application should be terminated. If the application has more than one instance currently running, this attribute will be used to decide whether one of them should be terminated. If only one instance remains, singleThreshold is used instead.
For historic reasons the mis-spelling multiThreshhold is also accepted.
-pass-header header (none)
The name of an HTTP Request Header to be passed in the request environment. This option makes available the contents of headers which are normally not available (e.g. Authorization) to a CGI environment.
-priority n (0)
The process priority to be assigned to the application instances (using setpriority()).
-processSlack n (5)
If the sum of the number of all currently running dynamic FastCGI applications and processSlack exceeds maxProcesses, the process manager invokes the killing policy. This is to improve performance at higher loads by killing some of the most inactive application instances before reaching maxProcesses.
-restart (none)
Causes the process manager to restart dynamic applications upon failure (similar to static applications).
-restart-delay n (5 seconds)
The minimum number of seconds between the respawning of failed instances of applications. This delay prevents a broken application from soaking up too much of the system.
-singleThreshold n (0)
An integer between 0 and 100 used to determine whether the last instance of a FastCGI application can be terminated. If the process manager computed load factor for the application is lower than the specified threshold, the last instance is terminated. In order to make your executables run in the “idle” mode for the long time, you would specify a value closer to 1, however if memory or CPU time is of primary concern, a value closer to 100 would be more applicable. A value of 0 will prevent the last instance of an application from being terminated; this is the default value, changing it is not recommended (especially if -appConnTimeout is set).
For historic reasons the mis-spelling singleThreshhold is also accepted.
-startDelay n (3 seconds)
The number of seconds the web server waits patiently while trying to connect to a dynamic FastCGI application. If the interval expires, the process manager is notified with hope it will start another instance of the application. The startDelay must be less than appConnTimeout to be effective.
-updateInterval n (300 seconds)
The updateInterval determines how often statistical analysis is performed to determine the fate of dynamic FastCGI applications.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.