百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

Nginx系列:OpenResty 1.25.3.x 编译安装

suiw9 2024-11-14 19:11 38 浏览 0 评论

一、简介

本实验不仅仅记录Openresty的编译安装,重点记录了相关nginx模块的安装,以此来增强Openresty的功能。本实验安装的模块有:

  • geoip2:识别ip地址地理信息。
  • IP2Location:识别ip地址地理信息。(可以替代geoip2,与geoip2类似)
  • Modsecurity:开源waf,业界广泛使用的,真的免费。
  • nginx-ssl-fingerprint:ja3指纹,可以提高反爬效果。
  • nginx-module-vts:Nginx虚拟主机流量状态模块,适合一台主机上部署了很多站点的场景。
  • ngx_healthcheck:
    https://github.com/zhouchangxun/ngx_healthcheck_module,健康检查,不仅仅支持http。还支持更多:四层支持的检测类型:tcp / udp / http、七层支持的检测类型:http / fastcgi


PS:您可以简单的理解为Openresty其实就是Nginx,通过lua来扩展Nginx的功能。即使您的环境使用Nginx就可以满足您的需要,我依然建议您尝试使用Openresty代替Nginx,毕竟当您需要扩展其功能时Openresty会极为的方便。


二、实验环境

  1. 操作系统:Almalinux9.4(基本上兼容Centos9 Stream、RockLinux9)
  2. openresty版本:1.25.3.2


三、操作系统环境准备


3.1、时间同步

yum -y install chrony
mv /etc/chrony.conf /etc/chrony.conf.bak
vi /etc/chrony.conf

在 /etc/chrony.conf 文件开头增加下面的内容:

server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst
server ntp5.aliyun.com iburst
server ntp6.aliyun.com iburst
server ntp7.aliyun.com iburst

如下图:

启动chronyd服务:

systemctl start chronyd.service
systemctl enable chronyd.service
systemctl status chronyd.service


3.2、关闭selinux

# 执行下面的命令即可
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config


3.3、安装相关依赖

dnf install -y epel-release

dnf install -y gcc-c++ perl readline-devel pcre-devel openssl-devel gcc postgresql-devel curl

dnf -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel unzip patch wget screen libxslt libxslt-devel gd gd-devel perl-ExtUtils-Embed vim bzip2 patch lbzip2


3.4、添加www用户和组

groupadd www
useradd -g www www -s /sbin/nologin -M


四、安装Openresty

为了方便学习的同学,特意将用到的安装包分享给大家,包名:openresty-1.25.3.2.zip,内容如下:

https://www.123865.com/s/Lj5A-uR2jd?提取码:4ztl
https://www.123684.com/s/Lj5A-uR2jd?提取码:4ztl


我实验时将openresty-1.25.3.2.zip解压后的内容上传到了/root/openresty-1.25.3.2,然后设置变量方便下面的操作

OPENRESTY_SOFTWARE="/root/openresty-1.25.3.2"
# 验证
echo $OPENRESTY_SOFTWARE



4.1、安装jemalloc

说明:

  • jemalloc 是一个高效的内存分配库,它被设计用于改善并发程序和高负载场景下的内存管理。与操作系统提供的默认内存分配器(例如 malloc)相比,jemalloc 在减少内存碎片、提高多线程性能、优化内存分配速度等方面具有显著优势。
  • github地址:
    https://github.com/jemalloc/jemalloc
  • 实验测试证明如果仅yum -y install jemalloc安装,nginx在编译--with-ld-opt="-ljemalloc"时无法通过


cd $OPENRESTY_SOFTWARE/modules
tar xf jemalloc-5.3.0.tar.bz2
cd jemalloc-5.3.0
./configure
gmake && gmake install
echo '/usr/local/lib' >> /etc/ld.so.conf.d/local.conf
ldconfig /usr/local/lib
ldconfig -v | grep jemalloc


4.2、geoip2 动态识别库

说明:

  • 用于使用geoip识别ip地址的地理信息。
  • github地址:
    https://github.com/maxmind/libmaxminddb/releases
  • 使用geoip需要libmaxminddb 对 mmdb 的高效访问,因此首先需要安装 libmaxminddb 的动态识别库。


cd $OPENRESTY_SOFTWARE/modules
tar xf libmaxminddb-1.11.0.tar.gz
cd libmaxminddb-1.11.0
./configure
gmake && gmake install
ldconfig /usr/local/lib


4.3、安装IP2Location C库

说明:

IP2Location类似geoip,也是用于识别ip地址地理信息的。相当于让自己多了一个选择,有人认为IP2Location优于geoip。

IP2Location也是有免费版的,而且很好用。

  • 参考:
    https://www.ip2location.com/development-libraries/ip2location/nginx
  • 下载ip2location-nginx:
    https://github.com/ip2location/ip2location-nginx
  • 下载ip2location C库:
    https://github.com/chrislim2888/IP2Location-C-Library
