Nginx编译及部署

Nginx中configure命令的常用编译配置参数,具有带“--with”前缀的编译配置参数的模块不会被默认编译,若要使用该功能模块,需要使用提供的编译配置参数进行编译配置。相反,具有带“--without”前缀的编译配置参数的模块都会被默认编译,如果不想使用某个功能模块,在进行编译置时添加带有“--without”前缀的参数即可。

编译配置参数
默认值/默认编译状态
参数说明

--prefix=PATH

/usr/local

编译后代码的安装目录

--with-select_module

不编译

编译 select I/O 事件机制模块,在不支持Ngi默认I/O 事件机制的操作系统下自动编译该模块

--without-select_module

编译

不编译 select I/0 事件机制模块

--with-poll_module

不编译

编译 poll I/0 事件机制模块,在不支持 Nginx认I/O 事件机制的操作系统下自动编译该模块

--without-poll_module

编译

不编译 pollI/0事件机制模块

--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_xst _module=dynamic

——

动态编译XSLT样式表转换XML 响应模块

--with-http_image_filter_module

编译

编译图像转换模块

--with-http_image_flter_module=dynamic

——

动态编译图像转换模块

--with-http_geoip_module

编译

编译客户端IP 解析城市地址模块

--with-http_geoip_module=dynamic

——

动态编译客户端IP解析城市地址模块

--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

编译

不编译 SSI支持模块

--without-http_userid_module

编译

不编译 cookie 操作模块

--without-http_access_module

编译

不编译基于IP的访问控制模块

--without-http_auth_basic _module

编译

不编译HTTP基本认证模块

--without-http_mirror_module

编译

不编译访问镜像模块

--without-http_autoindex _module

编译

不编译自动目录索引模块

--without-http_geo_module

编译

不编译根据客户 IP 创建变量模块

--without-http_map_module

编译

不编译变量映射模块

--without-http_split_clients _module

编译

不编译自定义客户请求分配模块

--without-http_referer_module

编译

不编译referer 操作模块

--without-http_rewrite_module

编译

不编译rewrite规则模块

--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负载均衡算法模块

--without-http_upstream_ip_hash_module

编译

不编译HTTP协议ip hash负载均模块

--without-http_upstream_least_conn_module

编译

不编译最少连接数算法负载均衡模块

--without-http_upstream_random_module

编译

不编译随机选择算法负载均衡模块

--without-http_upstream_keepalive_module

编译

不编译负载均衡后端长连接支持模块

--without-http_upstream_zone_module

编译

不编译负载均衡共享内存支持模块

--with-http_perl_module

不编译

编译Perl脚本支持模块

--with-http_perl_module=dynamic

——

动态编译Perl脚本支持模块

--with-stream

不编译

编译TCP/UDP代理模块

--with-stream=dynamic

——

动态编译TCP/UDP代理模块

--with-stream_ssl_module

不编译

编译TCP/UDP代理SSL支持模块

--with-stream_realip_module

不编译

编译TCP/UDP代理真实IP模块

--with-stream_geoip_module

不编译

编译地域信息解析模块

--with-stream_geoip_module=dynamic

——

动态编译地域信息解析模块

--with-stream_ssl_preread_module

不编译

编译TCP/UDP代理的SSL预处理模块

--with-google_perftools_module

不编译

编译 Google 的 TCMalloc 内存管理支持模块

--with-cpp_test_module

不编译

编译 C++ 兼容测试模块

--add-module=PATH

——

添加第三方模块

--add-dynamic-module=PATH

——

添加第三方动态模块

--with-ld-opt

——

设置附加的编译参数

--with-pcre-jit

——

pcre JIT 支持,该功能可以提升处理正则表达式的速度

--with-compat

——

启用动态模块兼容模式,参见 1.2.3 节

--with-debug

——

打开调试日志

安装编译工具及依赖库,脚本如下:

yum -y install 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

编译所有功能模块(默认安装目录 /usr/local/nginx),脚本如下:

./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

添加第三方模块:

添加静态模块

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

添加动态模块

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

环境变量配置

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

命令参数

[root@dyw /]# nginx -h
nginx version: nginx/1.17.4
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/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

上述代码中的主要参数解释说明如下:

  • -v 参数:显示Nginx 执行文件的版本信息。

  • -V参数:显示Nginx 执行文件的版本信息和编译配置参数。

  • -t 参数:进行配置文件语法检查,测试配置文件的有效性。

  • -T 参数:进行配置文件语法检查,测试配置文件的有效性,同时输出所有有效配置内容

  • -g 参数:在测试配置文件有效性时,不输出非错误信息。

  • -s 参数:发送信号给 Nginx 主进程,信号可以为以下4 个

  • stop:快速关闭

  • quit:正常关闭。

  • reopen:重新打开日志文件。

  • reload:重新加载配置文件,启动一个加载新配置文件的 Worker Process,正常关闭一个加载旧配置文件的 Worker Process。

  • -p参数:指定Nginx 的执行目录,默认为 confgure 时的安装目录,通常为 /usr/local/nginx。

  • -c参数:指定nginxconf文件的位置,默认为 conf/nginx.conf。

  • -g 参数:外部指定配置文件中的全局指令。

应用示例如下:

nginx -t # 执行配置文件检测

nginx -t -q # 执行配置文件检测,且只输出错误信息

nginx -s stop # 快速停止Nginx

nginx -s quit # 正常关闭Nginx

nginx -s reopen # 重新打开日志文件

nginx -s reload # 重新加载配置文件

nginx -p /usr/local/newnginx # 指定Nginx的执行目录

nginx -c /etc/nginx/nginx.conf # 指定nginx.conf文件的位置

# 外部指定pid和worker_processes配置指令参数

nginx -g "pid /var/run/nginx.pid; worker_processes 'sysctl -n hw.ncpu';"

将Nginx注册成系统服务

cat >/usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The Nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q
ExecStart=/usr/local/nginx/sbin/nginx -g "pid /run/nginx.pid;"
ExecReload=/usr/local/nginx/sbin/nginx -t -q
ExecReload=/usr/local/nginx/sbin/nginx -s reload -g "pid /run/nginx.pid;"

ExecStop=/bin/kill -S HUP SMAINPID
Killsignal=SIGQUIT
TimeoutStopSec=5
Ki11Mode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

Nginx服务控制

systemctl enable nginx
systemctl start nginx
systemctl reload nginx
systemctl stop nginx
systemctl status nginx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豢龙先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值