您的位置:首页 > 运维架构 > Nginx

nginx学习总结四(nginx+php-cgi)

2011-01-06 22:55 344 查看



一、Nginx环境

前面说了nginx的优点:

服务器在3万并发连接下,开启的10个Nginx进程消耗150M内存(15M*10=150M),开启的64个php-cgi进程消耗1280M内存(20M*64=1280M),加上系统自身消耗的内存,总共消耗不到2GB内存。如果服务器内存较小,完全可以只开启25个php-cgi进程,这样php-cgi消耗的总内存数才500M。

二、安装前准备

1、安装GD库

安装freeytpe
# tar zxvf freetype-2.4.3.tar.gz -C /usr/local/src/freetype-2.4.3
# cd /usr/local/src/freetype-2.4.3
# ./configure --prefix=/usr/local/freetype-2.4.3
# make
# make install
安装libpng
#tar -zxvf libpng-1.4.4.tar.gz -C /usr/local/src/
# cd /usr/local/src/libpng-1.4.4
#./configure --prefix=/usr/local/libpng-1.4.4
# make
# make install

安装jpeg
# tar -zxvf jpegsrc.v8b.tar.gz -C /usr/local/src/
# cd /usr/local/src/jpeg-8b/
# ./configure --prefix=/usr/local/jpeg-8b
# make
# make install
# ./configure --prefix=/usr/local/jpeg --enable-shared --enable-static
注:在RHEL5系统64 位下编译jpeg-6b时候报以下错误
make: ./libtool: Command not found make: *** [jcapimin.lo] Error 127
错误分析:由于libtool版本过低导致的,进入jpeg-6b的源码目录,复制相关文件覆盖,然后重新configure。
cp /usr/share/libtool/config.sub .
cp /usr/share/libtool/config.guess .
安装GD
最好重新加载下lib #ldcongfig
查询下 #ldcongfig -p | grep jpeg
#ldcongfig -p | grep free
#ldcongfig -p | grep libpng
# tar zxvf gd-2.0.35.tar.gz -C /usr/local/src
# cd /usr/local/src/gd-2.0.35
# ./configure --prefix=/usr/local/gd-2.0.33 --with-jpeg=/usr/local/jpeg-8b --with-freetype=/usr/local/freetype-2.4.3 --with-png=/usr/local/libpng-1.4.4/ --with-zlib --enable-m4_pattern_allow
#make && make install
make[2]: *** [gd_png.lo] 错误 1

make[2]: Leaving directory `/usr/local/src/gd-2.0.33'

make[1]: *** [all-recursive] 错误 1

make[1]: Leaving directory `/usr/local/src/gd-2.0.33'

make: *** [all] 错误 2

解决办法

vi gd_png.c