cd $OPENRESTY_SOFTWARE/modules/ip2location
tar xf IP2Location-C-Library-8.6.1.tar.gz
cd IP2Location-C-Library-8.6.1
autoreconf -i -v --force
./configure
gmake && gmake install
cd data
# 时间较长,耐心等待
perl ip-country.pl
# 然后执行:
ldconfig /usr/local/lib



4.4、安装Modsecurity

说明:

要使得nginx结合Modsecurity,①首先安装Modsecurity库;②再次安装ModSecurity-nginx模块。

也就是说openresty通过ModSecurity-nginx来调用Modsecurity库。

  • github地址:
    https://github.com/SpiderLabs/ModSecurity


4.4.1、安装Modsecurity所需的依赖

dnf -y install -y git wget

dnf -y install -y gcc-c++ flex bison yajl lua curl-devel curl zlib-devel pcre-devel pcre2-devel libxml2-devel ssdeep-devel libtool autoconf automake make 

#启用 PowerTools (CRB) 仓库,进而安装doxygen 
# 如果是Almalinux8 需要执行:dnf config-manager --set-enabled powertools
dnf config-manager --set-enabled crb
dnf -y install doxygen lua-devel yajl-devel lmdb lmdb-devel 


4.4.2、安装Modsecurity

cd $OPENRESTY_SOFTWARE/ModSecurity
git checkout -b v3/master origin/v3/master
git submodule init
git submodule update

sh build.sh
./configure
# 有多少核的cpu就-j多少,加快编译。
make -j4
make install


# modsecurity库安装到了 /usr/local/modsecurity/lib
echo "/usr/local/modsecurity/lib" | sudo tee /etc/ld.so.conf.d/modsecurity.conf
ldconfig
# 验证
ldconfig -p | grep modsecurity



4.5、准备nginx模块

cd $OPENRESTY_SOFTWARE/modules/ip2location/
tar xf ip2location-nginx-8.6.0.tar.gz

cd $OPENRESTY_SOFTWARE/modules/
tar xf openssl-1.1.1w.tar.gz
tar xf pcre-8.45.tar.gz
tar xf zlib-1.3.1.tar.gz
tar xf ngx_cache_purge-2.3.tar.gz
unzip ngx_healthcheck_module-master.zip
tar xf nginx-module-vts-0.2.2.tar.gz
tar xf ngx_http_geoip2_module-3.4.tar.gz

# git下载ModSecurity-nginx仓库即可,我已经在安装包中准备好了
git clone https://github.com/SpiderLabs/ModSecurity-nginx

# JA3指纹,我已经在安装包中准备好了
git clone https://github.com/phuslu/nginx-ssl-fingerprint


4.6、导入补丁

# 解压OpenResty
cd $OPENRESTY_SOFTWARE/
tar xf openresty-1.25.3.2.tar.gz
cd openresty-1.25.3.2/bundle/nginx-1.25.3/
  
# 导入模块 ngx_healthcheck_module
patch -p1 < ../../../modules/ngx_healthcheck_module-master/nginx_healthcheck_for_nginx_1.19+.patch
echo $?
  
# 导入模块:nginx-ssl-fingerprint
# https://github.com/phuslu/nginx-ssl-fingerprint?tab=readme-ov-file
patch -p1 < ../../../modules/nginx-ssl-fingerprint/patches/nginx-1.25.patch
echo $?
  
# 导入模块:nginx-ssl-fingerprint的openssl补丁
# 我现在还是在openresty-1.25.3.x/bundle/nginx-1.25.3/ 目录下,但是这个补丁是给openssl打的,这里为了操作顺畅,直接添加参数-d指定打补丁的目录
# 且注意,打补丁的openssl的版本号。不要打错了。我这里默认使用的是openssl1.1.1w
patch -p1 -d ../../../modules/openssl-1.1.1w < ../../../modules/nginx-ssl-fingerprint/patches/openssl.OpenSSL_1_1_1-stable.patch
echo $?


4.7、编译安装Openresty

cd $OPENRESTY_SOFTWARE/openresty-1.25.3.2
./configure \
--with-luajit \
--with-http_iconv_module \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_perl_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_auth_request_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-threads \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_ssl_preread_module \
--with-http_slice_module \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module \
--with-pcre=../modules/pcre-8.45 \
--with-zlib=../modules/zlib-1.3.1 \
--with-openssl=../modules/openssl-1.1.1w \
--with-http_perl_module=dynamic \
--add-module=../modules/ngx_cache_purge-2.3 \
--add-module=../modules/ngx_healthcheck_module-master \
--add-dynamic-module=../modules/ngx_http_geoip2_module-3.4 \
--add-dynamic-module=../modules/ip2location/ip2location-nginx-8.6.0 \
--add-module=../modules/nginx-ssl-fingerprint \
--add-module=../modules/ModSecurity-nginx \
--add-module=../modules/nginx-module-vts-0.2.2 \
--with-ld-opt="-L/usr/local/lib" \
--with-ld-opt="-ljemalloc"

