Nginx安装配置(lua全模块、GEOIP、加入系统服务)---无坑安装

nginx安装一般两种:A下载安装包安装、B直接yum在线安装

A:在线安装是最简便的,如果只需基本的web和转发服务就别往下看,直接【yum install nginx】,少折腾,稳定才是王道。

B:下载安装包安装便以自定义参数、加载模块,但缺少系统服务支持。比如用systemctl start(stop\status) nginx命令,还有最常配置命令:nginx -t、nginx -s *需要对应安装目录下才能运行。以下介绍nginx下载全模块安装并加入系统服务。

GeoIP的用处:对企业应用还是很有必要,列三个内容(后期我再写这些案例应用,有兴趣可以加关注):

     1:方便对来源判断和筛选,分地域阻断攻击;

     2:方便制定安全策略,比如禁止或允许某国家或城市访问;

     3:需要做报表看版或展示屏的少不它,在报表上做个实时访问地图,逼格爆仓。

GeoIP介绍:GeoIP 是IP 地理位置数据库,可以根据IP 地址(支持IPv4 和IPv6), 定位该IP 所在的地理位置和ASN 等信息。 GeoIP 目前已经升级到GeoIP2,GeoIP2 有两个版本,一个免费版(GeoLite2),一个收费版本(GeoIP2, 200$ 起步)。

此例使用免费版(收费版本的准确率稍高一些,更新频率为每周二更新一次, 免费版是每月第一个周二更新一次)

