Nginx之编译及部署

1.编译环境准备

1.1 操作系统的准备

Nginx是一款优秀的开源软件,是运行在操作系统 上的应用程序,因此Nginx的性能依赖于操作系统及其 对底层硬件的管理机制,为了使Nginx在运行时发挥最 大的性能,需要对操作系统的服务配置和参数做一些调整。系统服务配置可用如下方式实现。

(1)系统服务安装

CentOS可用最小化安装,安装完毕后,用下列命令安装补充工具

#1.安装扩展工具包epel yum源
[root@ansible01 ~]# yum -y install epel-release
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
软件包 epel-release-7-14.noarch 已安装并且是最新版本
无须任何处理
#2.安装工具
[root@ansible01 ~]# yum install -y net-tools wget nscd lsof
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
软件包 net-tools-2.0-0.25.20131004git.el7.x86_64 已安装并且是最新版本
软件包 wget-1.14-18.el7_6.1.x86_64 已安装并且是最新版本
软件包 lsof-4.87-6.el7.x86_64 已安装并且是最新版本
正在解决依赖关系
--> 正在检查事务
---> 软件包 nscd.x86_64.0.2.17-326.el7_9.3 将被 安装
--> 正在处理依赖关系 glibc = 2.17-326.el7_9.3,它被软件包 nscd-2.17-326.el7_9.3.x86_64 需要
--> 正在检查事务
---> 软件包 glibc.x86_64.0.2.17-317.el7 将被 升级
--> 正在处理依赖关系 glibc = 2.17-317.el7,它被软件包 glibc-common-2.17-317.el7.x86_64 需要
---> 软件包 glibc.x86_64.0.2.17-326.el7_9.3 将被 更新
--> 正在检查事务
---> 软件包 glibc-common.x86_64.0.2.17-317.el7 将被 升级
---> 软件包 glibc-common.x86_64.0.2.17-326.el7_9.3 将被 更新
......
已安装:
  nscd.x86_64 0:2.17-326.el7_9.3                                                                                                                                                           

作为依赖被升级:
  glibc.x86_64 0:2.17-326.el7_9.3                                                          glibc-common.x86_64 0:2.17-326.el7_9.3                                                         

完毕!

(2)DNC缓存

编辑/etc/resolv.conf配置DNS服务器,打开NSCD 服务,缓存DNS,提高域名解析响应速度。

[root@ansible01 ~]# systemctl start nscd.service
[root@ansible01 ~]# systemctl enable nscd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nscd.service to /usr/lib/systemd/system/nscd.service.
Created symlink from /etc/systemd/system/sockets.target.wants/nscd.socket to /usr/lib/systemd/system/nscd.socket.

(3)修改文件打开数限制

操作系统默认单进程最大打开文件数为1024,要 想实现高并发,可以把单进程的文件打开数调整为 65536。

[root@ansible01 ~]# echo -e "* soft nofile 65536# *号表示所用用户\n* hard nofile 65536">>/etc/security/limits.conf

1.2 Linux内核参数

Linux系统是通过proc文件系统实现访问内核内部 数据结构及改变内核参数的,proc文件系统是一个伪 文件系统,通常挂载在/proc目录下,可以通过改 变/proc/sys目录下文件中的值对内核参数进行修 改。/proc/sys目录下的目录与内核参数类别如下表所示

|--------|--------|
| 目录 | 内核参数类别 |
| fs | 文件系统 |
| kernel | CPU、进程 |
| net | 网络 |
| vm | 内存 |

Linux系统环境下,所有的设备都被看作文件来进 行操作,建立的网络连接数同样受限于操作系统的最 大打开文件数。最大打开文件数会是系统内存的10% (以KB来计算),称为系统级限制。可以使用sysctl -a | grep fs.file-max命令查看系统级别的最大打开 文件数。同时,内核为了不让某个进程消耗掉所有的 文件资源,也会对单个进程最大打开文件数做默认值 处理,称之为用户级限制,默认值一般是1024,使用 ulimit -n命令可以查看用户级文件描述符的最大打开数。

