一、简介

  Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为 “engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器.Nginx是由俄罗斯人 Igor Sysoev为俄罗斯访问量第二的 Rambler.ru站点开发的,它已经在该站点运行超过三年了。Igor Sysoev在建立的项目时,使用基于BSD许可。
  在高并发连接的情况下,Nginx是Apache服务器不错的替代品。Nginx同时也可以作为7层负载均衡服务器来使用。Nginx 0.8.46 + PHP 5.2.14 (FastCGI) 可以承受3万以上的并发连接数,相当于同等环境下Apache的10倍。

  Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中包括新浪博客、新浪播客、网易新闻、腾讯网、搜狐博客等门户网站频道,六间房、56.com等视频分享网站,Discuz!官方论坛、水木社区等知名论坛,盛大在线、金山逍遥网等网络游戏网站,豆瓣、人人网、YUPOO相册、金山爱词霸、迅雷在线等新兴Web 2.0网站。

   为什么Nginx的性能要比Apache高得多?这得益于Nginx使用了最新的epoll(Linux 2.6内核)和kqueue(freebsd)网络I/O模型,而Apache则使用的是传统的select模型。目前Linux下能够承受高并发访问的 Squid、Memcached都采用的是epoll网络I/O模型。
   处理大量的连接的读写,Apache所采用的select网络I/O模型非常低效。下面用一个比喻来解析Apache采用的select模型和Nginx采用的epoll模型进行之间的区别:
   假设你在大学读书,住的宿舍楼有很多间房间,你的朋友要来找你。select版宿管大妈就会带着你的朋友挨个房间去找,直到找到你为止。而epoll版 宿管大妈会先记下每位同学的房间号,你的朋友来时,只需告诉你的朋友你住在哪个房间即可,不用亲自带着你的朋友满大楼找人。如果来了10000个人,都要 找自己住这栋楼的同学时,select版和epoll版宿管大妈,谁的效率更高,不言自明。同理,在高并发服务器中,轮询I/O是最耗时间的操作之 一,select和epoll的性能谁的性能更高,同样十分明了。

二、LNMP搭建步奏

  1. php编译安装

cd /usr/local/src/

wget http://mirrors.sohu.com/php/php-5.4.37.tar.bz2

tar -jxvf php-5.4.37.tar.bz2 

ls /usr/local/php/

删除之前的php:rm -rf /usr/local/php/

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc  --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif --disable-ipv6 --enable-ftp

make

make install

配置文件:cp php.ini-production /usr/local/php/etc/php.ini

启动脚本:cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod 755 /etc/init.d/php-fpm 

加入到系统服务列表:chkconfig --add php-fpm

开机启动:chkconfig php-fpm on

cd /usr/local/php/etc

重命名

mv php-fpm.conf.default  php-fpm.conf

查看是否有错 /usr/local/php/sbin/php-fpm  -t

useradd -s /sbin/nologin php-fpm

service php-fpm start

2.nginx编译安装

cd /usr/local/src/

wget http://mirrors.sohu.com/nginx/nginx-1.6.2.tar.gz

tar -zxvf nginx-1.6.2.tar.gz 

cd nginx-1.6.2

./configure --prefix=/usr/local/nginx --with-pcre

make

make install

cd /usr/local/nginx/

启动nginx:/usr/local/nginx/sbin/nginx

3.测试php解析

nginx配置文件:vim /usr/local/nginx/conf/nginx.conf

 打开(72行)location ~ \.php$ {

            root           html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;

            include        fastcgi_params;

        }

cat /usr//local/nginx/html/index.html 

cd /usr/local/nginx/html

vi info.php

 <?php

  phpinfo();

 ?>

重新加载:/usr/local/nginx/sbin/nginx -s reload

4.nginx启动脚本和配置文件

vim /usr/local/nginx/conf/nginx.conf

 清空里面的内容加入下面的内容:


启动脚本

# vi /etc/init.d/nginx 写入

#!/bin/bash

# chkconfig: - 30 21

# description: http service.

# Source Function Library

. /etc/init.d/functions


# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"

NGINX_CONF="/usr/local/nginx/conf/nginx.conf"

NGINX_PID="/usr/local/nginx/var/nginx.pid"


RETVAL=0

prog="Nginx"


start() {

        echo -n $"Starting $prog: "

        mkdir -p /dev/shm/nginx_temp

        daemon $NGINX_SBIN -c $NGINX_CONF

        RETVAL=$?

        echo

        return $RETVAL

}


stop() {

        echo -n $"Stopping $prog: "

        killproc -p $NGINX_PID $NGINX_SBIN -TERM

        rm -rf /dev/shm/nginx_temp

        RETVAL=$?

        echo

        return $RETVAL

}


reload(){

        echo -n $"Reloading $prog: "

        killproc -p $NGINX_PID $NGINX_SBIN -HUP

        RETVAL=$?

        echo

        return $RETVAL

}


restart(){

        stop

        start

}


configtest(){

    $NGINX_SBIN -c $NGINX_CONF -t

    return 0

}


case "$1" in

  start)

        start

        ;;

  stop)

        stop

        ;;

  reload)

        reload

        ;;

  restart)

        restart

        ;;

  configtest)

        configtest

        ;;

  *)

        echo $"Usage: $0 {start|stop|reload|restart|configtest}"

        RETVAL=1

esac


exit $RETVAL


# chmod 755 /etc/init.d/nginx


配置文件

user nobody nobody; 

worker_processes 2; 

error_log /usr/local/nginx/logs/nginx_error.log crit; 

pid /usr/local/nginx/logs/nginx.pid; 

worker_rlimit_nofile 51200; 

events 

    use epoll; 

    worker_connections 6000; 

http 

 

    include mime.types; 

    default_type application/octet-stream; 

    server_names_hash_bucket_size 3526; 

    server_names_hash_max_size 4096; 

    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' 

    '$host "$request_uri" $status' 

    '"$http_referer" "$http_user_agent"'; 

    sendfile on; 

    tcp_nopush on; 

    keepalive_timeout 30; 

    client_header_timeout 3m; 

    client_body_timeout 3m; 

    send_timeout 3m; 

    connection_pool_size 256; 

    client_header_buffer_size 1k; 

    large_client_header_buffers 8 4k; 

    request_pool_size 4k; 

    output_buffers 4 32k; 

    postpone_output 1460; 

    client_max_body_size 10m; 

    client_body_buffer_size 256k; 

    client_body_temp_path /usr/local/nginx/client_body_temp; 

    proxy_temp_path /usr/local/nginx/proxy_temp; 

    fastcgi_temp_path /usr/local/nginx/fastcgi_temp; 

    fastcgi_intercept_errors on; 

    tcp_nodelay on; 

    gzip on; 

    gzip_min_length 1k; 

    gzip_buffers 4 8k; 

    gzip_comp_level 5; 

    gzip_http_version 1.1; 

    gzip_types text/plain application/x-javascript text/css text/htm application/xml; 

    include vhosts/*.conf;  

 

}

cd /usr/local/nginx/conf/

mkdir vhosts

cd vhosts/

vim defualt.conf

   server{

   listen 80 default_server;

    server_name localhost;

    index index.html index.htm index.php;

    root /tmp/1233;

    deny all;

}

 mkdir /tmp/1233

 /usr/local/nginx/sbin/nginx -t                                                                  

/etc/init.d/nginx reload

 curl -x127.0.0.1:80 www.baidu.com(403错误)

vim 111.conf

  server


  {

      listen 80;

      server_name 111.com;

      index index.html index.htm index.php;

      root /data/www;

 

      location ~ \.php$ {

          include fastcgi_params;

          #fastcgi_pass unix:/tmp/php-fcgi.sock;

          fastcgi_pass 127.0.0.1:9000;

          fastcgi_index index.php;

          fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

      }

  }

/usr/local/nginx/sbin/nginx -t

 /etc/init.d/nginx reload

curl -x127.0.0.1:80  111.com/forum.php -I

5.php-fpm配置文件

vim /usr/local/php/etc/php-fpm.conf

(清空内容)> /usr/local/php/etc/php-fpm.conf

   [global] 

   pid = /usr/local/php/var/run/php-fpm.pid

   error_log = /usr/local/php/var/log/php-fpm.log 

   [www] 

   listen = www.sock 

   user = php-fpm 

   group = php-fpm

   listen.owner = nobody  //和后面的nginx的一致

   listen.group = nobody // 同上

   pm = dynamic 

   pm.max_children = 50 

   pm.start_servers = 20 

   pm.min_spare_servers = 5 

   pm.max_spare_servers = 35 

   pm.max_requests = 500 

   rlimit_files = 1024 

   slowlog = /tmp/www_slow.log

   request_slowlog_timeout = 1

   php_admin_value[open_basedir] =/data/www:/tmp/


/usr/local/php/sbin/php-fpm -t

/etc/init.d/php-fpm restart

 ps aux |grep php-fpm