1、nginx下载安装包及lua模块  (Nginx模块官方简介页面

1.1、官网下载地址:nginx: download   当前最新版:1.23.1(PS:右键复制获取下载地址)

 结果如下: 

#执行顺序:下载安装包=》解压到本目录=》准备geoip好模块后再安装
mkdir /etc/nginx && cd /etc/nginx
wget http://nginx.org/download/nginx-1.23.1.tar.gz  
tar -zxvf nginx*.tar.gz

#下载安装Lua模块及依赖(lua-nginx用当前版本,高版本不兼容会报错,被坑过)
wget -c 'https://github.com/openresty/lua-nginx-module/archive/v0.10.22.tar.gz' -O lua-nginx-module-0.10.22.tar.gz
wget -c 'https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz' -O ngx_devel_kit-0.3.1rc1.tar.gz
tar xzf lua-nginx-module-0.10.22.tar.gz
tar xzf ngx_devel_kit-0.3.1rc1.tar.gz

1.2、安装lua

lua安装
cd /etc/nginx
wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
tar -zxvf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5
make && make install
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0

 ps:需要修改lua配置,新版nginx编译配置会报错,后面编译部分有说明

 2、geoip下载安装配置

2.1、安装依赖 (包含nginx基本依赖)

#RHEL系统、Debian系统:
yum install -y wget perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel gd-devel GeoIP-devel autoconf-2.69-11.el7.noarch gcc libxml2 libxml2-dev openldap-devel python-devel gcc-c++  openssl-devel cmake pcre-devel nano wget  gcc gcc-c++ ncurses-devel perl pcre-devel libtool libsysfs automake openssl openssl-devel redhat-rpm-config.noarch unzip libmaxminddb libunwind.x86_64  -y
yum install -y libmaxminddb-devel  #安装此mmdblookup命令
yum install -y libunwind-devel.x86_64 systemd-devel libatomic_ops-devel  lua-devel
#提示“没有可用软件包 libxml2-dev”是正常,Debian系统用libxml2-dev,RHEL系统的是libxml2-devel

 2.2、下载区域、国家及城市数据
数据文件是.mmdb的文件,建议从官网下载(需注册-免费注册)

官网(点右上角可注册)  IP Geolocation and Online Fraud Prevention | MaxMind

官网下载页面 https://www.maxmind.com/en/accounts/758766/geoip/downloads

 

 如是测试用,可以这里下载2023-04-18更新数据:geoip私服下载 

(PS:私服网盘,ADSL线路,打不开请私信我)

2.3、下载IP数据及安装

wget https://pan.k8scn.work:15443/d/ChinaNetCould/datafiles/geoip-20221011/GeoLite2-ASN_20221011.tar.gz
wget https://pan.k8scn.work:15443/d/ChinaNetCould/datafiles/geoip-20221011/GeoLite2-City_20221011.tar.gz
wget https://pan.k8scn.work:15443/d/ChinaNetCould/datafiles/geoip-20221011/GeoLite2-Country_20221011.tar.gz

mkdir /etc/nginx && mkdir /etc/nginx/GeoIP2  #目录根据个人爱好自行修改

tar xvf GeoLite2-ASN*.tar.gz -C /etc/nginx/GeoIP2/
tar xvf GeoLite2-City*.tar.gz -C /etc/nginx/GeoIP2/
tar xvf GeoLite2-Country*.tar.gz -C /etc/nginx/GeoIP2/
mv /etc/nginx/GeoIP2/GeoLite2-City*/ /etc/nginx/GeoIP2/GeoLite2-City/ 
mv /etc/nginx/GeoIP2/GeoLite2-Country*/ /etc/nginx/GeoIP2/GeoLite2-Country/ 
mv /etc/nginx/GeoIP2/GeoLite2-ASN*/ /etc/nginx/GeoIP2/GeoLite2-ASN/ 

测试数据库对8.8.8.8IP判断,结果应在美国

mmdblookup --file /etc/nginx/GeoIP2/GeoLite2-City/GeoLite2-City.mmdb --ip 8.8.8.8

再来个阿里云的DNS(223.5.5.5)看看是在那里

  2.4、下载geoip2模块的代码
此代码参与nginx的编译,编译nginx需要 google-pertools进行辅助
安装google-pertools前需要安装libunwind,libuwinde已经在yum安装依赖时安装,make检查报

(注:模块保存路径/usr/local/src)

wget https://github.com/leev/ngx_http_geoip2_module/archive/master.zip
unzip master
mv ngx_http_geoip2_module-master  /usr/local/src/ngx_http_geoip2_module

安装google-pertools  #后期有新版本请自行到github下载,github网站国内经常访问不了,多试几次
wget -c -O gperftools-2.10.tar.gz  https://github.com/gperftools/gperftools/releases/download/gperftools-2.10/gperftools-2.10.tar.gz
tar xvf gperftools-2.10.tar.gz
mv gperftools-2.10 /usr/local/src/
cd /usr/local/src/gperftools-2.10
./configure
make check && make install  
#check过程报错【FAIL: stacktrace_unittest】是libunwind配置冲突可忽略,可编译加入参数--enable-frame-pointers解决

GeoIP安装配置完成

3、nginx安装

3.1 准备

useradd nginx   #指定用户调用
mkdir /var/lib/nginx && mkdir /var/lib/nginx/tmp

3.2 编译

cd /etc/nginx/nginx-1.13.1  #进入解压后程序包,执行以下

./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body \
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi \
--pid-path=/run/nginx.pid \
--lock-path=/run/lock/subsys/nginx \
--user=nginx \
--group=nginx \
--with-ipv6 \
--with-http_auth_request_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_sub_module \
--with-file-aio \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_perl_module=dynamic \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-pcre \
--with-pcre-jit \
--with-stream \
--with-stream_ssl_module \
--with-http_geoip_module=dynamic \
--add-module=/usr/local/src/ngx_http_geoip2_module \
--add-module=../ngx_devel_kit-0.3.1rc1 \
--add-module=../lua-nginx-module-0.10.13 \
--with-debug \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' \
--with-ld-opt='-Wl,-rpath,/path/to/luajit/lib,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

make -j2  #此处有坑,看下一步修改配置

3.3 make报错解决(只对当前版本安装遇到问题)

ps:安装过程很多坑,只要参数本文参数对应配置安装是完成可用的。

报错
lua-nginx-module-0.10.13/src/ngx_http_lua_headers_in.c:156:18: error: no member named 'cookies' in ''
修改ngx_http_lua_headers_in.c文件,版本升级不兼容导致,直接注释以下内容即可(前后加/* 内容段 */)

    { ngx_string("Cookie"),
                 offsetof(ngx_http_headers_in_t, cookies),
                 ngx_http_set_builtin_multi_header },

    { ngx_null_string, 0, ngx_http_set_header }

再次安装=》启动

make -j2     #没报错即可,警告可以不理
make install

#启动服务
cd /usr/sbin   #还没有加到系统服务,需要到二进制目录上执行
./nginx


nginx -V   #查看版本配置

4、系统服务加入Nginx项

4.1、建立服务项 (如你安装过程变更了参数也要相应修改)

vim /usr/lib/systemd/system/nginx.service #配置目录自行修改

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

4.2、建立 /etc/init.d/nginx文件,写入以下内容 (如你安装过程变更了参数也要相应修改)

#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# 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
# pidfile: /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"
lockfile=/var/lock/subsys/nginx
start() {
          [ -x $nginx ] || exit 5
          [ -f $NGINX_CONF_FILE ] || exit 6
         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
          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

4.3、加入到系统服务

chmod 755 /etc/init.d/nginx
chkconfig --add nginx
systemctl start nginx  #启动吧

5、nginx配置GeoIP模块

修改/etc/nginx/nginx.conf,在http段最前面加入以下内容(log定义为main,以便查看效果)

    log_format  main escape=json '$remote_addr - $remote_user [$time_local] '
                        '"$request" $status $body_bytes_sent '
                        '"$http_referer" "$http_user_agent" '
                        '$request_time '
                        '"$geoip2_data_country_name" "$geoip2_data_city_name" ';

    access_log  /var/log/nginx/main.access.log  main;
    geoip2 /etc/nginx/GeoIP2/GeoLite2-Country/GeoLite2-Country.mmdb{
        $geoip2_data_country_code  country  iso_code ;
        $geoip2_data_country_name  country names zh-CN;
    }
    geoip2 /etc/nginx/GeoIP2/GeoLite2-City/GeoLite2-City.mmdb {
        $geoip2_data_country_code country iso_code;
        $geoip2_data_country_name country names zh-CN;
        $geoip2_data_city_name city names zh-CN;
        $geoip2_data_province_name subdivisions 0 names en;
        $geoip2_data_province_isocode subdivisions 0 iso_code;
        $geoip2_continent_code continent code;
    }

重新加载服务:

nginx -s reload

到/var/log/nginx查看geoip效果

已经能从库中配对出地点了。 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

零士

运维调试耗力,喜欢给个赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值