[root@ansible01 ~]# sysctl -a | grep fs.file-max
fs.file-max = 173152
sysctl: reading key "net.ipv6.conf.all.stable_secret"
sysctl: reading key "net.ipv6.conf.default.stable_secret"
sysctl: reading key "net.ipv6.conf.ens33.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"
sysctl: reading key "net.ipv6.conf.virbr0.stable_secret"
sysctl: reading key "net.ipv6.conf.virbr0-nic.stable_secret"
[root@ansible01 ~]# ulimit -n
1024

2.Nginx源码编译

2.1 Nginx源码获取

Nginx源码可通过官网直接下载,源码获取命令如下:

[root@ansible01 ~]# mkdir -p /opt/data/source
[root@ansible01 ~]# cd /opt/data/source/
[root@ansible01 source]# wget http://nginx.org/download/nginx-1.27.0.tar.gz  
--2024-06-06 15:07:42--  http://nginx.org/download/nginx-1.27.0.tar.gz
正在解析主机 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:5c0:2601::6, ...
正在连接 nginx.org (nginx.org)|3.125.197.172|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1244887 (1.2M) [application/octet-stream]
正在保存至: "nginx-1.27.0.tar.gz"

100%[=================================================================================================================================================>] 1,244,887   17.8KB/s 用时 2m 10s 

2024-06-06 15:09:54 (9.35 KB/s) - 已保存 "nginx-1.27.0.tar.gz" [1244887/1244887])

[root@ansible01 source]# tar -zxvf nginx-1.27.0.tar.gz 
nginx-1.27.0/
nginx-1.27.0/man/
nginx-1.27.0/LICENSE
nginx-1.27.0/configure
nginx-1.27.0/auto/
nginx-1.27.0/CHANGES
nginx-1.27.0/CHANGES.ru
nginx-1.27.0/html/
nginx-1.27.0/contrib/
......
nginx-1.27.0/auto/types/value
nginx-1.27.0/auto/types/uintptr_t
nginx-1.27.0/auto/types/sizeof
nginx-1.27.0/man/nginx.8

2.2 编译配置参数

编译Nginx源码文件时,首先需要通过编译配置命 令configure进行编译配置。