找到“png.h”改成“/usr/local/libpng-1.4.4/include/png.h”
问题1:
编译最新的2.0.35版本,make的时候出现以下错误:
configure.ac:64: error: possibly undefined macro: AM_ICONV
If this token and others are legitimate, please use m4_pattern_allow.
See the Autoconf documentation.
make: *** [configure] Error 1
解决办法: configure的时候加上--enable-m4_pattern_allow参数;便能顺利编译安装
问题2:
aclocal:configure.ac:64: warning: macro `AM_ICONV' not found in library
解决:没有安装gettext软件包
gettext-0.14.6-4.el5、gettext-devel-0.14.6-4.el5

2、安装其他组件
安装 libiconv
#iconv的功能可算强大,可以对文本进行编码间的转换,它支持世界上很多的语言,当然我们主要用它来处理中文各种编码之间的转换,libiconv是一个开源的开发包,使用它可以非常方便的开发出适合自己用的编码转换工具。在这里http://gnuwin32.sourceforge.net/packages/libiconv.htm可以下载到libiconv的文档和开发包。
iconv本身linux系统自带,检查是否有/usr/bin/iconv 及 /usr/include/iconv.h 文件
安装 libmcrypt
#phpmyadmin基本上都安装完成了之后,出现了一个错误信息无法载入mcrypt 扩展
#Mcrypt扩展库依赖的包
# tar zxvf libmcrypt-2.5.8.tar.gz
# cd libmcrypt-2.5.8/
# ./configure
# make && make install
# /sbin/ldconfig
# cd libltdl/
# ./configure --enable-ltdl-install
# make && make install
安装mhash
#Mcrypt扩展库依赖的包
# tar zxvf mhash-0.9.9.9.tar.gz
# cd mhash-0.9.9.9/
# ./configure
# make
# make install
安装mcrypt
#其中,Mcrypt扩展库可以实现加密解密功能,就是既能将明文加密,也可以密文还原。PHP加密扩展库Mcrypt的算法和加密模式
# tar zxvf mcrypt-2.6.8.tar.gz
# cd mcrypt-2.6.8
# LD_LIBRARY_PATH=/usr/local/lib ./configure --with-libiconv-prefix
# make
# make install
注:configure时候出现如下错误:
configure: error: "You need at least libmhash 0.8.15 to compile this program http://mhash.sf.net/" 前面已经安装过mhash,初步分析为预编译的时候没找到lib文件所在的地址;
所以在./configure前面加上LD_LIBRARY_PATH=/usr/local/lib
安装 libevent
# tar -xzvf libevent-1.4.9-stable.tar.gz
# cd /usr/local/libevent-1.4.9-stable/
# ./configure --prefix=/usr/local/libevent
# make
# make install
#libevent是一个事件触发的网络库,适用于windows、linux、bsd等多种平台,内部使用select、epoll、kqueue等系统调用管理事件机制。著名分布式缓存软件memcached也是libevent based,,编译库代码,编译脚本会判断OS支持哪种类型的事件机制(select、epoll或kqueue),然后条件编译相应代码,供上层使用的接口仍然是保持统一的
安装pcre
#PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正规表达式库.这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是很有用的。Boost太庞大了,使用boost regex后,程序的编译速度明显变慢。测试了一下,同样一个程序,使用boost::regex编译时需要3秒,而使用pcre不到1秒。因此改用pcre来解决C语言中使用正则表达式的问题

# tar zxvf pcre-7.9.tar.gz
# cd pcre-7.9/
# ./configure
# make && make install

三、Nginx+PHP安装

1、PHP-CGI安装及配置

yum install libxml2-devel

# tar -zxvf php-5.2.10.tar.gz -C /usr/local/src
# cp /home/houzc/php-5.2.10-fpm-0.5.10.diff.gz /usr/local/src
#cd /usr/local/src

# gzip -cd php-5.2.10-fpm-0.5.11.diff.gz | patch -d php-5.2.10 -p1
注:将php-fpm解压到php-5.2.10
# cd /usr/local/src/php-5.2.10
# ./configure

--prefix=/usr/local/php-5.2.10 --with-mysql=/usr/local/mysql/ --with-freetype-dir=/usr/local/freetype-2.4.3 --with-jpeg-dir=/usr/local/jpeg-8b --with-png-dir=/usr/local/libpng-1.4.4/ --with-zlib --with-libxml-dir --enable-xml --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring //
--with-gd=/usr/local/gd-2.0.33 --with-openssl
# make && make install

配置php-cgi
# vi /usr/local/php-5.2.10/etc/php-fpm.conf
Equivalent to Apache MaxClients directive.
Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
Used with any pm_style.
<value name="max_children">500</value>
//修改php进程的最大数为500进程
Unix user of processes
<value name="user">www</value>
Unix group of processes
<value name="group">www</value>
//守护进程的用户
启动php-cgi
# echo “ulimit -SHn 65535” >> /etc/rc.loal
# /usr/local/php-5.2.10/sbin/php-fpm start
注:php-cgi进程,监听127.0.0.1的9000端口,进程数为500
(如果服务器内存小于3GB,可以只开启64个进程),用户为www(上面的配置)
php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload

2、NGINX安装与完整配置

注:需要先安装pcrc,见上面的安装过程
安装nginx
# tar zxvf nginx-0.7.65 .tar.gz
# cd nginx-0.7.65
# ./configure --prefix=/usr/local/nginx-0.7.65 --with-http_stub_status_module --with-http_ssl_module
//--with-http_stub_status_module:添加nginx监控参数
# make && make install

user www www;
worker_processes 30;
worker_cpu_affinity 00000001 00000001 00000001 00000001 00000010 00000010 00000010 00000010 00000100 00000100 00000100 00000100 00001000 00001000 00001000 00001000 00010000 00010000 00010000 00010000 00100000 00100000 00100000 00100000 01000000 01000000 01000000 01000000 10000000 10000000;

error_log logs/error.log;
pid nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
include /usr/local/nginx-0.7.65/conf/fastcgi_params;
keepalive_timeout

50;

sendfile on;
tcp_nopush on;
server_names_hash_bucket_size 128;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_http_version 1.1;
gzip_types text/javascript text/plain application/x-javascript text/css application/xml;

server {
listen 80;
server_name img.xywy.com
root html;

location / {
index index.htm index.php index.html;
root /www/img.xywy.com;
}

error_page 404 /404.php;
error_page 403 /404.php;

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/img.xywy.com$fastcgi_script_name;
include fastcgi_params;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d; //使用户浏览器缓存图片格式文件30天
}
location ~ .*\.(js|css)$
{
expries 1h; //使用户浏览器缓存js格式文件1小时
}
}

server
{
listen 80;
server_name status.206.xywy.com;

location / {
stub_status on;
access_log off;
}

}

启动nginx
# /usr/local/ nginx-0.7.65/sbin/nginx

当修改了nginx.conf配置,想测试一下是否正确,可以用下面的命令
# /usr/local/ nginx-0.7.65/sbin/nginx -t
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful

停止nginx
# kill -TERM `cat /usr/local/ nginx-0.7.65/logs/nginx.pid`

Nginx启动脚本

# cat /usr/local/ nginx-0.7.65/ngxin.sh

#!/bin/sh
### BEGIN INIT INFO
# Provides: Nginx-php-fpm(fastcgi)
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 3 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop nginx-fcgi in external FASTCGI mode
# Description: Start and stop nginx-fcgi in external FASTCGI mode
# http://www.linxutone.org msn:cnseek@msn.com
### END INIT INFO
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx-0.7.65/sbin/$NAME
CONFIGFILE=/usr/local/nginx-0.7.65/conf/nginx.conf
FPM_PATH=/usr/local/php-5.2.10/sbin/php-fpm
PIDFILE=/usr/local/nginx-0.7.65/logs/$NAME.pid
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

d_start() {
$FPM_PATH start > /dev/null 2>&1
$DAEMON -c $CONFIGFILE || echo -n " already running"
}
d_stop() {
$FPM_PATH stop > /dev/null 2>&1
kill -QUIT `cat $PIDFILE` || echo -n " not running"
}
d_reload() {
$FPM_PATH reload > /dev/null 2>&1
kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration ..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 1
d_start
echo "."
;;
*)
echo "Usage: $NAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0
写个

<?
phpinfo();

?>
test就可以了。



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息