LAMP

[转]在apache下php以编译fastcgi方式运行

http://blog.163.com/gwo-cce@126/blog/static/32573649200882342821925/

一般php的安装都是编译为apache的一个模块,运行在apache的上下文中,权限比较高.如果不是特别注重安全性和使用一些线程不安全的模块,没有必要搞cgi.cgi模式一般比module要慢.但是,最近安装php5在make install时总是说dlname not found很是烦恼,想尝试编译成fastcgi看看效果.

一、安装php

wget http://cn2.php.net/get/php-5.2.5.tar.bz2/from/cn.php.net/mirror
tar jxvf php-5.2.5.tar.bz2
cd php-5.2.5
./configure --prefix=/usr/local/php5-fcgi --with-mysql=/usr/local/mysql --with-zlib-dir --with-freetype-dir=/usr --with-jpeg-dir=/usr --with-png-dir=/usr --enable-gd-native-ttf --with-gd --enable-track-vars --enable-ftp --with-iconv --with-gettext --with-curl --enable-fastcgi --with-openssl

1. 编译参数不能加 –with-apxs2=/usr/local/apache2/bin/apxs 否则安装出来的php执行文件是cli模式的

2. 如果系统已经安装好了zlib、freetype、jpeg和png是gd库的基础,一般系统都会带有,那么用–with-xxxx-dir=/usr 就行了.gd2也不要另外下载安装了,貌似gd库的核心成员被zend挖走了,总之php源码自己带有一套更加帅气的gd库,用–with-gd就ok 了

make
make install
cp php.ini-dist /usr/local/php5-fcgi/lib/php.ini

二、 安装mod_fcgid

其实php官方主推fascgi,但是国内有牛人发现它的管理模式有点冗余,详情请看

http://fastcgi.coremail.cn/

经过测试,国人的fascgi在进程回收,内存控制方面比较不错.

wget http://jaist.dl.sourceforge.net/sourceforge/mod-fcgid/mod_fcgid.2.2.tgz
tar zxvf mod_fcgid.2.2.tgz
cd mod_fcgid.2.2
make
make install

安装好以后在apache的 modules 目录下会有一个 mod_fcgid.so

附fastcgid的安装过程

wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz
tar zxvf mod_fastcgi-2.4.6.tar.gz
cd mod_fastcgi-2.4.6

如果web服务器是apache2

cp Makefile.AP2 Makefile
vi Makefile

把top_dir = /usr/local/apache 修改成apache的安装目录

make
make install

三、配置apache

vi一下httpd.conf添加这样的内容

LoadModule fcgid_module modules/mod_fcgid.so

ScriptAlias /fcgi-bin/ "/usr/local/php5-fcgi/bin/"

AddHandler php-fastcgi .php

Action php-fastcgi /fcgi-bin/php-cgi

AddType application/x-httpd-php .php

Options FollowSymLinks ExecCGI

AllowOverride None

Order allow,deny

allow from all

第一句话装载mod_fcgid,fastcgi用LoadModule fcgid_module modules/mod_fcgid.so这个去装载

然后建立一个/fcgi-bin/的虚拟目录到php-cgi的那个目录,分别用AddHandler和Action关联apache和php-cgi,然后AddType建立一个web文件类型php,最后用Directory定义一下访问权限.

其实建立/fcgi-bin/虚拟目录偷懒了,为了更好的安全性应该是用ln -s将php-cgi链到另外的目录,而不是将整个php安装目录暴露,诸如此类的写法

AddHandler fcgid-script .php .py .pl .fcgi

IdleTimeout 300

ProcessLifeTime 1800

MaxProcessCount 100

DefaultMinClassProcessCount 3

DefaultMaxClassProcessCount 8

IPCConnectTimeout 15

IPCCommTimeout 300

MaxRequestsPerProcess 100

# RewriteCond %{REQUEST_URI} ^/fcgi-bin/.* [NC]

# RewriteRule .*/php$ /nonexistent.file [L]

IdleTimeout 发呆时限

ProcessLifeTime 一个进程的最长生命周期,过期之后无条件kill

MaxProcessCount 最大进程个数

DefaultMinClassProcessCount 每个程序启动的最小进程个数

DefaultMaxClassProcessCount 每个程序启动的最大进程个数

IPCConnectTimeout 程序响应超时时间

IPCCommTimeout 与程序通讯的最长时间,上面的错误有可能就是这个值设置过小造成的

