比思論壇

標題: linux下nginx连接PHP5.5 [打印本頁]

作者: lq396295206    時間: 2013-12-30 10:37
標題: linux下nginx连接PHP5.5
一。安装php5.5

1.安装依赖包

yum install gcc make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel libaio -y



2.安装php5.5

tar -zxvf php-5.5.6.tar.gz

cd php-5.5.6



Shell代码  
./configure \   
--prefix=/usr/local/php5 \   
--with-config-file-path=/usr/local/php5/etc \   
--with-bz2 \   
--with-curl \   
--enable-ftp \   
--enable-sockets \   
--disable-ipv6 \   
--with-gd \   
--with-jpeg-dir=/usr/local \   
--with-png-dir=/usr/local \   
--with-freetype-dir=/usr/local \   
--enable-gd-native-ttf \   
--with-iconv-dir=/usr/local \   
--enable-mbstring \   
--enable-calendar \   
--with-gettext \   
--with-libxml-dir=/usr/local \   
--with-zlib \   
--with-pdo-mysql=mysqlnd \   
--with-mysqli=mysqlnd \   
--with-mysql=mysqlnd \   
--enable-dom \   
--enable-xml \   
--with-libdir=lib64 \   
--enable-pdo \   
--with-oci8=instantclient,/usr/lib/oracle/11.2/client/lib \   
--with-pdo-oci=instantclient,/usr,10.2.0.1 \   
--enable-fpm  

./configure \
--prefix=/usr/local/php5 \
--with-config-file-path=/usr/local/php5/etc \
--with-bz2 \
--with-curl \
--enable-ftp \
--enable-sockets \
--disable-ipv6 \
--with-gd \
--with-jpeg-dir=/usr/local \
--with-png-dir=/usr/local \
--with-freetype-dir=/usr/local \
--enable-gd-native-ttf \
--with-iconv-dir=/usr/local \
--enable-mbstring \
--enable-calendar \
--with-gettext \
--with-libxml-dir=/usr/local \
--with-zlib \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-mysql=mysqlnd \
--enable-dom \
--enable-xml \
--with-libdir=lib64 \
--enable-pdo \
--with-oci8=instantclient,/usr/lib/oracle/11.2/client/lib \
--with-pdo-oci=instantclient,/usr,10.2.0.1 \
--enable-fpm 编译过程有可能报如下错误:

checking for known struct flock definition… configure: error: Don’t know how to define struct flock on this system, set –enable-opcache=no

解决方法:

yum groupinstall "Development tools"



说明:以上配置根据自己需要选择就行,如果使用nginx+php,就必须使用php-fpm。

make

make install



3.配置php5.5

cp php-5.5.6/php.ini-production /usr/local/php5/etc/php.ini  #php配置文件

cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf #php-fpm配置文件



4.启动php-fpm

/usr/local/php5/sbin/php-fpm

查看是否启动成功:

netstat -lnt | grep 9000

tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN



vi /usr/local/php5/etc/php-fpm.conf

打开pid = run/php-fpm.pid前的注释,这样fpm的进程就会被写入这个文件:/usr/local/php5/var/run/php-fpm.pid



php-fpm 关闭:

kill -INT `cat /usr/local/php5/var/run/php-fpm.pid`

php-fpm 重启:

kill -USR2 `cat /usr/local/php5/var/run/php-fpm.pid`



修改了php.ini后要记得重启php-fpm。



二。nginx配置

1.http下增加如下内容:

Conf代码  
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;      
fastcgi_connect_timeout 300;      
fastcgi_send_timeout 300;      
fastcgi_read_timeout 300;      
fastcgi_buffer_size 64k;      
fastcgi_buffers 4 64k;      
fastcgi_busy_buffers_size 128k;      
fastcgi_temp_file_write_size 128k;      
fastcgi_cache TEST;      
fastcgi_cache_valid 200 302 1h;      
fastcgi_cache_valid 301 1d;      
fastcgi_cache_valid any 1m;   

fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;   
fastcgi_connect_timeout 300;   
fastcgi_send_timeout 300;   
fastcgi_read_timeout 300;   
fastcgi_buffer_size 64k;   
fastcgi_buffers 4 64k;   
fastcgi_busy_buffers_size 128k;   
fastcgi_temp_file_write_size 128k;   
fastcgi_cache TEST;   
fastcgi_cache_valid 200 302 1h;   
fastcgi_cache_valid 301 1d;   
fastcgi_cache_valid any 1m;  
下面是对上述代码的含义进行介绍。

第一行代码是为FastCGI缓存指定一个文件路径、目录结构等级、关键字区域存储时间和非活动删除时间。

fastcgi_connect_timeout:指定连接到后端FastCGI的超时时间。

fastcgi_send_timeout:指定向FastCGI传送请求的超时时间,这个值是已经完成两次握手后向FastCGI传送请求的超时时间。

fastcgi_read_timeout:指定接收FastCGI应答的超时时间,这个值是已经完成两次握手后接收FastCGI应答的超时时间。

fastcgi_buffer_size:用于指定读取FastCGI应答第一部分需要用多大的缓冲区,这个值表示将使用1个64KB的缓冲区读取应答的第一部分(应答头),可以设置为fastcgi_buffers选项指定的缓冲区大小。

fastcgi_buffers:指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求。如果一个PHP脚本所产生的页面大小为256KB,那么会为其分配4个64KB的缓冲区来缓存;如果页面大小大于256KB,那么大于256KB的部分会缓存到fastcgi_temp指定的路径中,但是这并不是好方法,因为内存中的数据处理速度要快于硬盘。一般这个值应该为站点中PHP脚本所产生的页面大小的中间值,如果站点大部分脚本所产生的页面大小为256KB,那么可以把这个值设置为“16 16k”、“4 64k”等。

fastcgi_busy_buffers_size:的默认值是fastcgi_buffers的两倍。

fastcgi_temp_file_write_size:表示在写入缓存文件时使用多大的数据块,默认值是fastcgi_buffers的两倍。

fastcgi_cache:表示开启FastCGI缓存并为其指定一个名称。开启缓存非常有用,可以有效降低CPU的负载,并且防止502错误的发生,但是开启缓存也会引起很多问题,要视具体情况而定。

fastcgi_cache_valid:fastcgi用来指定应答代码的缓存时间,实例中的值表示将200和302应答缓存一个小时,将301应答缓存1天,其他应答均缓存1分钟。



2.server下配置如下内容:

Conf代码  
location ~* \.(php[3-9]?|phtm[l]?)(\/.*)*$ {   
        fastcgi_index index.php;   
        fastcgi_pass 127.0.0.1:9000;   
        include fastcgi_params;   
        set $path_info "";   
        set $real_script_name $fastcgi_script_name;   
        if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {   
                set $real_script_name $1;   
                set $path_info $2;   
        }   
        fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;   
        fastcgi_param SCRIPT_NAME $real_script_name;   
        fastcgi_param PATH_INFO $path_info;   
}  

        location ~* \.(php[3-9]?|phtm[l]?)(\/.*)*$ {
                fastcgi_index index.php;
                fastcgi_pass 127.0.0.1:9000;
                include fastcgi_params;
                set $path_info "";
                set $real_script_name $fastcgi_script_name;
                if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                        set $real_script_name $1;
                        set $path_info $2;
                }
                fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
                fastcgi_param SCRIPT_NAME $real_script_name;
                fastcgi_param PATH_INFO $path_info;
        }

这里的配置方式可以支持CI、ThinkPHP等基于PATH_INFO的请求方式。



还有,如果希望隐藏index.php,则在上面的配置下进行如下配置:



Conf代码  
location /ICBBS {         
                if (!-e $request_filename){   
                rewrite (.*) /ICBBS/index.php/$1;         
                }   
        }  

location /ICBBS {      
                if (!-e $request_filename){
                rewrite (.*) /ICBBS/index.php/$1;      
                }
        }



说明:ICBBS就是你的项目名称,这里将所有以ICBBS请求的不存在路径跳转到/ICBBS/index.php。


作者: yct1978    時間: 2014-3-13 12:33
学习学习




歡迎光臨 比思論壇 (http://108.170.5.98:8080/) Powered by Discuz! X2.5