前言
Zeus是一个运行于Unix下的非常优秀的Web Server,而PHP则是Unix下一个非常优秀的后台脚本语言。 这两个产品都是为非常喜欢的产品。为什么要写这样的一个Howto呢?是因为有大量的网站脚本是使用PHP开发的, 而这些程序运行在Zeus下也是一个非常好的选择。写这份文档的目的在于能让大家的PHP系统良好的运行于Zeus服务器上。
很早的时候我写过一份整合Zeus和PHP的文章,它主要是讲如何将PHP以FastCGI的本地调用方式来运行于Zeus中的, 本份Howto主要是来讲如何让PHP运行于Remote Responders方式下。因为这样会比以local方式有更高的可扩展性和运行效率。
准备工作
首先你应该Zeus安装完成,也不会过多的讲如何安装和配置Zeus服务器本身,因为它的安装和配置实在是太简单了。
如果有可能,最好将ports使用cvsup来升级一下。
对于PHP依赖的相关软件如果能提前装,则从ports中安装好。如MySQL、GD等。 安装fastcgi
注意,如果你安装的PHP的版本会低于4.3.0哪么你才需要这步。新的PHP版本已经内置了fastcgi的库。安装fastcgi的方法非常的简单:
root@~$cd /usr/ports/www/fcgi/
root@/usr/ports/www/fcgi$make;make install;make clean
编译完成后我们可以在/usr/local/lib目录中看到有libfcgi.a文件,同时在/usr/local/include目录中会有fastcgi的所有头文件。
编译PHP
编译PHP可以使用通常的PHP编译选项,下面是我使用的一个配置:
./configure --prefix=/usr/local/php --enable-fastcgi --with-mysql=/usr/local
需要注意的是在PHP 4.3.0以上版本是使用的 --enable-fastcgi 选项,而PHP 4.3.0以前的版本应使用--with-fastcgi参数。
接着就是安装PHP到系统中:
make
make install
编译完成后,我们来测试一下安装的php是能正确运行:
root@~$cd /usr/local/php/bin/
root@/usr/local/php/bin$./php
这时将进入php代码输入状态,输入
phpinfo(); ?>
按ctrl-d运行后会见到php返回的信息则为正确。
配置FastCGI/PHP
配置FastCGI
进入Zeus管理控制台,打开需要配置的VHost配置。进入 API Support 中的 FastCGI 设置。在其中设置:
Enabling Support for FastCGI Programs : Enable
在Configuring FastCGI Remote Responders中设置:
Directory name : /usr/local/php/bin/php
Location: Machine name: localhost
Additional methods supported?:None
如果你的PHP应用不在本机,即使PHP没有安装在本机上,哪么也要设置 Docroot path 参数,它可以不存在于相应的目录下。
都设置完成后,点击 Apply 按钮。 配置PHP解析指向
进入 URL Handling 中的 Handlers 设置。在 Adding a Handler 中设置以下参数:
File Extension : php
Specify the path and filename of the handler, relative to the document root : /usr/local/php/bin/php
HTTP 404 errors are handled by : The handler
注意,这里的 Specify the path and filename of the handler, relative to the document root 应和你上一步设置的 Directory name 值相同。
都设置完成后,点击 Apply 按钮。
所有的设置完成后使用vhost的commit功能将更 改提交并应用。这样Zeus就设置好了Fastcgi和PHP的相关参数。
配置FastCGI/PHP启动
在Zeus的rc.d目录中新建一个S05php的文件,内容为:
#!/bin/sh
# Script to start and stop the persistent PHP runner for FastCGI.
# Please check paths before use.
# FastCGI PHP binary
FPHPBIN=/usr/local/php/bin/php
# Location to place semaphore
SEMFILE=/tmp/php.pid
PHP_FCGI_CHILDREN=100
PHP_FCGI_MAX_REQUESTS=1000
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
# This is Linux - use /proc to increase the local (ephemeral) port range
#echo 1024 65000 > /proc/sys/net/ipv4/ip_local_port_range
if [ -z "$ZEUSHOME" ]
then
cd `dirname $0`/..
ZEUSHOME=`pwd`
export ZEUSHOME
fi
case "$1" in
'start')
if [ -e $SEMFILE ]
then
echo FastCGI PHP error: already running.Restart FastCGI PHP now
kill `cat $SEMFILE`
sleep 5
fi
if [ ! -x $FPHPBIN ]
then
echo FastCGI PHP error: please check that $FPHPBIN is executable and exists.
exit 1
fi
echo Starting FastCGI PHP.
$ZEUSHOME/web/bin/fcgirunner --user=65534 --group=65534 --pidfile=$SEMFILE 8002 $FPHPBIN
;;
'stop')
if [ -e $SEMFILE ]
then
echo Stopping FastCGI PHP.
kill `cat $SEMFILE`
rm $SEMFILE
exit 0
fi
'restart')
if [ -e $SEMFILE ]
then
echo Stopping FastCGI PHP.
kill `cat $SEMFILE`
sleep 5
fi
echo Starting FastCGI PHP.
$ZEUSHOME/web/bin/fcgirunner --user=65534 --group=65534 --pidfile=$SEMFILE 8002 $FPHPBIN
*)
echo "usage: $0 {start|stop|restart}"
;;
esac
exit 1
In this script there are the following contents which need to be changed according to the system conditions Modification:
FPHPBIN=/usr/local/php/bin/php should be set to the path of php
SEMFILE=/tmp/php.pid to generate the path of php.pid, the directory must be writable
PHP_FCGI_CHILDREN=100 Number of php processes
PHP_FCGI_MAX_REQUESTS=1000 The number of requests each php process can respond to before exiting, used to release resources. The above two settings are based on hardware configuration and website visits. The default value is 8,500. Generally speaking, PHP_FCGI_CHILDREN > Maximum concurrent access + 10
PHP_FCGI_MAX_REQUESTS If the setting is too small, websites with a large number of visits will frequently increase the load due to restarts of the PHP process.
#echo 1024 65000 > /proc/sys/net/ipv4/ip_local_port_range only for linux
--user=65534 --group=65534 is the user and group for the php process to run, generally set to the nobody user FreeBSD is 65534/65534, Linux is 99/99
Finally, set the S05php file as an executable file and run FastCGI/PHP:
chmod 755 S05php
./S05php start
Once started, PHP_FCGI_CHILDREN+1 php processes will be displayed in the ps -ax list.
Create an info.php file in the Docroot directory corresponding to your vhost. The content is:
phpinfo();
?>
Use a browser to access the vhost. info.php file, you should be able to see the PHP info page.
Notes
Thanks to CCF forum owner hunreal for writing the S05php script, it is really easy to use!
If you want to know anything else, you can go to the Zeus PHP support page to get more information.
Note: Please indicate the source of any reprint or excerpt (Chinese FreeBSD User Group http://www.cnfug.org)