在FreeBSD Apache中如何配置 PHP mod_fastcgi模块

时间:2019-11-20 08:53:57  来源:igfitidea点击:

mod_fastcgi是apache web服务器的一个cgi模块。
FastCGI是CGI的一个独立于语言的、可扩展的、开放的扩展,它提供了高性能而不受服务器特定api的限制。

FreeBSD 安装Apache web服务器

使用FreeBSD ports集合安装apache:

# cd /usr/ports/www/apache22
# make install clean
# echo 'apache22_enable="YES"' >> /etc/rc.conf

在FreeBSD 安装php5

确保php cgi二进制文件存在,并且编译时设置了支持fastcgi:

# /usr/local/bin/php-cgi -v

输出示例

PHP 5.2.6 with Suhosin-Patch 0.9.6.2 (cgi-fcgi) (built: Sep 10 2008 19:07:02)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies

如果没有看到cgi-fcgi这个单词,那么使用fastcgi支持重新编译php:

# cd /usr/ports/lang/php5
# make config
# make install clean

同时确保安装了php-gd,php-mysql和其他扩展:

# cd /usr/ports/lang/php5-extensions/
# make install clean

FreeBSD配置 Apache mod_fastcgi

安装mod_fastcgi:

# cd /usr/ports/www/mod_fastcgi
# make install clean

打开/usr/local/etc/apache22/httpd.conf文件:

# vi /usr/local/etc/apache22/httpd.conf

确保下面的行是取消注释的:

LoadModule fastcgi_module     libexec/apache22/mod_fastcgi.so

创建虚拟主机配置

测试配置:

  • Apache共享IP地址:74.86.49.131
  • 域名:www.theitroad.com
  • DocumentRoot目录:/websites/home/www.theitroad.com/http
  • 日志目录:/websites/home/www.theitroad.com/logs
  • PHP cgi-bin目录:/websites/home/www.theitroad.com/cgi-bin /

创建目录结构:

# mkdir -p /websites/home/www.theitroad.com/{http,https,logs,cgi-bin,images,private,stats}
# chown -R theosftpuser:theosftpgroup /websites/home/www.theitroad.com/

httpd.conf配置参考:

<VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot "/websites/home/www.theitroad.com/http"
        ServerName www.theitroad.com
        ServerAlias www.theitroad.com
        ErrorLog "/websites/home/www.theitroad.com/logs/error.log"
        CustomLog "/websites/home/www.theitroad.com/logs/access.log" common
        ScriptAlias /cgi-bin/ "/websites/home/www.theitroad.com/cgi-bin/"
 
	<Directory "/websites/home/www.theitroad.com/http">
		Options -Indexes FollowSymLinks +ExecCGI
		AllowOverride AuthConfig FileInfo
		AddHandler php5-fastcgi .php
		Action php5-fastcgi /cgi-bin/php.cgi
		Order allow,deny
		Allow from all
	</Directory>
 
	<Directory "/websites/home/www.theitroad.com/cgi-bin">
		AllowOverride None
		Options None
		Order allow,deny
		Allow from all
	</Directory>
 
</VirtualHost>

创建php脚本

创建如下的shell脚本(/websites/home/www.theitroad.com/cgi-bin/php.cgi):

#!/bin/sh
# Shell Script To Run PHP5 using mod_fastcgi under Apache 2.x
# Tested under FreeBSD 6.x and 7.x
### Set PATH ###
PHP_CGI=/usr/local/bin/php-cgi
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
### no editing below ###
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec $PHP_CGI

将上述shell脚本重命名为php.cgi,复制到/websites/home/www.theitroad.com/cgi-bin/。
设置权限:

# chmod +x /websites/home/www.theitroad.com/cgi-bin/php.cgi

重启Apache

执行以下命令以重启Apache:

# /usr/local/etc/rc.d/apache22 restart

在FreeBSD防火墙中打开端口80

将以下PF防火墙规则添加到/etc/pf.conf文件:

pass in on $ext_if proto tcp from any to 74.86.49.131 port 80 flags S/SA synproxy state

重新加载防火墙:

# /etc/rc.d/pf restart

测试

将以下代码放在/websites/home/www.theitroad.com/http/test.php中:

<?php
phpinfo();
?>

使用浏览器打开 http://www.theitroad.com/test.php