CentOS6.7 installation deployment Nginx (detailed primary configuration file)

keepalived + nginx : Realize high availability

nginx:

web server

reverse proxy, also supports caching(Cached on disk)

Support FastCGI

tengine: Taobao official corrects based on the original code of nginxnginx made many improvements and directly integrated many third-party modules intonginx and made a lot of improvements.

Advantages

Nginx keeps10 000one There are no active connections, and these connections only take up2.5MBmemory< /p>

The main process is mainly Complete the following tasks:

1. Read and verify the configuration information;

< p style="margin:0in;font-family:宋体;font-size:10.5pt">2. Create, bind and close sockets;

3. Startup, termination and maintenanceworkerthe number of processes;

4. Reconfiguration of work features without stopping the service;

5. Control non-interrupted Program upgrade, enable the new binary program and roll back to the old version when needed;

6. Reopen the log File, realize log scrolling;

7. Compile embeddedperlscript;

The main tasks completed by the worker process include:

1. Receiving, incoming and Handling connections from the client;

2. Provide reverse proxy and filtering functions;

3. Any other tasks that nginx can complete;

cache loader process The main tasks completed include:

1. Check the cache objects in the cache storage;

2. Use cache metadata to build an in-memory database;

The main task of the cache manager process:

1. Cache invalidation and expiration Inspection;

Compile and install nginx

1. The environment needed for compilation

< span style="font-family:Calibri">yum -y install gcc gcc-c++ autoconf a utomake zlib zlib-devel openssl openssl-devel pcre-devel gd-devel

2, create program user groupadd -r -g 3< span style="font-family:宋体">11 nginx && useradd -r -g 311 -r -u 306 nginx

2.1, downloadnginx< /span>Package, omitted from decompression,

3. Options needed for compilation

./configure \

–prefix=/usr/local/nginx\

–s bin-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-version number\ < span style="font-family:宋体"> refers to the source directory, not the installation directory

4. A startup script is required:vim /etc/rc.d/init.d/nginx The script is below↓

Then Add execution permissions chmod x /etc/rc.d/init.d/nginx

Start up chkconfig –add nginx chkconfig –level 35 nginx on

5 , Start the service service nginx start

Define LNMP

nginx thinks To integratephp does not support modular installation, you must install< span style="font-family:calibri">phpCompile toFastCGImode! php-fpm

Compile the last item of php

Edit the configuration file of php-fpm:

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

Configure the relevant options of fpm to the values ​​you need, and Enablepidfile (the last line below):

< p style="margin:0in;font-size:10.5pt">pm.max_children = 150 Maximum Number of child processes

pm.start_servers = 8 The number of child processes started at the beginning

pm.min_spare_servers = 5 Minimum idle number

pm.max_spare_servers = 10 Maximum free number

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

< p style="margin:0in;font-family:宋体;font-size:10.5pt">Now you can start php-fpm:

# service php-fpm start

Use the following command to verify (if there are several php-fpm processes in the output of this command, it means the startup is successful):

< p style="margin:0in;font-family:Calibri;font-size:10.5pt"># ps aux | grep php-fpm

Integrate php andnginx

1, edit/etc/nginx/nginx.conf, enable the following options:

location ~ \.php$ {

root root html; This defines the location of your php web page file< /p>

fastcgi_pass 127.0.0.1:9000; Reverse proxy method

fastcgi_index index.php ;

fastcgi_param SCRIPT_FILENAME /script$fastcgi_script_name;

include fastcgi_params;

}

2, edit/etc/nginx/fastcgi_params, change its content to the following:

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;

< p style="margin:0in;font-family:Calibri;font-size:10.5pt">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;< /p>

fastcgi_param SERVER_NAME $server_name;

and add a php format homepage to the supported main page formats, Similar to the following:

location / {

root html;

index index.php index.html index.htm ;

}

Then reload the nginx configuration file:

# service nginx reload

3. In/usr/htmlNewindex.php’s test page, testphp< /span>Whether it works normally:

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

< p style="margin:0in;font-family:Calibri;font-size:10.5pt">phpinfo();

?>

Then you can access this test page through a browser.

nginx main configuration file

server{ }: virtual host, eachserverDefine a virtual host.

location{ }: Based onURIpath to define access properties.

location / {: Define a URL path

root root html;: defines the place where all webpage files under the URL path are in the local system< /span>

index index.html index.htm;: Return to who the default main page belongs to

}

error_page 500 502 503 504 /50x.html; If the error code you return is 50… these ones, just read them< span style="font-family:calibri">/50xThis web page file.

location = /50x.html {: If you visit this page

root root html;: require this page to be located. . . Under the path

}

location URI: It takes effect for all objects under the current path and sub-paths, Lowest priority

location = URI: Exactly match the specified path, excluding sub-paths, only valid for the current resource, the highest priority span>

location ~ URI< /span>: Implement pattern matching URI, hereURIRegular expressions can be used to distinguish between uppercase and lowercase characters,Priority three

location~*URI: Implement pattern matching URI, hereURIYou can use regular expressions, without distinguishing between uppercase and lowercase characters,Priority three

location^~URI: Make it clear that URIs do not use regular expressions, the second priority

< p style="margin:0in;font-family:Calibri;font-size:10.5pt">

User access rules:

location / {

deny 192.168.1.1

allow 192.13.10.0

deny all

alllow all

}

You can define the rules yourself, or insert it into other webpage definition locations!

Configure virtual host (a server module defines one virtual machine, two virtual machines need to add twoservermodule)

http {

server {< /p>

listen 80;

server_name www.a.org;

#charset koi8-r;

#access_log logs/host.acc ess.log main;

location / {

root /www/a.org;

index index.html index.htm;

}

5. Install xcache , ForphpAcceleration:

1, installation

p>

# 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

At the end of the installation, a line similar to the following will appear:

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

2, editphp.ini< /span>, integrationphpandxcache:

First import the sample configuration provided by xcachephp.ini

# mkdir /etc/php.d

# cp xcache.ini /etc/php.d

Description: The xcache.ini file is inxcache in the source directory.

Next edit /etc/php.d/xcache.ini and findextension The line at the beginning of is modified as follows:

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

Note: If there are multiple entries in the php.ini file< /span>extensioncommand line, make sure this new line is ranked first.

3. Restartphp-fpm

# service php-fpm restart

VI. Supplementary description

If you want to use it in SSLphp, need to be inphp< Add this option in span style="font-family:calibri">location:

fastcgi_param HTTPS on;

nginx startup script

#!/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

 

lockfil e=/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 $”Usag e: $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 {

        Li sten       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 ac cess 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;

    #    }

    #}

keepalived + nginx   :实现高可用

nginx:

web服务器

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

支持FastCGI

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

 

优点

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

 

 

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

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

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

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

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  op enssl  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

< span style="font-family:宋体">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_pa ram  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

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  m ain;

 

        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

< p style="margin:0in;font-family:Calibri;font-size:10.5pt">        ;;

    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:900 0

        #

        #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 docume nt 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_cip hers  on;

dex  index.html index.htm;

    #    }

    #}

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-de vel  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/< span style="font-family:Calibri">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       $conten t_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

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< /span>

 

首先将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他所能支持的连接数,一个wo rk支持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.ht ml {  

            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;

    #    }

    #}

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/sb in/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

 

 

< p style="margin:0in;font-size:14.0pt;color:red">定义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        fas tcgi_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的配置文件:< /p>

# service nginx reload

 

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

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

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 p roxy 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

< p style="margin:0in;font-family:Calibri;font-size:10.5pt">}

 

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

        # concu rs 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;

    #    }

    #}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4863 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.