Elton's Blog

Tag: fastcgi

升级内核后spawn-fcgi无法启动

by on 九.17, 2009, under Linux, PHP

今天升级Linux内核到2.6.30-r5。但是升级重启后,发现blog打不开了。Nginx报502的Gateway错误。

第一反应就是起到php解析作用的spawn-fcgi没有启动起来。于是尝试再次手动启动。 但是发现怎么也启动不起来。

开始以为是因为升级内核引起的,于是退回以前的内核版本,结果一样。始终无法启动spawn-fcgi的fastcgi服务。说明不是内核的问题。 检查Nginx日志和系统日志都没有什么有价值的信息。

在一筹莫展的时候,突然注意到启动spawn-fcgi的命令中包含了php-cgi这个命令。 会不会是php的问题呢。 后来手工直接执行,发现确实php有问题。 重新使用emerge -av php编译安装php后,终于可以启动spawn-fcgi了。 网站也一切正常了。

出现问题的原因应该是以前使用emerge升级过系统,部分升级影响了php所需要调用的文件,当时没有重新启动,升级后的影响没有马上生效。 这次升级内核重启后,那些升级都生效了,结果问题就暴露出来了。

以后出现问题还是不要着急,任何环节都有可能出现问题。 而且有时候可能被问题的表象所蒙蔽,比如这次升级就误认为是因为内核的问题,但是其实后来证明根本跟内核没有关系。 出现问题还是要不放过任何可能出现问题的地方,即使你觉得不可能出现问题的地方也要去测试一下,逐一排查,最终肯定可以找到问题的原因的。

Gentoo下安装PHP和Nginx请参考之前发表的帖子

Leave a Comment :, , more...

CentOS环境中lighttpd+php+fastcgi+eAccelerator安装配置

by on 六.21, 2009, under Linux, PHP

Lighttpd 作为新一代的web server,以小巧(不到1M的大小)、快速而著称,因为服务器上安装了rails、java,并以lighttpd为前端代理服务器,不想再部署apache了,所以直接使用lighttpd来部署,顺便看一下性能如何。

本文主要介绍在CentOS下,配置一套用lighttp作为web server的php环境

· 安装Lighttpd
从http://www.lighttpd.net/download/下载源码
安装前先检查pcre是否安装,需要pcre和pcre-devel两个包。 用yum search pcre*检查,如果都是installed就是都安装了。否则安装缺少的包。

1
2
3
4
5
yum install pcre-devel
 
tar xzvf lighttpd-1.4.23.tar.gz
cd lighttpd-1.4.23
./configure --prefix=/usr/local/lighttpd

configure完毕以后,会给出一个激活的模块和没有激活模块的清单,可以检查一下,是否自己需要的模块都已经激活,在enable的模块中一定要有“mod_rewrite”这一项,否则重新检查pcre是否安装。然后编译安装:

1
make && make install

编译后配置:

1
2
3
cp doc/sysconfig.lighttpd /etc/sysconfig/lighttpd
mkdir /etc/lighttpd
cp doc/lighttpd.conf /etc/lighttpd/lighttpd.conf

如果你的Linux是RedHat/CentOS,那么:

1
cp doc/rc.lighttpd.redhat /etc/init.d/lighttpd

如果你的Linux是SuSE,那么:

1
cp doc/rc.lighttpd /etc/init.d/lighttpd

其他Linux发行版本可以自行参考该文件内容进行修改。然后修改/etc/init.d/lighttpd,把

1
lighttpd="/usr/sbin/lighttpd"

改为

1
lighttpd="/usr/local/lighttpd/sbin/lighttpd"

此脚本用来控制lighttpd的启动关闭和重起:

1
2
3
/etc/init.d/lighttpd start
/etc/init.d/lighttpd stop
/etc/init.d/lighttpd restart

如果你希望服务器启动的时候就启动lighttpd,那么:

1
chkconfig lighttpd on

这样lighttpd就安装好了,接下来需要配置lighttpd。

配置Lighttpd

修改/etc/lighttpd/lighttpd.conf
1)server.modules
取消需要用到模块的注释,mod_rewrite,mod_access,mod_fastcgi,mod_simple_vhost,mod_cgi,mod_compress,mod_accesslog是一般需要用到的。

2)server.document-root, server.error-log,accesslog.filename需要指定相应的目录

3)用什么权限来运行lighttpd
server.username = “nobody”
server.groupname = “nobody”
从安全角度来说,不建议用root权限运行web server,可以自行指定普通用户权限。

4)静态文件压缩
compress.cache-dir = “/tmp/lighttpd/cache/compress”
compress.filetype = (“text/plain”, “text/html”,”text/javascript”,”text/css”)
可以指定某些静态资源类型使用压缩方式传输,节省带宽,对于大量AJAX应用来说,可以极大提高页面加载速度。

5)配置ruby on rails

最简单的配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$HTTP["host"] == "www.xxx.com" {
 server.document-root = "/yourrails/public"
 server.error-handler-404 = "/dispatch.fcgi"
 fastcgi.server = (".fcgi" =>
    ("localhost" =>
      ("min-procs" => 10,
       "max-procs" => 10,
       "socket" => "/tmp/lighttpd/socket/rails.socket",
       "bin-path" => "/yourrails/public/dispatch.fcgi",
       "bin-environment" => ("RAILS_ENV" => "production")
      )
    )
 )
}

即由lighttpd启动10个FCGI进程,lighttpd和FCGI之间使用本机Unix Socket通信。

如果想指定www.abc.com以及所有二级域名,则需要把第一行改为
$HTTP[”host”] =~ “(^|.)abc.com” {

}

如果要设置代理,比如lighttpd和tomcat整合,tomcat放在lighttpd后面,则需要通过代理访问tomcat

$HTTP["host"] =~ “www.domain.cn” {
proxy.server = ( “” => ( “localhost” => ( “host”=> “127.0.0.1″, “port”=> 8080 ) ) )
}

则www.domain.cn为主机的网址都交给tomcat处理,tomcat的端口号为8080. 在tomcat的虚拟主机中,需要捕获www.domain.cn这个主机名,设置这个虚拟主机。这里的host都是跟tomcat里面的虚拟主机对应的。

· 安装支持fastcgi的PHP
安装PHP所需的相关类库
curl

1
2
3
4
5
6
7
wget http://curl.cs.pu.edu.tw/download/curl-7.19.5.tar.bz2
 
tar xvjf curl-7.19.5.tar.bz2
cd curl-7.19.5
./configure --prefix=/usr/local/curl
make
make install

gettext

1
2
3
4
5
6
wget ftp://ftp.ntu.edu.tw/pub/gnu/gnu/gettext/gettext-0.17.tar.gz
tar xvzf gettext-0.17.tar.gz
cd gettext-0.17
./configure --prefix=/usr/local/gettext
make
make install

zlib

1
2
3
4
5
wget http://kent.dl.sourceforge.net/sourceforge/libpng/zlib-1.2.3.tar.gz
tar xvzf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure --prefix=/usr/local/zlib
make && make install

libpng

1
2
3
4
5
wget http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/l/li/libpng/libpng-1.2.9.tar.gz
tar xvzf libpng-1.2.9.tar.gz
cd libpng-1.2.9
./configure --prefix=/usr/local/libpng
make && make install

jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
wget http://www.ijg.org/files/jpegsrc.v6b.tar.gz
tar xvzf jpegsrc.v6b.tar.gz
cd jpeg-6b/
./configure --prefix=/usr/local/jpeg6
make
 
mkdir /usr/local/jpeg6/bin
mkdir -p /usr/local/jpeg6/bin
mkdir -p /usr/local/jpeg6/man/man1
mkdir -p /usr/local/jpeg6/lib
mkdir -p /usr/local/jpeg6/include
make install-lib
make install

freetype

1
2
3
4
5
6
wget http://download.savannah.gnu.org/releases/freetype/freetype-2.3.9.tar.gz
tar xvzf freetype-2.3.9.tar.gz
cd freetype-2.3.9
./configure --prefix=/usr/local/freetype2
make
make install

gd

1
2
3
4
5
6
7
wget http://www.libgd.org/releases/gd-2.0.35.tar.gz
tar xvzf gd-2.0.35.tar.gz
cd gd-2.0.35
./configure --prefix=/usr/local/gd2 --with-zlib=/usr/local/zlib/ --with-png=/usr/local/libpng/ --with-jpeg=/usr/local/jpeg6/ --with-freetype=/usr/local/freetype2/
make
如果第一次make出错,试着再make一次,我就是这样,第二次就对了。
make install

PHP

1
2
3
4
5
6
7
8
tar xvzf php-5.2.10.tar.gz
cd php-5.2.10
./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-jpeg-dir=/usr/local/jpeg6/ --with-png-dir=/usr/local/libpng/ --with-gd=/usr/local/gd2/ --with-freetype-dir=/usr/local/freetype2/  --with-zlib-dir=/usr/local/zlib --with-curl=/usr/local/curl --with-gettext=/usr/local/gettext --enable-fastcgi --enable-zend-multibyte --with-config-file-path=/etc --enable-discard-path --enable-force-cgi-redirect
make
make install
cp php.ini-dist /etc/php.ini
 
可以使用php -m查看你安装的模块

eAccelerator
eAccelerator是一个开源的PHP加速器

1
2
3
4
5
6
7
8
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
tar xjvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3
export PHP_PREFIX="/usr/local/php"
$PHP_PREFIX/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/bin/php-config
make
make install

执行好后,会提示安装到的路径,下面会用到,如我的被安装到这里
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613
编辑php.ini中的内容
vim /etc/php.ini

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cgi.fix_pathinfo = 1
 
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

如果一切顺利,你可以通过下面命令来验证是否安装成功

1
2
3
4
5
$ php -v
PHP 5.2.10 (cli) (built: Jun 20 2009 23:32:09)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
    with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator

修改/etc/lighttpd/lighttpd.conf文件,添加下面的配置
vim /etc/lighttpd/lighttpd.conf

1
2
3
4
5
6
7
8
fastcgi.server             = ( ".php" =>
                               ( "localhost" =>
                                 (
                                   "socket" => "/tmp/php-fastcgi.socket",
                                   "bin-path" => "/usr/local/php/bin/php-cgi"
                                 )
                               )
                            )

重启lighttpd

1
/etc/init.d/lighttpd restart

写一个php测试文件在lighttpd的网站目录里,测试php是否安装成功

Leave a Comment :, , , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...