hopeless-dream

导航

nginx编译安装

安装

下载软件包

[root@apache ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz

安装依赖

[root@apache nginx-1.18.0]# yum install -y gcc gcc-c++ pcre-devel zlib-devel openssl-devel libxml2-devel libxslt-devel gd-devel GeoIP-devel jemalloc-devel libatomic_ops-devel perl-devel perl-ExtUtils-Embed

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com

...

Updated:
  openssl-devel.x86_64 1:1.0.2k-24.el7_9                                                                                         

Dependency Updated:
  GeoIP.x86_64 0:1.5.0-14.el7                libdb.x86_64 0:5.3.21-25.el7                 libdb-utils.x86_64 0:5.3.21-25.el7     
  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-24.el7_9       
  openssl-libs.x86_64 1:1.0.2k-24.el7_9  

用途:

1、gcc:GNU Compiler Collection,GNU编译器套件

2、zlib:提供压缩库

3、pcre:Perl语言兼容正则表达式

4、openssl:用于通信加密和安全通信的工具包

5、libxml2:XML文档的C解释器

6、libxslt:XSLT的C库,用于转换XML文档

7、gd:PHP图形处理的扩展库,提供处理图片的API

8、GeoIP:全球地理位置信息库

9、jemalloc:Facebook开源的内存分配器

10、libatomic:gcc的原子库

nginx的configure命令编译参数

编译参数 默认值 参数说明
--prefix=PATH  不编译 nginx安装目录
--with-select_module 不编译 编译select I/O时间机制模块
--without-select_module 编译  
--with-poll_module  不编译  
--without-poll_module 不编译 poll事件支持模块
--with-threads 不编译 启用线程池支持
--with-file-aio 不编译 启用AIO支持
--with-http_ssl_module 不编译 启用SSL模块
--with-http_v2_module 不编译 HTTP/2模块
--with-http_realip_module 不编译 HTTP的真实IP模块
--with-http_addition_module 不编译 响应内容追加模块
--with-http_xslt_module 不编译 XSLT样式表转换模块
--with-http_xslt_module=dynamic 不编译 动态编译XSLT样式表转换模块
--with-http_image_filter_module  不编译 图像转换模块
--with-http_image_filter_module=dynamic 不编译 动态编译图像转换模块
--with-http_geoip_module  不编译 GEOip支持模块
--with-http_geoip_module=dynamic 不编译 动态编译GEOip支持模块

--with-http_sub_module

不编译 字符串替换模块
--with-http_dav_module 不编译 WebDAV协议模块
--with-http_flv_module 不编译 FLV文件伪流媒体服务器模块
--with-http_mp4_module 不编译 MP4文件伪流媒体服务器模块
--with-http_gunzip_module 不编译 gzip压缩模块
--with-http_gzip_static_module 不编译 发送gz与压缩文件数据模块
--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 不编译 运行状态模块
--without-http_charset_module 不编译 字符集转换模块
--without-http_gzip_module 不编译 gzip方式压缩输出模块
--without-http_ssi_module 不编译 ssl支持模块
--without-http_userid_module 不编译 cookie操作模块
--without-http_access_module 不编译 基于IP的访问控制模块
--without-http_auth_basic_module 不编译 HTTP基本认证模块
--without-http_mirror_module 不编译 访问镜像模块
 --without-http_split_clients_module 不编译 自定义客户请求分配模块
--without-http_referer_module 不编译 referer操作模块
--without-http_rewrite_module 不编译 rewrite规则模块
--without-http_autoindex_module 不编译 自动目录索引模块
--without-http_geo_module 不编译 根据客户IP创建变量模块
--without-http_map_module 不编译 变量映射模块
--without-http_proxy_module 不编译 代理功能模块
--without-http_fastcgi_module 不编译 FastCGI支持模块
--without-http_uwsgi_module 不编译 uWSGI支持模块
 --without-http_scgi_module 不编译 SCGI支持模块
--without-http_grpc_module  不编译 gRPC支持模块
 --without-http_memcached_module 不编译 Memcached服务访问模块
--without-http_limit_conn_module 不编译 并发连接数控制模块
--without-http_limit_req_module 不编译 单IP请求数限制模块
--without-http_empty_gif_module 不编译 空GIF图片模块
--without-http_browser_module 不编译 客户端浏览器识别模块
--without-http_upstream_hash_module 不编译 hash负载均衡算法模块
--with-stream 不编译 TCP/UDP代理模块
--with-stream=dynamic 不编译  
--with-stream_ssl_module 不编译 TCP/UDP代理的SSL支持模块
--with-stream_realip_module 不编译 TCP/UDP代理的真实IP模块
--with-stream_geoip_module 不编译 编译地域信息解析模块
--with-stream_ssl_preread_module  不编译 TCP/UDP代理的SSL预处理模块
--with-google_perftools_module    启用Google的TCMalloc内存管理支持模块
--add-module   添加第三方模块
     
     

其中,带有--with编译参数的模块默认不会被编译,如果使用,只能手动提供对应编译参数。--without编译参数的模块默认启用,不使用的话需要明确提供对应编译参数。

添加nginx用户

[root@localhost ~]# useradd -s /sbin/nologin -M nginx

不添加用户会在启动时报错

[root@localhost nginx-1.18.0]# nginx 
nginx: [emerg] getpwnam("nginx") failed

[root@localhost nginx-1.18.0]# useradd -s /sbin/nologin -M nginx
[root@localhost nginx-1.18.0]# nginx 

编译所有功能模块

# ./configure --prefix=/usr/local/nginx1.18 --with-threads \
--user=nginx --group=nginx --with-debug \
--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

添加第三方模块

nginx支持添加第三方功能模块,在执行configure命令时,使用--add-module=PATH参数执行第三方模块代码路径,在make时同步编译。

添加第三方静态模块,如:

 ./configure --add-module=./ngx_http_proxy_connect_module

添加第三方动态模块,如:

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

二进制启动文件创建软连接

[root@localhost nginx-1.18.0]# ln -s /usr/local/nginx1.18/sbin/nginx /usr/sbin/nginx

 

Nginx的Unit文件解读(可以参考:https://www.cnblogs.com/zh-dream/p/12336421.html)

# cat /usr/lib/systemd/system/nginx.service
[Unit]                                                                # 记录service文件的通用信息
Description=The nginx HTTP and reverse proxy server                   # 服务的描述信息
After=network-online.target remote-fs.target nss-lookup.target        # 在指定服务之后启动
Wants=network-online.target                                           # 服务启动的弱依赖,等式右边指定的服务启动是否成功,不影响本服务

[Service]                                                             # 记录service文件的服务相关信息
Type=forking                                                          # 标准的UNIX Daemon启动方式
PIDFile=/run/nginx.pid                                                # PID文件存放位置
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid                            # 服务启动前执行删除旧的pid文件
ExecStartPre=/usr/sbin/nginx -t                                       # 服务启动前执行配置文件语法检查
ExecStart=/usr/sbin/nginx                                             # 服务启动执行的命令
ExecReload=/usr/sbin/nginx -s reload                                  # 重启nginx
# ExecReload=/usr/sbin/nginx -t -q # 服务重启前执行配置文件语法检测
# ExecReload=/usr/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 # 多用户环境下使用

 添加环境变量

[root@localhost nginx1.18]# echo -e "NGINX_HOME=/usr/local/nginx-1.18.0\nPATH=$NGINX_HOM/sbin:$PATH" >> /etc/profile

[root@localhost nginx1.18]# . /etc/profile

 

posted on 2022-01-20 17:38  hopeless-dream  阅读(243)  评论(0编辑  收藏  举报