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

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

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

相关推荐

ssh终端xshell日志查看命令(xshell怎么看日志)

现在我们云服务器运维较多用的是SSH工具,其中常用的包括PUTTY、XSHELL等,其实大同小异界面UI稍微不同,但是都可以进入远程连接。这里有朋友提到如何查看服务器的日志文件,这个其实和是否使用XS...

使用 Fail Ban 日志分析 SSH 攻击行为

通过分析`fail2ban`日志可以识别和应对SSH暴力破解等攻击行为。以下是详细的操作流程和关键分析方法:---###**一、Fail2ban日志位置**Fail2ban的日志路径因系统配置...

如何高效读取Linux日志文件?这些命令要熟记于心!

在Linux系统中,日志文件通常存储在/var/log目录下。比如,/var/log/syslog(或/var/log/messages,视发行版而定)记录系统整体事件,/var/log/a...

Windows服务器远程登录日志查询方法,linux查看登录日志方法

概述本文介绍Windows、Linux服务器查询系统的远程登录日志方法。根据服务器所使用的操作系统不同,有以下两种查询方法。Linux操作系统的登录日志查询通过远程连接登录Linux服务器,使用roo...

iptables防火墙如何记录日志(防火墙日志查看)

例如:记录所有ssh服务的登录的日志首先,我们需要了解如何将所有的iptables的INPUT链数据包记录到/var/log/messages中。如果你已经有一些iptables规则了,那么将记录日志...

如何安全管理SSH密钥以防止服务器被入侵

SSH密钥安全管理实施指南(2025年更新版)一、密钥生成与存储规范高强度密钥生成bashCopyCodessh-keygen-ted25519-a100#生成ED25519算法密钥(比...

在CentOS上安装nginx服务器(centos搭建代理服务器)

一、环境描述1.虚拟机配置CPU:单核内存:2GB硬盘:120GBIP:10.24.17.1082.操作系统版本:CentOS6.6x86_64安装方式:Minimal3.虚拟化环境VM...

CentOS7安全加固的一份整理规划建议

◆更新系统:及时更新CentOS7操作系统版本和安全补丁,确保系统以最新状态运行。◆关闭不必要的服务:在运行系统时,应关闭不需要的服务和端口,以减少系统暴露的攻击面。◆安装防火墙:使用iptables...

第四十七天-二叉树,centOS安装tomcat,Maven,vsftpd

学习笔记:1.Maven是Apache下的一个纯Java开发的开源项目。基于项目对象模型(缩写:POM)概念,Maven利用一个中央信息片断能管理一个项目的构建、报告和文档等步骤。Maven...

Linux远程桌面连接使用教程 Widows终端远程连接Linux服务器

一、前言为什么不是远程连接Linux服务器?因为我不会,远程连接window我就用电脑自带的“远程桌面连接”。以下所述都是在CentOS操作系统下的。服务器刚换成Linux的时候很迷茫,感觉无从下手...

CentOS 安全加固操作,保护你的操作系统

系统加固是保障系统安全的重要手段,对于维护企业数据安全、用户隐私以及系统稳定运行具有重要意义。加固后的系统更加健壮和稳定,能够有效减少因安全问题导致的系统故障和停机时间,提高系统的可用性和可靠性。通过...

Dockerfile部署Java项目(docker如何部署java项目)

1、概述本文主要会简单介绍什么是Docker,什么是Dockerfile,如何安装Docker,Dockerfile如何编写,如何通过Dockerfile安装jar包并外置yaml文件以及如何通过do...

CentOS7云主机部署Fail2ban阻断SSH暴力破解

关于Fail2banFail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是调用防火墙屏蔽)例如:当有人在试探你的HTTP、SSH、SMTP、FTP密...

在CentOS7上用源码编译安装PostgreSQL

1、新建postgres用户#useraddpostgres&&passwdpostgres2、安装依赖包#yum-yinstallmakegccgcc-c++readline...

pure-ftpd 使用(ftp prompt命令)

pure-ftpd是一个免费的ftp软件,其他介绍就不多说了。我们直接开始主题安装centosyuminstallepel-releaseyuminstallpure-ftpd配置备份原配置...

取消回复欢迎 发表评论: