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

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

nanshan 2024-11-03 14:00 42 浏览 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之类的使用,之后单独笔记记录如何使用。方便区分安装和使用,尽量不重复记录。

相关推荐

Linux下C++访问web—使用libcurl库调用http接口发送解析json数据

一、背景这两天由于一些原因研究了研究如何在客户端C++代码中调用web服务端接口,需要访问url,并传入json数据,拿到返回值,并解析。 现在的情形是远程服务端的接口参数和返回类型都是json的字符...

干货 | 这 3 个超经典的Linux实战项目,让你分分钟入门Linux系统

编译安装nginx搭建小游戏网站编译安装流程下载nginx代码wget-P/server/tools/http:nginx.org/download/nginx1.22.0.tar.gz解压并进...

权限管理-树莓派linux⑦

前言当你在看这篇README,我感到非常荣幸。作为支持开源、分享的理念的我,给大家带来一些学习上的乐趣。由于本人并非专业的教育领域人士,很多时候天马行空,随心所欲的表达方式,可能让部分人感到不适。请根...

每天Linux学习:linux文件属性

ls-lih先通过这个命令来观察(-l列表显示目录内容详细,-i第一列显示inode,-h将文件大小显示为我们常见的kb,mb等单位)从截图中我们能看出文件属性由这9列信息组成:第1列:inod...

Linux ln、unlink命令用法

ln命令可以用来创建软链接或硬链接。1.创建软链接:ln-s源文件目标文件例如:ln-s/usr/lib/libc.so/usr/local/lib/libc.so.6这样就创建了一...

Linux 系统启动完整流程

一、启动系统流程简介如上图,简述系统启动的大概流程:1:硬件引导UEFi或BIOS初始化,运行POST开机自检2:grub2引导阶段系统固件会从MBR中读取启动加载器,然后将控制权交给启动加载器GRU...

最火的 CI/CD 平台 Jenkins 详细搭建教程(for Linux)

在正式学习Jenkins之前我们需要对两个名词有一定了解,其一是DevOps,另外一个就是CI/CD。何为DevOps?来自wiki百科介绍DevOps是一系列软件开发实践,强调开发人员(Dev)和测...

hadoop集群搭建详细方法

第一步:搭建配置新的虚拟机格式化之前先把tmp目录下所有与Hadoop有关的信息全部删除rm-rf/tmp/hadoop-centos*开启之后jps只有Java的进程:sudovi/et...

Linux 常用命令集合

系统信息arch显示机器的处理器架构(1)uname-m显示机器的处理器架构(2)uname-r显示正在使用的内核版本dmidecode-q显示硬件系统部件-(SMBIOS/DM...

inode文件索引,你了解嘛?你的Linux基础真的扎实嘛?

一、inode是什么?深入了解inode,就要从文件存储说起来!文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector)。每个扇区储存512字节。读取硬盘的时候,不会一个个扇区地读取,这样效率...

linux实例之创建service服务

前面我们讲过可以通过service命令来启动,重启,停止指定的服务程序。service服务可以在系统启动时,自动运行该服务,我们可以利用这一特点,创建service文件,并且让系统重启时,自动执行命令...

linux之软连接和硬连接的区别

硬连接硬链接是通过索引节点进行的链接。在Linux中,多个文件指向同一个索引节点是允许的,像这样的链接就是硬链接。硬链接只能在同一文件系统中的文件之间进行链接,不能对目录进行创建。如果删除硬链接对应的...

Linux inode 详解

简介索引节点(IndexNode)是Linux/类unix系统文件系统上的一种数据结构,用于存储有关文件或目录的元数据。它包含文件的所有信息,除了文件名和数据。inode在文件系统如何存储和检...

Bash 脚本实例:获取符号链接的目标位置

我们都熟悉Linux中的符号链接,通常称为符号链接或软链接,符号链接是指向任何文件系统中的另一个文件或目录的特定文件。本文将介绍Linux中符号链接的基础知识,并创建一个简单的bash脚本...

windows快捷方式,符号链接,软链接和硬链接

当一个软件大量的向C盘写入数据,而我们又无法修改软件保存数据的位置时,可以使用windows系统的“符号链接”(SymbolicLink)功能,将保存数据的位置修改到其它分区中。符号链接类似于我们熟...

取消回复欢迎 发表评论: