LINUX环境下Nginx与php-fpm安装部署

来源:互联网 发布:京东店铺销量数据 编辑:程序博客网 时间:2024/05/21 03:19

一、系统环境准备:

1.安装gc++和gcc 支持

Yum install gcc*

2.安装mysql支持(依赖于php环境,如果不用php则无需安装)

yum installmysql-devel.x86_64

3.安装libtool libtool-ltdl-deve支持

Yum install libtool*

4.安装xml2支持

yum install  libxml2

5.安装 libxml2-devel 支持

yum install libxml2-devel

6.安装 curl 支持

yum install libcurl

7.安装 curl-devel 支持

yum install libcurl-devel

二、程序安装包列表:

正则表达式安装包:pcre-8.34.tar.gz

nginx 安装包:nginx-1.4.7.tar.gz

nginx 清除缓存模块:ngx_cache_purge-2.1.tar.gz

nginx 本地缓存模块:ngx_slowfs_cache-1.10.tar.gz

如果采用php:则需要安装以下插件:

png 图像处理模块:libpng-1.2.31.tar.gz

jpeg 处理模块:jpegsrc.v6b.tar.gz

字体 引擎库模块:freetype-2.4.11.tar.gz

php 绘图模块:gd-2.0.33.tar.gz

php 压缩模块:zlib-1.2.3.tar.gz

php 环境:php-5.4.28

php 缓存加速器:xcache-3.1.0.tar.gz

三、Nginx安装

1.Nginx下载:

http://nginx.org/en/download.html下载相应的版本,我们这里使用nginx1.4.7

2.正则表达式包下载:

http://sourceforge.net/projects/pcre/files/pcre/8.34/  下载pcre-8.34.tar.gz

3.安装正则表达式prce:

#Tar zxvf pcre-8.34.tar.gz

#cd pcre-8.34

#./configure

#make

#make install

4.安装nginx

# tar zxvfnginx-1.4.7.tar.gz

# tar zxvfngx_cache_purge-2.1.tar.gz

# tar zxvfngx_slowfs_cache-1.10.tar.gz

# cd nginx-1.4.7

# ./configure  --prefix=/opt/nginx/--with-http_realip_module --with-http_stub_status_module --add-module=/opt/pkg/ngx_cache_purge-2.1  --add-module=/opt/pkg/ngx_slowfs_cache-1.10

# make

# make install

# cd /opt/nginx/sbin/

# ./nginx -t

./nginx: error whileloading shared libraries: libpcre.so.1: cannot open shared object file: No suchfile or directory

注意:这里的报错,解决方法如下:

# ln -s/lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1

# ./nginx -t

nginx: the configurationfile /opt/nginx//conf/nginx.conf syntax is ok

nginx: configuration file/opt/nginx//conf/nginx.conf test is successful

完事大吉,nginx已经安装完毕。

./nginx 启动

通过浏览器来访问,会出现nginx的欢迎界面。

四、php-fpm安装

1.安装libpng

# tar -zxvflibpng-1.2.31.tar.gz

# cd  libpng-1.2.31/scripts/

# mv  makefile.linux  ../makefile

# cd ../

# make

# make install

2.安装jpegsrc

# tar -zxvfjpegsrc.v6b.tar.gz

# cd jpeg-6b

# ./configure--enable-shared --enable-static

# make

注: 执行make命令时出错,如下图:

错误分析:由于libtool版本过低导致的,重新下载新版本的libtool以默认方式安装,执行以下命令:
./configure
make
make install
然后进入jpeg-6b的源码目录,然后执行以下步骤,切记!
cp /usr/share/libtool/config/config.sub .
cp /usr/share/libtool/config/config.guess .
./configure --enable-shared --enable-static
make
mkdir -p /usr/local/man/man1
make install

3.安装freetype

# tar zxvffreetype-2.4.11.tar.gz

# cd freetype-2.4.11

# ./configure

# make

# make install

会出现如下报错:

解决方法:

mkdir/usr/local/include/freetype2/freetype/internal
       需要在freetype的安装目录下创建internal 这个目录然后,
       make install

4.安装gd

# tar -zxvfgd-2.0.33.tar.gz

# cd gd-2.0.33

# ./configure --with-png--with-freetype --with-jpeg

# make install

# cp  /usr/local/lib/libgd.so  /usr/local/include/

# cd  /usr/local/include/

# vim gd_io.h

在void (*gd_free) (structgdIOCtx *);下一行新增:

void (*data);

5.安装zlib

# tar zxvfzlib-1.2.3.tar.gz

# cd zlib-1.2.3

# CFLAGS="-O3-fPIC" ./configure

# make

# make install

6.安装php

# tar -vxjf php-5.4.28.tar.bz2

# cd php-5.4.28

#

# ./configure --prefix=/opt/php5  --disable-debug  --with-mysql--with-config-file-path=/opt/php5 --with-gd=/usr/local/include  --with-jpeg-dir=/usr/local/include --with-zlib  --with-png-dir=/usr/local/include --with-curl--with-freetype-dir=/usr/local/include/freetype2/freetype/  --enable-fpm --enable-mbstring

# make

# make install

#cpphp.ini-production  /opt/php5/php.ini

#cd /opt/php5/etc/

#cp -rfphp-fpm.conf.default php-fpm.conf

7.安装xcache

# tar zxvfxcache-3.1.0.tar.gz

# cd xcache-3.1.0

#执行/opt/php5/bin/phpize

# ./configure--with-php-config=/opt/php5/bin/php-config --enable-xcache

# make

# make install

# cat xcache.ini  >> /opt/php5/php.ini

# cd /opt/php5/

# vim  php.ini

修改:

xcache.admin.user ="xcache"

xcache.admin.pass ="20869b649c6d7403dddd97ae7d1f2881"

注:echo -n "bewinner"|md5sum  //获得md5 加密串

xcache.readonly_protection= On

xcache.mmap_path =    "/opt/php5/xcache"

# touch xcache

# chmod 777 xcache

8.启动php-fpm

/opt/php5/sbin/php-fpm

使用netstat -ano | grep 9000查看端口是否已监听。

五、nginx相关配置说明

1.配置nginx来支持php

    server {

        listen      80;

        server_name  127.0.0.1;   

        location ~ \.php$ {

            root           /opt/webroot/bbs;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            include        fastcgi.conf;

        }

    }

       让nginx来支付php,将所有的php动态文件交由php来处理。

2.Nginx中FastCGI参数的优化

fastcgi_connect_timeout600;指定连接到后端FastCGI的超时时间。

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

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

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

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

fastcgi_busy_buffers_size128k;的默认值是fastcgi_buffers的两倍。

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

3.Nginx中gzip参数配置说明:

gzip on;nginx采用gzip压缩的形式发送数据。这将会减少我们发送的数据量。

gzip_disable"msie6";为指定的客户端禁用gzip功能。我们设置成IE6或者更低版本以使我们的方案能够广泛兼容。

gzip_min_length  1k;设置对数据启用压缩的最少字节数。如果一个请求小于1000字节,我们最好不要压缩它,因为压缩这些小的数据会降低处理此请求的所有进程的速度。

gzip_buffers 4 16k; 设置gzip申请内存的大小,其作用是按块大小的倍数申请内存空间。

gzip_http_version 1.0;用于识别http协议的版本,早期的浏览器不支持gzip压缩,用户会看到乱码,所以为了支持前期版本加了此选项,目前此项基本可以忽略。

gzip_comp_level 4;设置数据的压缩等级。这个等级可以是1-9之间的任意数值,9是最慢但是压缩比最大的。我们设置为4,这是一个比较折中的设置。

gzip_types   text/plain application/x-javascript text/cssapplication/xml;设置需要压缩的数据格式。

gzip_vary on;//和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持。因此,为避免浪费不支持的也压缩,需要根据客户端的HTTP头来判断,是否需要压缩。

4.Nginx和php的动静态分离

动静态分离指的为,静态资源由nginx来处理,动态资源由php-fpm来进行处理。

  server {

       listen       80;

       server_name  pcpay.gamecomb.com;

             location / {

            root   /opt/project/recharge;

            index  index.php index.html index.html;

            proxy_set_header Host $host;

               }注:recharge下的静态资源由nginx来处理。

       location ~.*\.(gif|jpg|jpeg|png|bmp|swf)$

    {

      expires      30d;

    }

    location ~ .*\.(js|css)?$

    {

      expires      1h;

    }注:包括,图片资源,jscss等均由nginx来处理,缓存。

        location ~ \.php$ {

            root           /opt/project/recharge;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            include        fastcgi.conf;

        }注:所有的php程序,均交由后端的php来处理,到fastcgi,连接端口为9000端口。

}

5.Nginx缓存的应用

nginx具有web缓存服务,proxy_cache,但是有一个问题就是,proxy_cache不能清除指定的URL缓存,只能设置URL过期时间,但是nginx第三方模块ngx_cache_purge能清除指定URL。

  nginx安装时需要将ngx_cache_purege加载进去。我们再安装nginx时已经完成安装。

 

代码如下:

       location ~* /

(gamecomb2.0/cjactive|gamecomb2.0/goldcode|gamecomb2.0/images|gamecomb2.0/newyearsact|gamecomb2.0/sc

ripts|gamecomb2.0/signactivity|gamecomb2.0/signactivity2|gamecomb2.0/style|gamecomb2.0/video|gamecomb2.0/wap|gamecomb2.0/

yjactive|static|template/default){

            root   /opt/webroot/bbs;

            slowfs_cache        local_cache;

            slowfs_cache_key    $uri$is_args$args;

            slowfs_cache_valid  1d;

            expires     1d;

       }

代码解释:

代码以为为缓存gamecomb2.0文件夹下cjactive goldcode images等文件夹,缓存时间为一天,失效时间为1天。

注:nginx动态处理的不进行缓存,缓存只针对静态处理文件。

6.Nginx 301重定向配置

       server {

    listen       80;

   server_name  test.gamecomb.comaaa.gamecomb.com

    #server_namelocalhost;

    if ($host !='test.gamecomb.com' ) {

        rewrite^/(.*)$ http://aaa.gamecomb.com$1 permanent;

    }

}

如果访问的 URL 不是test.gamecomb.com,则跳到aaa.gamecomb.com上面去。

7.Nginx多站点配置

在一台服务器上,我们有时候需要同时跑几个应用。比如既有app1,又有app2,分别对应不同的域名app1对应test.gamecomb.com,app2对应aaa.gamecomb.com如何来实现呢?

首先是 Nginx 的主配置,位于 /opt//nginx/ngnix.conf,让它保持默认就行:

http {

    server {

        listen       80;

        server_name localhost;

               location / {

            ......

       }

       location ~ \.php$ { 

          .......

       }

    }

    include /opt/nginx/conf.d/*.conf;

}

注意到这一句,include/opt/nginx/conf.d/*.conf; 它会加载 conf.d 文件夹下的所有配置文件。那么接下来的事情就简单了,我们设计两个 .conf ,一个是app1的配置,一个是app2的配置。

App1.conf配置:

server {

    listen      80;

    server_name aaa.gamecomb.com;

   location / {

            root   /opt/webroot/app1;

            index  index.php index.html index.html;

            proxy_set_header Host $host;

                     }

        location ~ \.php$ {

            root           /opt/webroot/app1;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            include        fastcgi.conf;

        }

}

App2.conf配置:

server {

    listen      80;

    server_name aaa.gamecomb.com;

   location / {

            root   /opt/webroot/app2;

            index  index.php index.html index.html;

            proxy_set_header Host $host;

                     }

        location ~ \.php$ {

            root           /opt/webroot/app2;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            include        fastcgi.conf;

        }

}

8.Nginx反向代理实现

在我本机上发布了一个应用,是由tomcat 或者 apache来动态处理,但是当前服务器压力较大,tomcat或者apache压力过大,处理不过来,需要交由另外一台服务器来处理,那么这种的需求就需要用到nginx的一个典型的功能,反向代理。

Nginx 反向代理的指令不需要新增额外的模块,默认自带 proxy_pass 指令,只需配置相关代码就可以实现反向代理。

首先要使用到的是nginx的upstrem模块,定义一个名称为estore_yy_pool的服务器,里面的配置有ip地址(为要反向代理到的ip地址),权重,超时时间。

配置好后,再用location发布的模块中使用proxy_pass实现即可。代码如下:

下面代码实现的功能就是当用户访问到154.5这台服务器的12580buyopmng应用时,不是交由本机的tomcat来处理,而是由upstream定义好的estore_yy_pool去处理,及反向代理到172.29.154.9服务器。

http {

       upstream estore_yy_pool{

              server 172.29.154.9:5050  weight=1 max_fails=2 fail_timeout=30s;

                     }

    server {

        listen       8080;

        server_name  172.29.154.5;

       location /12580buyopmng/ {

                     include /usr/local/nginx/conf/block.conf;

                     proxy_set_header Host $host:8080;

                     proxy_set_header  X-Real-IP-BW $remote_addr;

                     proxy_set_header X-Forward-For$remote_addr;

                     proxy_pass http://estore_yy_pool;

              }

    }

}