你可以根据自己的实际情况调整某些模块为“动态模块”,这样带需要的时候再使用。动态模块,静态模块各有优缺点,请自行斟酌。

推荐Openresty的默认安装位置保持默认的/usr/local/openresty,除非你有特殊需求再自行修改编译选项。--prefix=PATH

make && make install


4.8、创建启动脚本

vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
LimitCORE=infinity
LimitNOFILE=1024000
LimitNPROC=1024000
PrivateTmp=true

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
systemctl status nginx


4.9、设置Openresty环境变量

cat>/etc/profile.d/openresty.sh<<EOF
export PATH=\$PATH:/usr/local/openresty/bin:/usr/local/openresty/luajit/bin
EOF
source /etc/profile


总结

到此openresty编译安装完毕。

这里不记录ip2location、modsecurity之类的使用,之后单独笔记记录如何使用。方便区分安装和使用,尽量不重复记录。

相关推荐

5款Syslog集中系统日志常用工具对比推荐

一、为何要集中管理Syslog?Syslog由Linux/Unix系统及其他网络设备生成,广泛分布于整个网络。因其包含关键信息,可用于识别网络中的恶意活动,所以必须对其进行持续监控。将Sys...

跨平台、多数据库支持的开源数据库管理工具——DBeaver

简介今天给大家推荐一个开源的数据库管理工具——DBeaver。它支持多种数据库系统,包括Mysql、Oracle、PostgreSQL、SLQLite、SQLServer等。DBeaver的界面友好...

强烈推荐!数据库管理工具:Navicat Premium 16.3.2 (64位)

NavicatPremium,一款集数据迁移、数据库管理、SQL/查询编辑、智能设计、高效协作于一体的全能数据库开发工具。无论你是MySQL、MariaDB、MongoDB、SQLServer、O...

3 年 Java 程序员还玩不转 MongoDB,网友:失望

一、什么场景使用MongoDB?...

拯救MongoDB管理员的GUI工具大赏:从菜鸟到极客的生存指南

作为一名在NoSQL丛林中披荆斩棘的数据猎人,没有比GUI工具更称手的瑞士军刀了。本文将带你围观五款主流MongoDB管理神器的特性与暗坑,附赠精准到扎心的吐槽指南一、MongoDBCompass:...

mongodb/redis/neo4j 如何自己打造一个 web 数据库可视化客户端?

前言最近在做neo4j相关的同步处理,因为产线的可视化工具短暂不可用,发现写起来各种脚本非常麻烦。...

solidworks使用心得,纯干货!建议大家收藏

SolidWorks常见问题...

统一规约-关乎数字化的真正实现(规范统一性)

尽管数字化转型的浪潮如此深入人心,但是,对于OPCUA和TSN的了解却又甚少,这难免让人质疑其可实现性,因为,如果缺乏统一的语义互操作规范,以及更为具有广泛适用的网络与通信,则数字化实际上几乎难以具...

Elasticsearch节点角色配置详解(Node)

本篇文章将介绍如下内容:节点角色简介...

产前母婴用品分享 篇一:我的母婴购物清单及单品推荐

作者:DaisyH8746在张大妈上已经混迹很久了,有事没事看看“什么值得买”已渐渐成了一种生活习惯,然而却从来没有想过自己要写篇文章发布上来,直到由于我产前功课做得“太过认真”(认真到都有点过了,...

比任何人都光彩照人的假期!水润、紧致的肌肤护理程序

图片来源:谜尚愉快的假期临近了。身心振奋的休假季节。但是不能因为这种心情而失去珍贵的东西,那就是皮肤健康。炙热的阳光和强烈的紫外线是使我们皮肤老化的主犯。因此,如果怀着快乐的心情对皮肤置之不理,就会使...

Arm发布Armv9边缘AI计算平台,支持运行超10亿参数端侧AI模型

中关村在线2月27日消息,Arm正式发布Armv9边缘人工智能(AI)计算平台。据悉,该平台以全新的ArmCortex-A320CPU和领先的边缘AI加速器ArmEthos-U85NPU为核心...

柔性——面向大规模定制生产的数字化实现的基本特征

大规模定制生产模式的核心是柔性,尤其是体现在其对定制的要求方面。既然是定制,并且是大规模的定制,对于制造系统的柔性以及借助于数字化手段实现的柔性,就提出了更高的要求。面向大规模定制生产的数字化业务管控...

创建PLC内部标准——企业前进的道路

作者:FrankBurger...

标准化编程之 ----------- 西门子LPMLV30测试总结

PackML乃是由OMAC开发且被ISA所采用的自动化标准TR88.00.02,能够更为便捷地传输与检索一致的机器数据。PackML的主要宗旨在于于整个工厂车间倡导通用的“外观和感觉”,...

取消回复欢迎 发表评论: