CentOS6.5安装Nginx (包含日志记录功能)

Nginx在CentOS6.5操作系统的安装文档.

Author: cs12110@163.com

Date:December 02 2017


相关的安装包和安装文档可在https://download.csdn.net/download/cnzyyh/10424275 下载!

1. 安装依赖


所有依赖在rpm-for-nginx-centos6.5.tar压缩文件里面能找到,安装前请先解压.

仅适用于CentOS6.5平台

请注意安装命令的 -ivh -Uvh

[root@centos6 rpm-for-nginx-centos6.5]# rpm -Uvh tzdata-2017c-1.el6.noarch.rpm
[root@centos6 rpm-for-nginx-centos6.5]# rpm -ivh ppl-0.10.2-11.el6.x86_64.rpm
[root@centos6 rpm-for-nginx-centos6.5]# rpm -ivh cloog-ppl-0.15.7-1.2.el6.x86_64.rpm
[root@centos6 rpm-for-nginx-centos6.5]# rpm -ivh mpfr-2.4.1-6.el6.x86_64.rpm
[root@centos6 rpm-for-nginx-centos6.5]# rpm -ivh cpp-4.4.7-18.el6.x86_64.rpm
[root@centos6 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
[root@centos6 rpm-for-nginx-centos6.5]# rpm -ivh kernel-headers-2.6.32-696.16.1.el6.x86_64.rpm
[root@centos6 rpm-for-nginx-centos6.5]# rpm -ivh glibc-headers-2.12-1.209.el6_9.2.x86_64.rpm
[root@centos6 rpm-for-nginx-centos6.5]# rpm -ivh glibc-devel-2.12-1.209.el6_9.2.x86_64.rpm
[root@centos6 rpm-for-nginx-centos6.5]# rpm -ivh libgomp-4.4.7-18.el6.x86_64.rpm
[root@centos6 rpm-for-nginx-centos6.5]# rpm -Uvh libgcc-4.4.7-18.el6.x86_64.rpm
[root@centos6 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
[root@centos6 rpm-for-nginx-centos6.5]# rpm -Uvh gcc-*
[root@centos6 rpm-for-nginx-centos6.5]# rpm -Uvh pcre-7.8-7.el6.x86_64.rpm
[root@centos6 rpm-for-nginx-centos6.5]# rpm -ivh pcre-devel-7.8-7.el6.x86_64.rpm
[root@centos6 rpm-for-nginx-centos6.5]# rpm -ivh zlib-devel-1.2.3-29.el6.x86_64.rpm

2. 安装luajit


[root@dev2 luajit]# wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz

[root@dev2 luajit]# tar -xvf LuaJIT-2.0.4.tar.gz

[root@dev2 luajit] cd LuaJIT-2.0.4

[root@dev2 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 ====

添加系统环境变量

[root@dev2 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

[root@dev2 LuaJIT-2.0.4]# source /etc/profile

3. 模块ngx_devel_kit


如果不能连接外网请使用Nginx-CentOS6.5文件夹里面为ngx_devel_kit-0.3.0.tar.gz的压缩包,直接解压

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

[root@dev2 ngx-devel-kit]# tar -xzvf v0.3.0.tar.gz

4. 模块lua-nginx-module


如果不能连接外网请使用Nginx-CentOS6.5文件夹里面为lua-nginx-module-0.10.8.tar的压缩包,直接解压

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

[root@dev2 ngx-devel-kit]# tar -xzvf v0.10.8.tar.gz

5. 安装Nginx


如果不能连接外网请使用Nginx-CentOS6.5文件夹里面为nginx-1.10.3.tar.gz的压缩包,直接解压

[root@dev2 nginx]# wget http://nginx.org/download/nginx-1.10.3.tar.gz

[root@dev2 nginx]# tar -xvf nginx-1.10.3.tar.gz

[root@dev2 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 CBA@ */
        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实际解压路径为准
[root@dev2 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

[root@dev2 nginx-1.10.3]# make -j2

[root@dev2 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

解决方法

[root@dev2 nginx]# ln -s  /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

启动Nginx

[root@dev2 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值