Chinaunix首页 | 论坛 | 博客
  • 博客访问: 508775
  • 博文数量: 213
  • 博客积分: 7027
  • 博客等级: 少将
  • 技术积分: 1974
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-24 08:51
文章分类

全部博文(213)

文章存档

2007年(213)

我的朋友

分类: LINUX

2007-09-07 16:46:50

                    Nginx + PHP + FastCGI
Nginx是一个轻量级的 http server,也可作为 proxy server 使用,由一个俄罗斯哥们儿开发并在一些高负载的站点上跑了2年多。许多人用它来搭建 Rails 应用,有的评测性能已经超过 lighty。Nginx 能快速响应静态页面的请求,支持 fastcgi、ssl、virtual host、rewrite、HTTP Basic Auth、Gzip 等,功能比较完备,配置文件也是类似 lighty 那种很简介的形式,目前版本是0.5.8。Nginx 和 PHP 一起应用应该也是个不错的搭配:
1. 第一步安装 Nginx,最新版本是 0.5.8,配置:

./configure --prefix=/opt/nginx \
--sbin-path=/opt/nginx/sbin/nginx \
--conf-path=/opt/nginx/conf/nginx.conf \
--pid-path=/var/run/nginx/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--http-proxy-temp-path=/opt/nginx/temp/proxy \
--http-fastcgi-temp-path=/opt/nginx/temp/fcgi \
--lock-path=/var/run/nginx/nginx.lock \
--with-http_ssl_module \
--with-debug


如果是需要 rewrite 模块的话,系统需要有 pcre-devel 类似的包,然后就是 make ; make install 。

2.(nginx) config nginx
1) edit the nginx.conf
2) test the nginx.conf
# /opt/nginx/sbin/nginx -t -c /opt/nginx/conf/nginx.conf

3.(nginx) start && stop nginx

1) nginxctl.sh
-----------------------------------------------------------
#!/bin/bash

[ -x /opt/nginx/sbin/nginx ] || exit 0RETVAL=0

prog="nginx"

start() {
# Start daemons.
if [ -e /opt/nginx/conf/nginx.conf ] ; then

echo -n $"Starting $prog: "
/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf &
RETVAL=$?
[ $RETVAL -eq 0 ] && {
touch /var/lock/subsys/$prog
}
echo success $"$prog"
else
RETVAL=1
fi
return $RETVAL
}

stop() {
# Stop daemons.
echo -n $"Shutting down $prog: "
kill -3 `cat /var/run/nginx/nginx.pid 2>/dev/null`
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
return $RETVAL
}

case "$1" in

start )
start
;;

stop )
stop
;;

reconfigure )
if [ -f /var/lock/subsys/$prog ]; then
kill -1 `cat /var/run/nginx/nginx.pid 2>/dev/null`
RETVAL=$?
fi
;;

configtest )
/opt/nginx/sbin/nginx -t -c /opt/nginx/conf/nginx.conf
RETVAL=$?
;;

*)
echo $"Usage: $0 {start|stop|reconfigure|configtest}"
exit 1

esac

exit $RETVAL

2) # ln -s /opt/nginx/sbin/nginxctl.sh /sbin/nginxctl

3) /etc/rc.d/init.d/nginx.sh
-----------------------------------------------------------
#!/bin/bash

# nginx.sh
# Lijun ZHOU, 2007.01.17

# Location
# (Linux ) /etc/rc.d/init.d/nginx.sh, symlinked from rc3.d/S95nginx
# (FreeBSD) /usr/local/etc/rc.d/nginx.sh


if [ ! -e /var/run/nginx ]; then
mkdir -p /var/run/nginx
fi

chown -R web.web /var/run/nginx

chmod a+wt /var/run/nginx

/sbin/nginxctl start

# cd /etc/rc.d/rc3.d

# ln -s ../init.d/nginx.sh S95nginx


4. 然后需要 PHP 有 fastcgi 的支持,编译的时候带上 --enable-fastcgi 这个选项。

5. 运行FastCGI server,有2种方法,一种是运行 PHP 自带的 FastCGI server,可以用类似的脚本来执行;一种是使用 spawn-fcgi 这样的工具(lighty网站上有下载),推荐用后者,使用很方便:

spawn-fcgi -a 127.0.0.1 -p 9000 -u web -f /usr/bin/php -C 10
6. 配置 Nginx,将 nginx.conf 中关于 PHP 支持那段按实际情况稍作修改即可:

location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /webapp/web$fastcgi_script_name; include conf/fastcgi_params;}
那个 include 的 conf 目录里的文件可以从源码包里复制过来。

7. 启动 nginx,访问 localhost,能看到一个大大的 Welcome to nginx!,再跑几个 PHP 应用试试。



阅读(866) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~