FreeBSD使用mod_fastcgi模块配置Apache PHP

时间:2020-01-09 10:38:27  来源:igfitidea点击:

问题描述:如何配置和安装带有PHP5和mod_fastcgi的Apache Web服务器,以在FreeBSD服务器下更快地访问PHP?

解决方法:

mod_fastcgi是Apache Web服务器的cgi模块。

FastCGI是CGI的独立于语言,可扩展的开放扩展,可提供高性能而不受服务器特定API的限制。

Red Hat Linux/CentOS Linux特定的mod_fastcgi教程在这里。

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
    with XCache v1.2.2, Copyright (c) 2005-2007, by mOo

如果看不到单词cgi-fcgi,请访问/usr/ports/lang/php5重新编译具有fastcgi支持的php:

# cd /usr/ports/lang/php5

# make config

# make install clean

通过选择FastCGI选项启用FastCGI支持

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

# cd /usr/ports/lang/php5-extensions/

# make install clean

配置FreeBSD PHP5扩展

配置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地址:10.16.49.131
  • 域名:theitroad.com
  • DocumentRoot目录:/websites/home/theitroad.com/http
  • 日志目录:/websites/home/theitroad.com/logs
  • PHP cgi-bin目录:/websites/home/theitroad.com/cgi-bin /

如上所述创建目录结构:

# mkdir -p /websites/home/theitroad.com/{http,https,logs,cgi-bin,images,private,stats}

# chown -R theitroadftpuser:theitroadftpgroup /websites/home/theitroad.com/

根据您的设置替换用户名,组名和目录。

将以下配置指令追加到httpd.conf虚拟主机部分:

<VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot "/websites/home/theitroad.com/http"
        ServerName theitroad.com
        ServerAlias www.theitroad.com
        ErrorLog "/websites/home/theitroad.com/logs/error.log"
        CustomLog "/websites/home/theitroad.com/logs/access.log" common
        ScriptAlias /cgi-bin/ "/websites/home/theitroad.com/cgi-bin/"
 
	<Directory "/websites/home/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/theitroad.com/cgi-bin">
		AllowOverride None
		Options None
		Order allow,deny
		Allow from all
	</Directory>
 
</VirtualHost>

保存并关闭文件。

创建php.cgi脚本

创建如下的shell脚本(/websites/home/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/theitroad.com/cgi-bin/。
设置权限:

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

重新启动Apache

输入以下命令以重新启动Apache:

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

测试您的设置

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

<?php
phpinfo();
?>

可选:使用FreeBSD PF防火墙打开端口80

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

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

关闭并保存文件。
重新加载防火墙:

# /etc/rc.d/pf restart