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

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

liebian365 2024-10-26 13:05 26 浏览 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之类的使用,之后单独笔记记录如何使用。方便区分安装和使用,尽量不重复记录。

相关推荐

4万多吨豪华游轮遇险 竟是因为这个原因……

(观察者网讯)4.7万吨豪华游轮搁浅,竟是因为油量太低?据观察者网此前报道,挪威游轮“维京天空”号上周六(23日)在挪威近海发生引擎故障搁浅。船上载有1300多人,其中28人受伤住院。经过数天的调...

“菜鸟黑客”必用兵器之“渗透测试篇二”

"菜鸟黑客"必用兵器之"渗透测试篇二"上篇文章主要针对伙伴们对"渗透测试"应该如何学习?"渗透测试"的基本流程?本篇文章继续上次的分享,接着介绍一下黑客们常用的渗透测试工具有哪些?以及用实验环境让大家...

科幻春晚丨《震动羽翼说“Hello”》两万年星间飞行,探测器对地球的最终告白

作者|藤井太洋译者|祝力新【编者按】2021年科幻春晚的最后一篇小说,来自大家喜爱的日本科幻作家藤井太洋。小说将视角放在一颗太空探测器上,延续了他一贯的浪漫风格。...

麦子陪你做作业(二):KEGG通路数据库的正确打开姿势

作者:麦子KEGG是通路数据库中最庞大的,涵盖基因组网络信息,主要注释基因的功能和调控关系。当我们选到了合适的候选分子,单变量研究也已做完,接着研究机制的时便可使用到它。你需要了解你的分子目前已有哪些...

知存科技王绍迪:突破存储墙瓶颈,详解存算一体架构优势

智东西(公众号:zhidxcom)编辑|韦世玮智东西6月5日消息,近日,在落幕不久的GTIC2021嵌入式AI创新峰会上,知存科技CEO王绍迪博士以《存算一体AI芯片:AIoT设备的算力新选择》...

每日新闻播报(September 14)_每日新闻播报英文

AnOscarstatuestandscoveredwithplasticduringpreparationsleadinguptothe87thAcademyAward...

香港新巴城巴开放实时到站数据 供科技界研发使用

中新网3月22日电据香港《明报》报道,香港特区政府致力推动智慧城市,鼓励公私营机构开放数据,以便科技界研发使用。香港运输署21日与新巴及城巴(两巴)公司签署谅解备忘录,两巴将于2019年第3季度,开...

5款不容错过的APP: Red Bull Alert,Flipagram,WifiMapper

本周有不少非常出色的app推出,鸵鸟电台做了一个小合集。亮相本周榜单的有WifiMapper's安卓版的app,其中包含了RedBull的一款新型闹钟,还有一款可爱的怪物主题益智游戏。一起来看看我...

Qt动画效果展示_qt显示图片

今天在这篇博文中,主要实践Qt动画,做一个实例来讲解Qt动画使用,其界面如下图所示(由于没有录制为gif动画图片,所以请各位下载查看效果):该程序使用应用程序单窗口,主窗口继承于QMainWindow...

如何从0到1设计实现一门自己的脚本语言

作者:dong...

三年级语文上册 仿写句子 需要的直接下载打印吧

描写秋天的好句好段1.秋天来了,山野变成了美丽的图画。苹果露出红红的脸庞,梨树挂起金黄的灯笼,高粱举起了燃烧的火把。大雁在天空一会儿写“人”字,一会儿写“一”字。2.花园里,菊花争奇斗艳,红的似火,粉...

C++|那些一看就很简洁、优雅、经典的小代码段

目录0等概率随机洗牌:1大小写转换2字符串复制...

二年级上册语文必考句子仿写,家长打印,孩子照着练

二年级上册语文必考句子仿写,家长打印,孩子照着练。具体如下:...

一年级语文上 句子专项练习(可打印)

...

亲自上阵!C++ 大佬深度“剧透”:C++26 将如何在代码生成上对抗 Rust?

...

取消回复欢迎 发表评论: