CentOS6.7安装部署Nginx(详解主配置文件)

keepalived + nginx   :实现高可用

nginx:

web服务器

反向代理,也支持缓存(缓存在磁盘上)

支持FastCGI

tengine:淘宝官方在nginx原有的代码的基础上对nginx做了诸多改进,直接将很多第三方模块整合进了nginx,并作出了大量改进。

 

优点

Nginx保持10 000个没有活动的连接,而这些连接只占用2.5MB内存

 

 

主进程主要完成如下工作:

1. 读取并验正配置信息;

2. 创建、绑定及关闭套接字;

3. 启动、终止及维护worker进程的个数;

4. 无须中止服务而重新配置工作特性;

5. 控制非中断式程序升级,启用新的二进制程序并在需要时回滚至老版本;

6. 重新打开日志文件,实现日志滚动;

7. 编译嵌入式perl脚本;

 

worker进程主要完成的任务包括:

1. 接收、传入并处理来自客户端的连接;

2. 提供反向代理及过滤功能;

3. nginx任何能完成的其它任务;

 

cache loader进程主要完成的任务包括:

1. 检查缓存存储中的缓存对象;

2. 使用缓存元数据建立内存数据库;

 

cache manager进程的主要任务:

1. 缓存的失效及过期检验;

 

编译安装nginx

1、编译需要的环境

     yum -y install  gcc  gcc-c++  autoconf  automake  zlib  zlib-devel  openssl  openssl-devel  pcre-devel  gd-devel

2、创建程序用户  groupadd  -r -g 311  nginx      &&  useradd  -r -g 311 -r -u 306 nginx

2.1、下载nginx包,与解压省略,

3、编译需要的选项

./configure \

  --prefix=/usr/local/nginx\

  --sbin-path=/usr/local/nginx/sbin/nginx \

  --conf-path=/usr/local/nginx/etc/nginx.conf \

  --error-log-path=/usr/local/nginx/log/error.log \

  --http-log-path=/usr/local/nginx/log/access.log \

  --pid-path=/var/run/nginx/nginx.pid  \

  --lock-path=/var/lock/nginx.lock \

  --user=nginx \

  --group=nginx \

  --with-http_ssl_module \

  --with-http_flv_module \

  --with-http_stub_status_module \

  --with-http_gzip_static_module \

  --http-client-body-temp-path=/usr/local/nginx/client/ \

  --http-proxy-temp-path=/usr/local/nginx/proxy/ \

  --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ \

  --http-uwsgi-temp-path=/usr/local/nginx/uwsgi \

  --http-scgi-temp-path=/usr/local/nginx/scgi \

  --with-file-aio\

  --with-http_image_filter_module\

 --add-module=/root/fastdfs-nginx-module/src\

 --with-pcre=/usr/pcre-版本号\    指向的是源目录,不是安装目录

4、需要一个启动脚本 :vim  /etc/rc.d/init.d/nginx    脚本在下面↓

   然后添加执行权限  chmod  x   /etc/rc.d/init.d/nginx

   开机启动   chkconfig --add nginx     chkconfig --level 35  nginx on

5、启动服务  service  nginx   start

 

 

定义LNMP

nginx想要整合php不支持模块化方式安装,,必须把php编译成FastCGI模式!php-fpm

编译php最后一项

编辑php-fpm的配置文件:

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

配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):

pm.max_children = 150   最大子进程个数

pm.start_servers = 8     刚开始启动子进程个数

pm.min_spare_servers = 5  最小空闲数

pm.max_spare_servers = 10  最大空闲数

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

 

接下来就可以启动php-fpm了:

# service php-fpm start

 

使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):

# ps aux | grep php-fpm

整合php与nginx

1、编辑/etc/nginx/nginx.conf,启用如下选项:

location ~ \.php$ {

            root           html;   这个定义的是你php网页文件的位置

            fastcgi_pass   127.0.0.1:9000;  反向代理方式

            fastcgi_index  index.php;  

            fastcgi_param  SCRIPT_FILENAME  /script$fastcgi_script_name;

            include        fastcgi_params;

        }

 

2、编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容:

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格式的主页,类似如下:

location / {

            root   html;

            index  index.php index.html index.htm;

        }

       

而后重新载入nginx的配置文件:

# service nginx reload

 

3、在/usr/html新建index.php的测试页面,测试php是否能正常工作:

# cat > /usr/html/index.php << EOF

<?php

phpinfo();

?>

接着就可以通过浏览器访问此测试页面了。

 

 

 

nginx主配置文件

server{ }:虚拟主机,每一个server定义一个虚拟主机。

location{ }:基于URI路径来定义访问属性的。

            location / {      :定义一个URL路径

            root   html;      :定义了URL路径下的所有网页文件在本地系统的那个地方

            index  index.html index.htm;    :返回默认主页面是谁的

        }

           error_page   500 502 503 504  /50x.html;   若果你返回的错误代码是50....这几个,就读取/50x这个网页文件。

        location = /50x.html {      :如果你访问的就是这个页面

            root   html;       :要求这个页面位于。。。路径下

        }

location URI :对当前路径及子路径下的所有对象都生效,优先级最低

location = URI :精确匹配指定路径,不包括子路径,只对当前资源生效,优先级最高

location ~ URI :实现模式匹配URI,此处的URI可以使用正则表达式,区分字符大小写,优先级三

location~*URI :实现模式匹配URI,此处的URI可以使用正则表达式,不区分字符大小写,优先级三

location^~URI:明确说明URI不使用正则表达式,优先级第二

 

用户访问法则:

location  /  {

deny  192.168.1.1

allow  192.13.10.0

deny  all

alllow  all

}

可以自己定义法则,也可以插入到别的网页定义location中!

 

配置虚拟主机(一个server模块定义一个虚拟机,两个虚拟机需要添加两个server模块)

http {  

server {

        listen       80;

        server_name    www.a.org;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   /www/a.org;

            index  index.html index.htm;

        }

 

五、安装xcache,为php加速:

 

1、安装

# tar xf xcache-2.0.0.tar.gz

# cd xcache-2.0.0

# /usr/local/php/bin/phpize

# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config

# make && make install

 

安装结束时,会出现类似如下行:

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20100525/

 

2、编辑php.ini,整合phpxcache

 

首先将xcache提供的样例配置导入php.ini

# mkdir /etc/php.d

# cp xcache.ini /etc/php.d

 

说明:xcache.ini文件在xcache的源码目录中。

 

接下来编辑/etc/php.d/xcache.ini,找到extension开头的行,修改为如下行:

extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so

 

注意:如果php.ini文件中有多条extension指令行,要确保此新增的行排在第一位。

 

3、重新启动php-fpm

# service php-fpm restart

 

 

六、补充说明

 

如果要在SSL中使用php,需要在phplocation中添加此选项:

 

fastcgi_param HTTPS on;

 

 

 

nginx启动脚本

#!/bin/sh

#

# nginx - this script starts and stops the nginx daemon

#

# chkconfig:   - 85 15

# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \

#               proxy and IMAP/POP3 proxy server

# processname: nginx

# config:      /etc/nginx/nginx.conf

# config:      /etc/sysconfig/nginx

# pidfile:     /var/run/nginx.pid

 

# Source function library.

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

 

# Source networking configuration.

. /etc/sysconfig/network

 

# Check that networking is up.

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

 

nginx="/usr/sbin/nginx"

prog=$(basename $nginx)

 

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

 

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

 

lockfile=/var/lock/subsys/nginx

 

make_dirs() {

   # make required directories

   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`

   options=`$nginx -V 2>&1 | grep 'configure arguments:'`

   for opt in $options; do

       if [ `echo $opt | grep '.*-temp-path'` ]; then

           value=`echo $opt | cut -d "=" -f 2`

           if [ ! -d "$value" ]; then

               # echo "creating" $value

               mkdir -p $value && chown -R $user $value

           fi

       fi

   done

}

 

start() {

    [ -x $nginx ] || exit 5

    [ -f $NGINX_CONF_FILE ] || exit 6

    make_dirs

    echo -n $"Starting $prog: "

    daemon $nginx -c $NGINX_CONF_FILE

    retval=$?

    echo

    [ $retval -eq 0 ] && touch $lockfile

    return $retval

}

 

stop() {

    echo -n $"Stopping $prog: "

    killproc $prog -QUIT

    retval=$?

    echo

    [ $retval -eq 0 ] && rm -f $lockfile

    return $retval

}

 

restart() {

    configtest || return $?

    stop

    sleep 1

    start

}

 

reload() {

    configtest || return $?

    echo -n $"Reloading $prog: "

    killproc $nginx -HUP

    RETVAL=$?

    echo

}

 

force_reload() {

    restart

}

 

configtest() {

  $nginx -t -c $NGINX_CONF_FILE

}

 

rh_status() {

    status $prog

}

 

rh_status_q() {

    rh_status >/dev/null 2>&1

}

 

case "$1" in

    start)

        rh_status_q && exit 0

        $1

        ;;

    stop)

        rh_status_q || exit 0

        $1

        ;;

    restart|configtest)

        $1

        ;;

    reload)

        rh_status_q || exit 7

        $1

        ;;

    force-reload)

        force_reload

        ;;

    status)

        rh_status

        ;;

    condrestart|try-restart)

        rh_status_q || exit 0

            ;;

    *)

        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

        exit 2

esac

 

而后为此脚本赋予执行权限:

# chmod +x /etc/rc.d/init.d/nginx

 

 

 

主配置文件详解:

#user  nobody;

worker_processes  1;    如果负载以CPU密集型应用为主,如SSL或压缩应用,则worker数应与CPU数相同;如果负载以IO密集型为主,如响应大量内容给客户端,则worker数应该为CPU个数的1.52倍。每一个核心可以绑定一个线程!

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;  定义PID文件,这里禁用是我们自定义的选项生效了

 

events {

    worker_connections  1024; 事件驱动中,每一个work他所能支持的连接数,一个work支持1021个连接数!

}

 

 

http {

    include       mime.types; 包含的文件

    default_type  application/octet-stream; 默认支持的类型

 

    #log_format  main  '$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  main; 访问日志

    sendfile        on; 提升文件下载性能

    #tcp_nopush     on; 不做推送。先发送一个包,其余包缓存,等第一个包确认之后,在发送其余的包

 

    #keepalive_timeout  0; 使用长连接

    keepalive_timeout  65; 指定超时时间

 

    #gzip  on;先压缩后发送,带宽小,访问量大适用

 

    server {

        Listen       80;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

 

        location  /  {    URI路径

            root   html;   相对于URI,你的网页文件存放路径

            index  index.html index.htm;    主页面支持的文件类型

        }

 

        #error_page  404              /404.html;返回的错误代码是404,那就去读取下面50X这个文件

 

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {  

            root   html;

        }

 

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #    proxy_pass   http://127.0.0.1;

        #}

 

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        #location ~ \.php$ {

        #    root           html;

        #    fastcgi_pass   127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        #}

 

        # deny access to .htaccess files, if Apache's document root

        # concurs with nginx's one

        #

        #location ~ /\.ht {

        #    deny  all;

        #}

    }

# another virtual host using mix of IP-, name-, and port-based configuration

    #

    #server {

    #    listen       8000;

    #    listen       somename:8080;

    #    server_name  somename  alias  another.alias;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

 

 

    # HTTPS server

    #

    #server {

    #    listen       443 ssl;

    #    server_name  localhost;

 

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

 

    #    ssl_session_cache    shared:SSL:1m;

    #    ssl_session_timeout  5m;

 

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers  on;

dex  index.html index.htm;

    #    }

    #}

猜你喜欢

转载自www.cnblogs.com/linux-s/p/11288878.html
今日推荐