MaxRequestsPerProcess 每个进程最多完成处理个数,达成后自杀,因为PHP最多只处理500次请求。不过这个是mod_fcgid 1.11版本添加的,我们主机上暂时不支持。

重启一下apache就ok了.

通过用ab对比PHP模块运行的服务器,FASTCGI要慢很多,目前还没找解决方案.

四、参考资料

http://linux.chinaunix.net/techdoc/install/2008/01/06/975966.shtml

http://www.seaoffire.net/fcgi-faq.html

http://typo3.org/development/articles/using-php-with-mod-fcgid/page/3/

[url=http://xok.la/2008/07/php_fastcgi_apache.html][/url]

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/249/showart_1092019.html

成功的Red Hat / CentOS Apache 2 FastCGI PHP Configuration

http://www.cyberciti.biz/tips/rhel-centos-fedora-apache2-fastcgi-php-configuration.html

Red Hat / CentOS Apache 2 FastCGI PHP Configuration

by Vivek Gite [Last updated: December 30, 2008]

FastCGI is a protocol for interfacing interactive programs with a web server. FastCGI’s main aim is to reduce the overhead associated with interfacing the web server and CGI programs, allowing a server to handle more web page requests at once.

Also, PHP is not recommended with multithreaded Apache2 (worker MPM) because of performance and some 3rd party PHP extensions are not not guaranteed thread-safe.

nginx and lighttpd has inbuilt support for FastCGI. For Apache web server you need to use either mod_fastcgi or mod_fcgid.

Why use mod_fastcgi instead of mod_perl / mod_php?

From the wikipedia article:

Instead of creating a new process for every request, FastCGI can use a single persistent process which handles many requests over its lifetime. Processing of multiple requests simultaneously is achieved either by using a single connection with internal multiplexing (ie. multiple requests over a single connection) and/or by using multiple connections. Many such processes can exist, something that can increase stability and scalability. FastCGI also allows programs to get the web server to do certain simple operations, like reading in a file, before the request is handed over. Environment information and page requests are sent from the web server to the process over a TCP connection (for remote processes) or Unix domain sockets (for local processes). Responses are returned from the process to the web server over the same connection. The connection may be closed at the end of a response, but the web server and the process are left standing.

Many web site administrators and programmers are finding that the separation of web applications from the web server in FastCGI (and the simpler SCGI) has many desirable advantages over embedded interpreters (mod_perl, mod_php, etc.). This separation allows server and application processes to be restarted independently — an important consideration for busy web sites. It also facilitates per-application security policies — important for ISPs and web hosting companies.

In this quick tutorial, you will learn about Apache 2 + mod_fastcgi + PHP installation and configuration under Red Hat Enterprise Linux / CentOS Linux version 5.x+.

Install mod_fastcgi

Make sure required packages are installed (httpd-devel and apr-devel required to compile mod_fastcgi), enter:
# yum install libtool httpd-devel apr-devel apr
Next, grab the latest mod_fastcgi source code, enter:
# cd /opt
# wget http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz

Untar tar ball, type:
# tar -zxvf mod_fastcgi-current.tar.gz
# cd mod_fastcgi-2.4.6/

Make a copy of Makefile.AP2, enter:
# cp Makefile.AP2 Makefile
Compile and install mod_fastcgi for 32 bit system, enter:
# make top_dir=/usr/lib/httpd
# make install top_dir=/usr/lib/httpd

Compile and install mod_fastcgi for 64 bit system, enter:
# make top_dir=/usr/lib64/httpd
# make install top_dir=/usr/lib64/httpd

Sample output:

make install top_dir=/usr/lib64/httpd
make[1]: Entering directory `/tmp/mod_fastcgi-2.4.6'
/usr/lib64/apr-1/build/libtool --silent --mode=install cp mod_fastcgi.la /usr/lib64/httpd/modules/
make[1]: Leaving directory `/tmp/mod_fastcgi-2.4.6'

Configure mod_fastcgi

Open /etc/httpd/conf.d/mod_fastcgi.conf file
# vi /etc/httpd/conf.d/mod_fastcgi.conf
Add an entry to it like this:
LoadModule fastcgi_module modules/mod_fastcgi.so
Save and close the file. Restart httpd, enter:
# service httpd restart
Sample output:

[Mon Dec 29 23:24:44 2008] [notice] caught SIGTERM, shutting down
[Mon Dec 29 23:24:44 2008] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Dec 29 23:24:44 2008] [notice] Digest: generating secret for digest authentication ...
[Mon Dec 29 23:24:44 2008] [notice] Digest: done
[Mon Dec 29 23:24:44 2008] [notice] FastCGI: process manager initialized (pid 4785)
[Mon Dec 29 23:24:44 2008] [notice] Apache/2.2.3 (CentOS) configured -- resuming normal operations

How do I configure PHP as FastCGI process under RHEL / CentOS Linux?

First, you need to disable mod_php5, enter:
# mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.disable

Create a shell script in cgi-bin directory called php.fcgi

Create a script as follows in /var/www/cgi-bin/php.fcgi (or put in your virtual domain cgi-bin directory)

#!/bin/bash
# Shell Script To Run PHP5 using mod_fastcgi under Apache 2.x
# Tested under Red Hat Enterprise Linux / CentOS 5.x
### Set PATH ###
PHP_CGI=/usr/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

Set permission, type:
# chmod +x /var/www/cgi-bin/php.fcgi
Finally, modify documentroot directory permission as follows. You need to use AddHandler and Action directives for mod_fastcgi:

 <Directory "/var/www/html">
   Options -Indexes FollowSymLinks +ExecCGI
    AllowOverride AuthConfig FileInfo
    AddHandler php5-fastcgi .php
    Action php5-fastcgi /cgi-bin/php.fcgi
    DirectoryIndex index.php index.html
    Order allow,deny
    Allow from all
</Directory>

Where,

  • AddHandler php5-fastcgi .php : To configure Apache to handle php files (within the scope of the directive) with the specified extension(s) as FastCGI applications.
  • Action php5-fastcgi /cgi-bin/php.fcgi : This directive adds an action, which will activate cgi-script when action-type is triggered by the request. The cgi-script is the URL-path to a resource that has been configured as a CGI script using ScriptAlias. In our case, requests for files with a .php file extension of are handled instead by the specified cgi script /cgi-bin/php.fcgi.

Save and close the file. Restart httpd:
# service httpd restart

mod_fastcgi virtual hosting configuration

  • Domain name: nixcraft.com
  • DocumentRoot: /websites/home/nixcraft.com/http
  • cgi-bin directory: /websites/home/nixcraft.com/cgi-bin
  • php.fcgi path: /websites/home/nixcraft.com/cgi-bin/php.fcgi
  • Logs file directory: /websites/home/nixcraft.com/logs

Based upon above settings your virtualhosting configuration for nixcraft.com domain should look like as follows:

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

Create a /websites/home/nixcraft.com/cgi-bin/php.fcgi as follows:

#!/bin/bash
PHP_CGI=/usr/bin/php-cgi
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec $PHP_CGI

Set permission and restart httpd:
# chmod +x /websites/home/nixcraft.com/cgi-bin/php.fcgi
# service httpd restart

cannot restore segment prot after reloc: Permission denied


/usr/local/apache/bin/apachectl restart
httpd: Syntax error on line 55 of /usr/local/apache/conf/httpd.conf: Cannot load /usr/local/apache/modules/libphp5.so into server: /usr/local/apache/modules/libphp5.so: cannot restore segment prot after reloc: Permission denied
解决办法:
先禁止掉SELinux
更改/etc/sysconfig/selinux 文件的内容为 SELINUX=disabled
再使用:chcon -t texrel_shlib_t /usr/local/apache/modules/libphp5.so

CentOS下PHP 5.2.x的安装、优化及安全设置

http://ecco.blog.hexun.com/6928963_d.html

编译PHP手册的新办法

http://phpeye.com/article/view/id/159

一个对Apache+suExec+fastcgi+php有点研究的人

http://www.sunboyu.cn/category/original/apache-original

eAccelerator 缓存编译好的PHP

只对DSO和FCGI有效

设置AUTO_INCREMENT开始值

ALTER TABLE `video` AUTO_INCREMENT=1

Apache2 始终403错误

原因就是selinux
关闭selinux
selinux的配置文件在/etc/selinux/config,改成disable

或者给目录设置权限
chcon -R -t httpd_user_content_t public_html/
参考

http://www.toplee.com/blog/93.html

http://www.webjx.com/server/linux/server_linux_2007_11_05_525.html

http://www.libing.net.cn/read.php/957.htm

nameservers in linux

in file /etc/resolv.conf