1. 程式人生 > >CentOS6.5安裝Nginx (包含日誌記錄功能)

CentOS6.5安裝Nginx (包含日誌記錄功能)

Nginx在CentOS6.5作業系統的安裝文件.

Author: [email protected]

Date:December 02 2017

相關的安裝包和安裝文件可在https://download.csdn.net/download/cnzyyh/10424275 下載!

1. 安裝依賴

所有依賴在rpm-for-nginx-centos6.5.tar壓縮檔案裡面能找到,安裝前請先解壓.

僅適用於CentOS6.5平臺

請注意安裝命令的 -ivh -Uvh

[[email protected] rpm-for-nginx-centos6.5]# rpm -Uvh tzdata-2017c-1.el6.noarch.rpm
[[email protected] rpm-for-nginx-centos6.5]# rpm -ivh ppl-0.10.2-11.el6.x86_64.rpm
[[email protected] rpm-for-nginx-centos6.5]# rpm -ivh cloog-ppl-0.15.7-1.2.el6.x86_64.rpm
[[email protected] rpm-for-nginx-centos6.5]# rpm -ivh mpfr-2.4.1-6.el6.x86_64.rpm
[[email protected] rpm-for-nginx-centos6.5]# rpm -ivh cpp-4.4.7-18.el6.x86_64.rpm
[[email protected] rpm-for-nginx-centos6.5]# rpm -Uvh glibc-common-2.12-1.209.el6_9.2.x86_64.rpm  glibc-2.12-1.209.el6_9.2.x86_64.rpm
[[email protected] rpm-for-nginx-centos6.5]# rpm -ivh kernel-headers-2.6.32-696.16.1.el6.x86_64.rpm
[[email protected] rpm-for-nginx-centos6.5]# rpm -ivh glibc-headers-2.12-1.209.el6_9.2.x86_64.rpm
[[email protected] rpm-for-nginx-centos6.5]# rpm -ivh glibc-devel-2.12-1.209.el6_9.2.x86_64.rpm
[[email protected] rpm-for-nginx-centos6.5]# rpm -ivh libgomp-4.4.7-18.el6.x86_64.rpm
[[email protected] rpm-for-nginx-centos6.5]# rpm -Uvh libgcc-4.4.7-18.el6.x86_64.rpm
[[email protected] rpm-for-nginx-centos6.5]# rpm -Uvh libstdc++-devel-4.4.7-18.el6.x86_64.rpm libstdc++-4.4.7-18.el6.x86_64.rpm
[[email protected] rpm-for-nginx-centos6.5]# rpm -Uvh gcc-*
[[email protected] rpm-for-nginx-centos6.5]# rpm -Uvh pcre-7.8-7.el6.x86_64.rpm
[[email protected] rpm-for-nginx-centos6.5]# rpm -ivh pcre-devel-7.8-7.el6.x86_64.rpm
[[email protected] rpm-for-nginx-centos6.5]# rpm -ivh zlib-devel-1.2.3-29.el6.x86_64.rpm

2. 安裝luajit

[[email protected] luajit]# wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz

[[email protected] luajit]# tar -xvf LuaJIT-2.0.4.tar.gz

[[email protected] luajit] cd LuaJIT-2.0.4

[[email protected] LuaJIT-2.0.4]# make install PREFIX=/usr/local/luajit

安裝成功的相關提示

==== Successfully built LuaJIT 2.0.4 ==+
......
==== Successfully installed LuaJIT 2.0.4 to /usr/local/luajit ====

新增系統環境變數

[[email protected] LuaJIT-2.0.4]# vim  /etc/profile

# export for luajit
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

[[email protected] LuaJIT-2.0.4]# source /etc/profile

3. 模組ngx_devel_kit

如果不能連線外網請使用Nginx-CentOS6.5資料夾裡面為ngx_devel_kit-0.3.0.tar.gz的壓縮包,直接解壓

[[email protected] ngx-devel-kit]# wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz

[[email protected] ngx-devel-kit]# tar -xzvf v0.3.0.tar.gz

4. 模組lua-nginx-module

如果不能連線外網請使用Nginx-CentOS6.5資料夾裡面為lua-nginx-module-0.10.8.tar的壓縮包,直接解壓

[[email protected] ngx-devel-kit]# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.8.tar.gz

[[email protected] ngx-devel-kit]# tar -xzvf v0.10.8.tar.gz

5. 安裝Nginx

如果不能連線外網請使用Nginx-CentOS6.5資料夾裡面為nginx-1.10.3.tar.gz的壓縮包,直接解壓

[[email protected] nginx]# wget http://nginx.org/download/nginx-1.10.3.tar.gz

[[email protected] nginx]# tar -xvf nginx-1.10.3.tar.gz

[[email protected] nginx]# cd nginx-1.10.3/

修改Nginx原始碼,解決log日誌中文亂碼問題 修改檔案:nginx-1.10.3/src/http/modules/ngx_http_log_module.c

static uintptr_t
ngx_http_log_escape(u_char *dst, u_char *src, size_t size)
{
    ngx_uint_t      n;
    /* 修改這裡 */
    /*static u_char   hex[] = "0123456789ABCDEF";*/

    static uint32_t   escape[] = {
        0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */

                    /* ?>=< ;:98 7654 3210  /.-, +*)( '&%$ #"!  */
        0x00000004, /* 0000 0000 0000 0000  0000 0000 0000 0100 */

                    /* _^]\ [ZYX WVUT SRQP  ONML KJIH GFED [email protected] */
        0x10000000, /* 0001 0000 0000 0000  0000 0000 0000 0000 */

                    /*  ~}| {zyx wvut srqp  onml kjih gfed cba` */
        0x80000000, /* 1000 0000 0000 0000  0000 0000 0000 0000 */

        0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
        0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
        0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
        0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
    };

    if (dst == NULL) {
        /* find the number of the characters to be escaped */
        n = 0;
        while (size) {
            if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
                n++;
            }
            src++;
            size--;
        }
        return (uintptr_t) n;
    }

    /* 修改這裡 */
    while (size) {
       /* if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
            *dst++ = '\\';
            *dst++ = 'x';
            *dst++ = hex[*src >> 4];
            *dst++ = hex[*src & 0xf];
            src++;
        } else {
            *dst++ = *src++;
        }*/
	    *dst++=*src++;
      size--;
    }
    return (uintptr_t) dst;
}

安裝Nginx

  • --prefix 指定nginx安裝路徑
  • --add-module 新增模組,以ngx_devel_kit-0.3.0.tar.gzlua-nginx-module-0.10.8.tar.gz實際解壓路徑為準
[[email protected] nginx-1.10.3]# ./configure --prefix=/usr/local/nginx --add-module=/home/dev/nginx/ngx-devel-kit/ngx_devel_kit-0.3.0 --add-module=/home/dev/nginx/lua-nginx-module/lua-nginx-module-0.10.8

[[email protected] nginx-1.10.3]# make -j2

[[email protected] nginx-1.10.3]# make install

使用

編輯conf/nginx.conf

#user  nobody;
worker_processes  1;
error_log  logs/error.log;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format main '"$request_body"'; --> 主要

    sendfile        on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    lua_need_request_body on; --> 主要
    server {
        listen       80;
        server_name  10.10.2.101;
	    charset utf-8; --> 設定編碼

        location / {
            root   html;
            index  index.html index.htm;
        }

    	location /hello { -->主要
    		default_type 'text/plain';
    		content_by_lua '
    			ngx.say("success")
    			ngx.req.read_body()
    		';
    		access_log logs/access.log main;
    	}
}

使用nginx -t檢查配置檔案是否正確

出現異常:sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

解決方法

[[email protected] nginx]# ln -s  /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

啟動Nginx

[[email protected] nginx]#sbin/nginx

瀏覽器訪問

visit

Nginx常用命令

停止Nginx: nginx -s stop

檢查Nginx配置: nginx -t

重新載入Nginx: nginx -s reload

如何不行可以嘗試解除安裝pcre rpm依賴包

檢視rpm已經安裝了什麼包? rpm -qa | grep pcre *

rpm如何解除安裝包? rpm -e gg