Nginx + GeoIP 编译安装 (GeoIP2+nginx-module-vts)

2019年6月28日

1 利用下面的命令来检查系统是否安装了apache

rpm -qa|grep httpd

如果已安装,由于apache和nginx 默认端口都是 80 所以如果nginx 要以80端口启动 那么需要将apache卸载或者关闭或者更改apache的端口 命令:

卸载 apache:
yum -y remove httpd
关闭 apache:
service httpd stop
修改 apache 端口:
vim /etc/httpd/conf/httpd
Listen 80 修改成其他端口 如 8000 ....

 

2 更新系统 (根据要求配置而决定)

yum update

 

3 安装编译需要的组件(可根据自己的需求添加 以下为一般用户的需求)

yum install gcc gcc-c++ zlib zlib-devel openssl openssl-devel libtool pcre pcre-devel gd

4 选择或者创建目录 (下载编译包的位置)

cd /usr/local/src

 

5 下载 安装 pcre组件 (如果上面yum 安装的时候已安装 pcre pcre-devel 可不进行此操作)

编译安装pcre

 

 

 

 

 

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz

tar zxf pcre-8.35.tar.gz

cd pcre-8.35

./configure --prefix=/usr/local/pcre-8.35 --enable-utf --enable-unicode-properties

make (如果提示 -bach make 安装make即可 命令 yum install make)

make install

make && make install

 

 

cd /usr/local/src
wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz
tar zxf ngx_cache_purge-2.1.tar.gz

6 下载GeoIP 源码包 编译 GeoIP 以及 GeoIP的数据库

cd /usr/local/src
wget http://www.maxmind.com/download/geoip/api/c/GeoIP-1.4.8.tar.gz
tar zxf GeoIP-1.4.8.tar.gz
cd GeoIP-1.4.8
libtoolize -f
_______________________________

 

 

 

 

 

[root@vps43021 GeoIP-1.4.8]# libtoolize -f
libtoolize: putting auxiliary files in `.'.
libtoolize: linking file `./ltmain.sh'
libtoolize: Consider adding `AC_CONFIG_MACRO_DIR([m4])' to configure.in and
libtoolize: rerunning libtoolize, to keep the correct libtool macros in-tree.
libtoolize: Consider adding `-I m4' to ACLOCAL_AMFLAGS in Makefile.am.
_______________________________

./configure
make && make install
安装完成后此目录的 data 下有 GeoIP.dat 加载这个的时候写上绝对路劲 我把移出去个人喜好你想做么做就怎么做
mkdir /usr/local/geoip
可以把 GeoIP.dat 移过来也可以下载
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gunzip *.gz

加载 nginx 里添加 (此刻nginx 还没安装 安装完nginx 在配置文件nginx.conf 里添加的)
geoip_country /usr/local/geoip/GeoIP.dat;
geoip_city /usr/local/geoip/GeoLiteCity.dat;

当然也可以使用 yum 去安装 GeoIP GeoIP-devel
yum install GeoIP GeoIP-devel
yum 安装GeoIP 的 dat 库在 /usr/share/GeoIP/

geoip_country /usr/share/GeoIP/GeoIP.dat;

7 下载安装 Ngixx

cd /usr/local/src

wget http://nginx.org/download/nginx-1.6.0.tar.gz

tar -xzf nginx-1.6.0.tar.gz

cd nginx-1.6.0

简易配置

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_realip_module --with-http_geoip_module --add-module=/usr/local/src/ngx_cache_purge-2.1 --with-pcre=/usr/local/pcre-8.35

多功能配置

 

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_realip_module --add-module=/usr/local/src/ngx_cache_purge-2.1 --with-pcre=/usr/local/pcre-8.35 --with-http_image_filter_module --with-http_dav_module --with-http_flv_module --with-http_random_index_module --with-http_secure_link_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-http_gunzip_module --with-http_auth_request_module --with-http_geoip_module

apt-get install libgd2-xpm libgd2-xpm-dev libxml2 libxml2-dev libxslt-dev zlib1g.dev  libgd-dev  libgeoip-dev

 

make && make install

 

#####    GeoIP2   prometheus nginx-module-vts

git  clone https://github.com/vozlt/nginx-module-vts.git

wget https://github.com/maxmind/libmaxminddb/releases/download/1.6.0/libmaxminddb-1.6.0.tar.gz
tar zxf  libmaxminddb-1.6.0.tar.gz
cd libmaxminddb-1.6.0/
ls
make
make install
echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf
vim /etc/ld.so.conf.d/geoip.conf
ldconfig
cd ../
wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/heads/master.zip
unzip master.zip

./configure --prefix=/usr/local/nginx --http-client-body-temp-path=/usr/local/nginx/client_temp --http-proxy-temp-path=/usr/local/nginx/proxy_temp \
  --http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --with-http_v2_module \
  --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_addition_module --with-http_xslt_module \
  --with-http_image_filter_module --with-http_geoip_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module  \
  --with-http_gunzip_module --with-http_gzip_static_module --with-file-aio --with-ipv6 --with-http_degradation_module --with-http_slice_module \
  --with-stream --with-stream_ssl_module --with-stream_realip_module --with-http_sub_module --with-pcre --add-module=/usr/local/src/nginx-module-vts/ --add-module=/usr/local/src/ngx_http_geoip2_module-master

 

8 创建nginx的用户 不必须 需要指定用户跑nginx 服务才执行

groupadd www #加www组
useradd -g www www #加www用户到www组中

 

9创建proxy配置文件

vi /usr/local/nginx/conf/proxy.conf

添加如下内容:

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header Port $proxy_port;
proxy_set_header XHost $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header HTTP_CLIENT_IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_headers_hash_max_size 512;
#proxy_headers_hash_bucket_size 512;
#client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_buffers 32 4k;
proxy_buffer_size 16k;
expires 60;
client_body_timeout 60;
client_header_timeout 60;

 

10 创建Nginx配置文件
先将原文件备份或删除:

vi /usr/local/nginx/conf/nginx.conf

简单配置内容如下:

#user nobody nobody;
worker_processes 4;

 

 

 

 

 

error_log logs/error.log;
pid logs/nginx.pid;

events {
worker_connections 4096;
}

http {
include mime.types;
include proxy.conf;
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"';

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
#     '$status $body_bytes_sent "$http_referer" '
#     '"$http_user_agent" "$http_x_forwarded_for" '
#     '"$upstream_addr" "$upstream_http_host" '
#     '"$host" "$geoip_country_name" "$geoip_region $geoip_city"';

access_log logs/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 45;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 256k;
gzip_http_version 1.0;
gzip_comp_level 5;
gzip_types text/plain image/gif image/jpeg image/png application/javascript application/x-javascript text/css application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
server_tokens off;
proxy_cache_path /var/log/nginx/proxy_temp/cache levels=1:2 keys_zone=cache:10m max_size=100m;
proxy_cache_key "$scheme://$host$uri";
geoip_country /usr/local/geoip/GeoIP.dat;
geoip_city /usr/local/geoip/GeoLiteCity.dat;

include sites/*.conf;
}

11 测试 nginx

修改配置后 就准备启动nginx 首先测试一下
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libGeoIP.so.1: cannot open shared object file: No such file or directory

报错找不到libGeoIP.so.1 明明已经编译安装过了啊 查找下看看

find  / -name libGeoIP.so.1
/usr/local/lib/libGeoIP.so.1
/usr/local/src/GeoIP-1.4.8/libGeoIP/.libs/libGeoIP.so.1

既然存在那么就做个软连接 丢到 /lib 下 注意下64位的系统是 /lib64

ln  -s  /usr/local/lib/libGeoIP.so.1 /lib64/libGeoIP.so.1
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
再度测试一下
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

如果你看到这样的提示 那说明你没有在安装nginx的时候指定 pcre的安装位置 由于是编译安装的 libpcre.so.1在安装的目录和 /usr/local/lib

locate libpcre.so.1
/usr/local/lib/libpcre.so.1
/usr/local/lib/libpcre.so.1.2.3
/usr/local/src/pcre-8.35/.libs/libpcre.so.1
/usr/local/src/pcre-8.35/.libs/libpcre.so.1.2.3

同理 做个软连接

ln -s /usr/local/lib/libpcre.so.1 /lib64/libpcre.so.1
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

12 最后启动nginx (此时 Nginx 不是开机启动 Nginx 开机启动)

nginx 配置 http://blog.csdn.net/shuihan0377/article/details/5822736

/usr/local/nginx/sbin/nginx

###############################################################################################

/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

从错误看出是缺少lib文件导致,进一步查看下

[root@localhost conf]# ldd $(which /usr/local/nginx/sbin/nginx)

linux-gate.so.1 => (0x0071b000)

libpthread.so.0 => /lib/libpthread.so.0 (0×00498000)

libcrypt.so.1 => /lib/libcrypt.so.1 (0×00986000)

libpcre.so.1 => not found

libcrypto.so.6 => /lib/libcrypto.so.6 (0×00196000)

libz.so.1 => /lib/libz.so.1 (0×00610000)

libc.so.6 => /lib/libc.so.6 (0x002d7000)

/lib/ld-linux.so.2 (0x006a8000)

libdl.so.2 => /lib/libdl.so.2 (0x008c3000)

可以看出 libpcre.so.1 => not found 并没有找到,进入/lib目录中手动链接下

# cd /lib (注意 32位和64位 目录不同 lib64)

# ln -s libpcre.so.0.0.1 libpcre.so.1

然后在启动nginx ok 了

##########################################################################

没有评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注