[转]nginx+php(spawn-fcgi)


一、编译安装PHP 5.2.5所需的支持库

1、wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.12.tar.gz
tar -xvzf libiconv-1.12.tar.gz 
./configure --prefix=/usr/local
make
make install

ln -s  /usr/local/lib/libiconv.so.2 /usr/lib/

2、wget http://ftp.twaren.net/Unix/NonGNU/freetype/freetype-2.3.9.tar.gz
tar -xvzf freetype-2.3.9.tar.gz
./configure
make
make install


3、wget http://voxel.dl.sourceforge.net/sourceforge/libpng/libpng-1.2.35.tar.gz
tar -xvzf libpng-1.2.35.tar.gz 
./configure
make
make install

4、wget ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz
tar -xvzf jpegsrc.v6b.tar.gz
./configure --enable-static --enable-shared
make
make install
make install-lib

在x86_64位上,checking host system type... Invalid configuration `x86_64-unknown-linux-gnu': machine `x86_64-unknown' not recognized 
在做 configure 的时候,报上述的错误。 
解决方案: 
把 /usr/share/libtool/config.guess 覆盖到相关软件自带的config.guess 
把 /usr/share/libtool/config.sub 覆盖到相关软件自带的config.sub 


5、#----If your system was install libxml2, you do not need to install it.----
# 如果系统已经安装了libxml2,就不要再安装libxml了,我升级了一下,失败了,出现的错误没有解决
#ftp://ftp.gnome.org/pub/GNOME/sources/libxml2/2.6/ 我下载时,最新版为libxml2-2.6.30.tar.gz
#tar zxvf libxml2-2.6.30.tar.gz
#cd libxml2-2.6.30/
#./configure
#make
#make install


6、yum install libxml2-devel.i386
yum install curl-devel
否则编译php时:
configure: error: xml2-config not found. Please check your libxml2 installation.


7、wget http://voxel.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz
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

在x86_64位上,./libtool: line 3965: ELF [0-9][0-9]*-bit [LM]SB (shared object|dynamic lib ): command not found
在做 configure 的时候,报上述的错误。 
解决方案: 
yum  -y install libtool-ltdl
yum -y  install libtool-ltdl-devel
ln -sf /usr/lib64/libltdl.a /usr/local/lib/libltdl.a

8、wget http://voxel.dl.sourceforge.net/sourceforge/mhash/mhash-0.9.9.9.tar.gz
 tar -xvzf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install


9、cp /usr/local/lib/libmcrypt.* /usr/lib 
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
/sbin/ldconfig

10、wget http://voxel.dl.sourceforge.net/sourceforge/mcrypt/mcrypt-2.6.8.tar.gz
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
./configure
make
make install


二 安装mysql
1、wget  http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.33-linux-i686-glibc23.tar.gz/from/http://mirror.services.wisc.edu/mysql/
//添加用于启动MySQL的用户及用户组
groupadd mysql
useradd mysql -g mysql

tar -C /usr/local/ -xvzf mysql-5.1.33-linux-i686-glibc23.tar.gz
2、//建立符号链接,如果以后有新版本的MySQL的话,你可以仅仅将源码解压到新的路径,然后重新做一个符号链接就可以了。这样非常方便,
数据也更加安全。
ln -s /usr/local/mysql-5.1.33-linux-i686-glibc23/ /usr/local/mysql

3、//初始化授权表
cd /usr/local/mysql
scripts/mysql_install_db --user=mysql
注意:执行该步骤时,必须将 /etc/mysql/ 下的 my.cnf 文件删除或改名,否则将会报错

4、//修改MySQl目录的所有权
chown -R mysql.mysql /usr/local/mysql-5.1.33-linux-i686-glibc23/
chown -R mysql.mysql /usr/local/mysql

5、//配置系统启动时自动启动MySQl
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
(另:mysql 5.5.8,启动时:/etc/init.d/mysqld: line 256: my_print_defaults: command not found
修改/etc/init.d/mysqld:  
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data)

6、cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf  
注:my-medium.cnf为中档规模的mysql,更多,查看/usr/local/mysql/support-files/目录

7、//启动Mysql
/service mysqld start


8、//添加mysql命令到/usr/local/bin目录
echo "export PATH=/usr/local/mysql/bin:$PATH" >>/etc/profile
source /etc/profile


9、//添加libmysqlclient.so.15动态链接库到/usr/lib
ln -s /usr/local/mysql/lib/libmysqlclient.so.15 /usr/lib


10、更改mysql root 密码
use mysql 
update user set Password=password('newpassword') where User='root'; 
flush privileges;




三 编译安装PHP(FastCGI模式)
1、
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-mysql=/usr/local/mysql \
--with-iconv-dir=/usr/local \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir  \
--enable-xml \
--disable-debug \
--disable-rpath \
--enable-discard-path \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--with-gd \
--with-curlwrappers \
--enable-mbregex \
--enable-fastcgi \
--enable-force-cgi-redirect \
--enable-mbstring \
--with-mcrypt \
--enable-ftp

php5.3.3以后的版本源码不需要再打php-fpm补丁,该补丁已集成进php5.3.3,
在编译时,不需再–enable-fastcgi了,在php5.3.3中强制启用fastcgi了。

在X86_64位上时,编译有问题,需要去掉下面两个扩展:--with-curl --with-curlwrappers 

sed -i 's#-lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt#& -liconv#' Makefile
必须,否则报错:
collect2: ld returned 1 exit status
make: *** [sapi/cgi/php-cgi] Error 1

make
make install


2、cp php.ini-recommended  /usr/local/php/etc/php.ini


3、编译安装PHP5扩展模块
wget http://pecl.php.net/get/memcache-2.2.5.tgz
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install



4、修改php.ini文件
sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"\nextension = "memcache.so"\n#' /usr/local/php/etc/php.ini

6、 关于php的404错误,查看是否打开了php.ini的下面的参数
cgi.fix_pathinfo=0
该参数用于是否使用完整路径



四 fastcgi
要在nginx上跑php,首先需要启动fastcgi,方法有三种,一是用php自带的fastcgi server,二是用lighttpd带的spawn-fcgi,三是用php-fpm。

我使用的是spawn-fcgi
1、一些相关的rpm包
yum install  bzip2*
yum install pcre-devel

2、获得spawn-fcgi,spawn-fcgi现在被分离出来,做为一个新的项目

wget http://www.lighttpd.net/download/spawn-fcgi-1.6.1.tar.gz
 
 tar -xvzf spawn-fcgi-1.6.1.tar.gz
 ./configure
 make 
 make install

/usr/local/bin/spawn-fcgi -h

3、创建www用户和组,以及其使用的目录:
groupadd www -g 48
useradd -u 48 -g www www
mkdir -p /EBS/www
chmod +w /EBS/www
chown -R www:www /EBS/www

4、启动php-cgi进程,监听127.0.0.1的10080端口,进程数为64(如果服务器内存小于3GB,可以只开启25个进程),脚本:

#! /bin/bash
address="127.0.0.1"
port="9000"
pidpath="/tmp/spawn_phpcgi_${port}.pid"
user="www"
group="www"
phpcgi="/usr/local/php/bin/php-cgi"
PHP_FCGI_CHILDREN=25
PHP_FCGI_MAX_REQUESTS=1000


echo_ok ()
{
echo -ne "\033[33C ["
echo -ne "\033[32m"
echo -ne "\033[1C OK"
echo -ne "\033[39m"
echo -ne "\033[1C ]\n"
}

start_spawn()
{
        env - PHP_FCGI_CHILDREN=${PHP_FCGI_CHILDREN} PHP_FCGI_MAX_REQUESTS=${PHP_FCGI_MAX_REQUESTS} /usr/local/bin/spawn-fcgi -a ${a
ddress} -p ${port} -u ${user} -g ${group} -f ${phpcgi} -P ${pidpath} 1>/dev/null
        echo -ne "php-cgi start successfull"
        echo_ok
}

case "$1" in
start)
        if [ ! -f $pidpath ]
        then
                start_spawn
        else
                pidcount=`ps -ef |grep ${phpcgi}|wc -l`
                if [ "$pidcount" -gt "1" ]
                then
                        echo -ne "php-cgi  already  running  " 
                        echo_ok
                else
                        rm -f $pidpath
                        start_spawn
                fi
        fi
        ;;

stop)
        pid=`cat ${pidpath} 2>/dev/null`
        kill ${pid} 2>/dev/null
        rm -f ${pidpath} 2>/dev/null
        echo -ne "php-cgi  stop successfull"
        echo_ok
        ;;
*)
echo "Usage: $0 {start|stop]}"
        exit 1
esac
exit

cp /usr/local/php/etc/php.ini /usr/local/php/bin/php.ini
注意:使用cgi时,读取的php.ini为与php-cgi程序在同一目录的php.ini
五、安装Nginx 0.6.36
1、echo ulimit -HSn 51200 >>/etc/profile
source /etc/profile

tar -xvzf nginx-0.6.36.tar.gz

2
./configure  --prefix=/usr/local/nginx \
--with-http_stub_status_module \
--without-poll_module \
--without-select_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_perl_module

make
make install


启动脚本

#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# this script create it by jackbillow at 2007.10.15.

# it is v.0.0.2 version.

# if you find any errors on this scripts,please contact jackbillow.

# and send mail to jackbillow at gmail dot com.

#

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

#              It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /usr/local/nginx/logs/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf


nginxd=/usr/local/nginx/sbin/nginx

nginx_config=/usr/local/nginx/conf/nginx.conf

nginx_pid=/var/run/nginx.pid


RETVAL=0

prog="nginx"


# Source function library.

. /etc/rc.d/init.d/functions


# Source networking configuration.

. /etc/sysconfig/network


# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0


[ -x $nginxd ] || exit 0



# Start nginx daemons functions.

start() {


if [ -e $nginx_pid ];then

   echo "nginx already running...."

   exit 1

fi


   echo -n $"Starting $prog: "

   daemon $nginxd -c ${nginx_config}

   RETVAL=$?

   echo

   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

   return $RETVAL


}



# Stop nginx daemons functions.

stop() {

        echo -n $"Stopping $prog: "

        killproc $nginxd

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid

}



# reload nginx service functions.

reload() {


    echo -n $"Reloading $prog: "

    #kill -HUP `cat ${nginx_pid}`

    killproc $nginxd -HUP

    RETVAL=$?

    echo


}


# See how we were called.

case "$1" in

start)

        start

        ;;


stop)

        stop

        ;;


reload)

        reload

        ;;


restart)

        stop

        start

        ;;


status)

        status $prog

        RETVAL=$?

        ;;

*)

        echo $"Usage: $prog {start|stop|restart|reload|status|help}"

        exit 1

esac


exit $RETVAL

 


3、编辑相关配置文件
(1).nginx配置文件
vi /usr/local/nginx/conf/nginx.conf


 

user  www www;

worker_processes 10;

error_log  logs/error.log notice;
pid        logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process. 
worker_rlimit_nofile 51200;

events 
{
      use epoll;

      worker_connections 51200;
}

http 
{
      include      mime.types;
      default_type  application/octet-stream;

      charset  gb2312;
      
      server_names_hash_bucket_size 128;
      
      #sendfile on;
      #tcp_nopush     on;

      keepalive_timeout 60;

      tcp_nodelay on;

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

      server
      {
              listen       80;
              server_name  192.168.1.2;
              index index.html index.htm index.php;
              root  /EBS/www;

              if (-d $request_filename)
              {
                     rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
              }
                             
              location ~ .*\.php?$
              {
                   include fcgi.conf;      
                   fastcgi_pass  127.0.0.1:9000;
                   fastcgi_index index.php;
              }

              log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
                                    '$status $body_bytes_sent "$http_referer" '
                                    '"$http_user_agent" $http_x_forwarded_for';
              access_log  logs/access.log  access;
      }


}

 

(2)、php cgi 配置文件
vi /usr/local/nginx/conf/fcgi.conf
注:nginx自带了一个配置文件,/usr/local/nginx/conf/fastcgi_params,该配置文件缺少红色字体的部分,会造成访问php文件时报404错误。
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param  REDIRECT_STATUS    200;


六 相关Linux内核参数
vi /etc/sysctl.conf,添加:

 

net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000    65000

 



使配置生效
sysctl -p

猜你喜欢

转载自yuanhsh.iteye.com/blog/1310906
今日推荐