参数名称 备注
--prefix=value 指向安装目录
--sbin-path 指向(执行)程序文件(nginx
--conf-path= 指向配置文件(nginx.conf
--error-log-path= 指向错误日志目录
--pid-path= 指向 pid 文件(nginx.pid
--lock-path= 指向 lock 文件(nginx.lock)(安装文件锁定,防止安装文件被别人利用,或自己误操作。)进程ID文件
--user= 指定程序运行时的用户名
--group= 指定程序运行时的用户组名
--builddir= 指向编译目录
--with-rtsig_module 启用 rtsig 模块支持(实时信号)
--with-select_module 启用 select 模块支持(一种轮询模式,不推荐在高载环境下使用)禁用:--without-select_module
--with-poll_module 启用 poll 模块支持(功能与 select 相同,与 select 特性相同,为一种轮询模式,不推荐在高载环境下使用)
--with-file-aio 启用 file aio 支持(一种 APL 文件传输格式)
--with-ipv6 启用 ipv6 支持
--add-module= 启用外部模块支持
--with-cc= 指向 C 编译器路径
--with-cpp= 指向 C 预处理路径
--with-cc-opt= 设置 C 编译器参数
--with-ld-opt= 设置连接文件参数
--with-cpu-opt= 指定编译的 CPU,可用的值为:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64
--without-pcre 禁用 PCRE 库
--with-pcre 启用 PCRE 库
--with-pcre= 指向 PCRE 库文件目录
--with-pcre-opt= 在编译时为 PCRE 库设置附加参数
--with-md5= 指向 MD5 库文件目录(消息摘要算法第五版,用以提供消息的完整性保护)
--with-md5-opt= 在编译时为 MD5 库设置附加参数
--with-md5-asm 使用 MD5 汇编源
--with-sha1= 指向 sha1 库目录(数字签名算法,主要用于数字签名)
--with-sha1-opt= 在编译时为 sha1 库设置附加参数
--with-sha1-asm 使用 sha1 汇编源
--with-perl= 设定 perl 库文件路径
--with-zlib= 指向 zlib 库目录
--with-zlib-opt= 在编译时为 zlib 设置附加参数
--with-zlib-asm= 为指定的 CPU 使用 zlib 汇编源进行优化,CPU 类型为 pentium, pentiumpro
--with-libatomic 为原子内存的更新操作的实现提供一个架构
--with-libatomic= 指向 libatomic_ops 安装目录
--with-openssl= 指向 openssl 安装目录
--with-openssl-opt 在编译时为 openssl 设置附加参数
--with-debug 启用 debug 日志
--with-http_ssl_module 启用 ngx_http_ssl_module 支持(使支持 HTTPS 请求,需已安装 openssl)
--with-http_realip_module 启用 ngx_http_realip_module 支持(这个模块允许从请求标头更改客户端的 IP 地址值,默认为关)
--with-http_addition_module 启用 ngx_http_addition_module 支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求)
--with-http_xslt_module 启用 ngx_http_xslt_module 支持(过滤转换 XML 请求)
--with-http_image_filter_module 启用 ngx_http_image_filter_module 支持(传输 JPEG/GIF/PNG 图片的一个过滤器)(默认为不启用。GD 库要用到)
--with-http_geoip_module 启用 ngx_http_geoip_module 支持(该模块创建基于与 MaxMind GeoIP 二进制文件相配的客户端 IP 地址的 ngx_http_geoip_module 变量)
--with-http_sub_module 启用 ngx_http_sub_module 支持(允许用一些其他文本替换 Nginx 响应中的一些文本)
--with-http_dav_module 启用 ngx_http_dav_module 支持(增加 PUT、DELETE、MKCOL 创建集合,COPY 和 MOVE 方法)默认情况下为关闭,需编译开启
--with-http_flv_module 启用 ngx_http_flv_module 支持(提供寻求内存使用基于时间的偏移量文件)
--with-http_gzip_static_module 启用 ngx_http_gzip_static_module 支持(在线实时压缩输出数据流)
--with-http_random_index_module 启用 ngx_http_random_index_module 支持(从目录中随机挑选一个目录索引)
--with-http_secure_link_module 启用 ngx_http_secure_link_module 支持(计算和检查要求所需的安全链接网址)
--with-http_degradation_module 启用 ngx_http_degradation_module 支持(允许在内存不足的情况下返回204或444码)
--with-http_stub_status_module 启用 ngx_http_stub_status_module 支持(获取 Nginx 自上次启动以来的工作状态)
--without-http_charset_module 禁用 ngx_http_charset_module 支持(重新编码 WEB 页面,但只能是一个方向--服务器端到客户端,并且只有一个字节的编码可以被重新编码)
--without-http_gzip_module 禁用 ngx_http_gzip_module 支持(该模块同 --with-http_gzip_static_module 功能一样)
--without-http_ssi_module 禁用 ngx_http_ssi_module 支持(该模块提供了一个在输入端处理处理服务器包含文件(SSI)的过滤器,目前支持 SSI 命令的列表是不完整的)
--without-http_userid_module 禁用 ngx_http_userid_module 支持(该模块用来处理用来确定客户端后续请求的 cookie
--without-http_access_module 禁用 ngx_http_access_module 支持(该模块提供了一个简单的基于主机的访问控制。允许/拒绝基于 IP 地址)
--without-http_auth_basic_module 禁用 ngx_http_auth_basic_module(该模块是可以使用用户名和密码基于 HTTP 基本认证方法来保护你的站点或其部分内容)
--without-http_autoindex_module 禁用 ngx_http_autoindex_module 支持(该模块用于自动生成目录列表,只在 ngx_http_index_module 模块未找到索引文件时发出请求。)
--without-http_geo_module 禁用 ngx_http_geo_module 支持(创建一些变量,其值依赖于客户端的IP地址)
--without-http_map_module 禁用 ngx_http_map_module 支持(使用任意的键/值对设置配置变量)
--without-http_split_clients_module 禁用 ngx_http_split_clients_module 支持(该模块用来基于某些条件划分用户。条件如:ip地址、报头、cookies等等)
--without-http_referer_module 禁用 ngx_http_referer_module支持(该模块用来过滤请求,拒绝报头中 Referer 值不正确的请求)
--without-http_rewrite_module 禁用 ngx_http_rewrite_module ,链接重写
--without-http_proxy_module 禁用 ngx_http_proxy_module 支持(有关代理服务器)
--without-http_fastcgi_module 禁用 ngx_http_fastcgi_module 支持(该模块允许 Nginx 与 FastCGI 进程交互,并通过传递参数来控制 FastCGI 进程工作。 )FastCGI 一个常驻型的公共网关接口。
--without-http_uwsgi_module 禁用 ngx_http_uwsgi_module 支持(该模块用来医用uwsgi协议,uWSGI服务器相关)
--without-http_scgi_module 禁用 ngx_http_scgi_module支持
--without-http_memcached_module 禁用 ngx_http_memcached_module 支持(该模块用来提供简单的缓存,以提高系统效率)
-without-http_limit_zone_module 禁用 ngx_http_limit_zone_module 支持(该模块可以针对条件,进行会话的并发连接数控制)
--without-http_limit_req_module 禁用 ngx_http_limit_req_module 支持(该模块允许你对于一个地址进行请求数量的限制用一个给定的session或一个特定的事件)
--without-http_empty_gif_module 禁用 ngx_http_empty_gif_module 支持(该模块在内存中常驻了一个1*1的透明GIF图像,可以被非常快速的调用)
--without-http_browser_module 禁用 ngx_http_browser_module 支持
--without-http_upstream_ip_hash_module 禁用 ngx_http_upstream_ip_hash_module 支持(该模块用于简单的负载均衡)
--with-http_perl_module 启用 ngx_http_perl_module 支持(该模块使nginx可以直接使用perl或通过ssi调用perl)
--with-perl_modules_path= 设定 perl 模块路径
--http-log-path= 设定 access log 路径
--http-client-body-temp-path= 设定 HTTP 客户端请求临时文件路径
--http-proxy-temp-path= 设定 HTTP 代理临时文件路径
--http-fastcgi-temp-path= 设定 HTTP Fastcgi 临时文件路径
--http-uwsgi-temp-path= 设定 HTTP uwsgi 临时文件路径
--http-scgi-temp-path= 设定 HTTP scgi 临时文件路径
--without-http 禁用 HTTP server 功能
--without-http-cache 禁用 HTTP Cache 功能
--with-mail 启用 POP3/IMAP4/SMTP 代理模块支持
--with-mail_ssl_module 启用 ngx_mail_ssl_module 支持
--without-mail_pop3_module 禁用 POP3 协议
--without-mail_imap_module 禁用 IMAP 协议
--without-mail_smtp_module 禁用 SMTP 协议
--with-google_perftools_module 启用 ngx_google_perftools_module 支持(调试用,剖析程序性能瓶颈)
--with-cpp_test_module 启用 ngx_cpp_test_module 支持

2.3 代码编译

安装编译工具及依赖库,命令如下:

[root@ansible01 source]# yum install -y gcc pcre-devel zlib-devel openssl-devel libxml2-devel libxslt-devel gd-devel GeoIP-devel jemalloc-devel libatomic_ops-devel perl-devel perl-ExtUtils-Embed
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
......
作为依赖被升级:
  krb5-libs.x86_64 0:1.15.1-55.el7_9  libxml2.x86_64 0:2.9.1-6.el7_9.6  libxml2-python.x86_64 0:2.9.1-6.el7_9.6  openssl.x86_64 1:1.0.2k-26.el7_9  openssl-libs.x86_64 1:1.0.2k-26.el7_9 
  xz.x86_64 0:5.2.2-2.el7_9           xz-libs.x86_64 0:5.2.2-2.el7_9    zlib.x86_64 0:1.2.7-21.el7_9            

完毕!

编译所有功能模块,命令如下:

./configure \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module=dynamic \
--with-stream_ssl_preread_module \
--with-compat \
--with-pcre-jit
make && make install

此处只作为示例,可根据具体的需求灵活调整参 数配置。编译后,默认安装目录 为/usr/local/nginx。

2.4 添加第三方模块

Nginx的功能是以模块方式存在的,同时也支持添 加第三方开发的功能模块。执行configure时,通过-- add-module=PATH参数指定第三方模块的代码路径,在 make时就可以进行同步编译了。

添加第三方静态模块的方法如下:

./configure --addmodule=../ngx_http_proxy_connect_module

添加第三方动态模块的方法如下:

./configure --add-dynamic-module=../ngx_http_proxy_connect_module --with-compat

3. Nginx部署

3.1 环境配置

Nginx编译成功后,为了便于操作维护,建议把 Nginx执行文件的路径添加到环境变量中,可以通过如 下命令完成。

cat >/etc/profile.d/nginx.sh << EOF
PATH=$PATH:/usr/local/nginx/sbin
EOF
source /etc/profile

在CentOS操作系统中,配置文件通常放在/etc目 录下,建议将Nginx的conf目录软连接到/etc目录下。

ln -s /usr/local/nginx/conf /etc/nginx

3.2 命令行参数

Nginx执行文件的命令行参数可以通过-h参数获 取,Nginx命令行参数如下:

Usage: nginx [-?hvVtTq] [-s signal] [-c
filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure
options then exit
-t : test configuration and exit
-T : test configuration, dump it and
exit
-q : suppress non-error messages
during configuration testing
-s signal : send signal to a master
process: stop, quit, reopen, reload
-p prefix : set prefix path (default:
/usr/local/openresty/nginx/)
-c filename : set configuration file
(default: conf/nginx.conf)
-g directives : set global directives out of
configuration file

3.3 注册系统服务

CentOS系统环境中使用systemd进行系统和服务管 理,可以按需守护进程,并通过systemctl命令进行 systemd的监测和控制。为了方便Nginx应用进程的维 护和管理,此处把Nginx注册成系统服务,由systemd 进行服务管理,命令如下。

cat >/usr/lib/systemd/system/nginx.service
<<EOF
[Unit]
# 记录service文件的通用信息
Description=The Nginx HTTP and reverse proxy
server # Nginx服务描述信息
After=network.target remote-fs.target nsslookup.target # Nginx服务启动依赖,在指定服务之后启
动
[Service]
# 记录service文件的service信息
Type=forking
# 标准UNIX Daemon使用的启动方式
PIDFile=/run/nginx.pid
# Nginx服务的pid文件位置
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
# Nginx服务启动前删除旧的pid文件
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q
# Nginx服务启动前执行配置文件检测
ExecStart=/usr/local/nginx/sbin/nginx -g "pid
/run/nginx.pid;" # 启动Nginx服务
ExecReload=/usr/local/nginx/sbin/nginx -t -q
# Nginx服务重启前执行配置文件检测
ExecReload=/usr/local/nginx/sbin/nginx -s
reload -g "pid /run/nginx.pid;"
# 重启Nginx服务
ExecStop=/bin/kill -s HUP $MAINPID
# 关闭Nginx服务
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
# 记录service文件的安装信息
WantedBy=multi-user.target
# 多用户环境下启用
EOF



systemctl enable nginx
# 将Nginx服务注册为系统启动后自动启动
systemctl start nginx
# 启动Nginx服务命令
systemctl reload nginx
# reload Nginx服务命令
systemctl stop nginx
# stop Nginx服务命令
systemctl status nginx
# 查看Nginx服务运行状态命令

注:我们在实际应用中一般直接添加yum源后直接yum install -y nginx进行安装。

相关推荐
这可就有点麻烦了8 分钟前
强化学习笔记之【TD3算法】
linux·笔记·算法·机器学习
DY009J8 分钟前
深度探索Kali Linux的精髓与实践应用
linux·运维·服务器
程序员-珍20 分钟前
虚拟机ip突然看不了了
linux·网络·网络协议·tcp/ip·centos
什么鬼昵称1 小时前
Pikachu- Over Permission-垂直越权
运维·服务器
码农小白1 小时前
linux驱动:(22)中断节点和中断函数
linux·运维·服务器
4647的码农历程1 小时前
Linux网络编程 -- 网络基础
linux·运维·网络
2401_857610032 小时前
SpringBoot实现:校园资料分享平台开发指南
服务器·spring boot·php
C++忠实粉丝2 小时前
Linux环境基础开发工具使用(2)
linux·运维·服务器
康熙38bdc3 小时前
Linux 环境变量
linux·运维·服务器
存储服务专家StorageExpert3 小时前
DELL SC compellent存储的四种访问方式
运维·服务器·存储维护·emc存储