章工运维 章工运维
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)

章工运维

业精于勤,荒于嬉
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)
  • linux

  • windows

  • 中间件

    • nginx

      • nginx配置教程
      • nginx常用配置
      • keepalived的原理和web服务高可用实践
      • 在CentOS 7上编译安装Nginx
      • nginx配置下载站点
      • nginx配置代理ftp
      • nginx配置php程序负载均衡
      • nginx反代grpc
    • kafka

    • rabbitmq

    • centos7.9部署keepalived
    • 应用性能监控-skywalking
  • 网络

  • 安全

  • 存储

  • 防火墙

  • 数据库

  • 系统

  • docker

  • other

  • 监控

  • 运维
  • 中间件
  • nginx
章工运维
2023-04-13

在CentOS 7上编译安装Nginx

# nginx编译安装

  1. 安装编译所需的依赖包:
sudo yum install -y gcc-c++ pcre-devel zlib-devel openssl-devel wget
1
  1. 下载 Nginx 1.20.2:
cd /opt
sudo wget http://nginx.org/download/nginx-1.20.2.tar.gz
sudo tar -zxvf nginx-1.20.2.tar.gz
cd nginx-1.20.2
1
2
3
4
  1. 配置编译选项:
./configure  --user=root --group=root --prefix=/data/app/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-pcre --with-stream
1

安装多模块

./configure --prefix=/data/nginx --pid-path=/data/nginx/run/nginx.pid --user=root --group=root --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module
1
  1. 编译并安装 Nginx:
sudo make
sudo make install
1
2
  1. 创建 Nginx 系统服务单元文件:
vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=The Nginx HTTP and reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/data/nginx/run/nginx.pid
ExecStartPre=/data/nginx/sbin/nginx -t
ExecStart=/data/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

保存并退出。

  1. 重新加载 systemctl 守护进程:
sudo systemctl daemon-reload
1
  1. 启动并设置 Nginx 在开机时自动启动:
sudo systemctl start nginx
sudo systemctl enable nginx
1
2
  1. 检查 Nginx 版本和运行状态:
nginx -v
sudo systemctl status nginx
1
2

# nginx编译后添加第三方模块

已经安装好的nginx,需要添加一个未被编译安装的模块,需要如何操作呢?

nginx的模块是需要重新编译nginx,而不是像apache一样配置文件引用.so。这里以安装第三方ngx_http_google_filter_module模块为例:

  1. 下载第三方扩展模块ngx_http_google_filter_module
# cd /data/software/ 
# git clone https://github.com/cuber/ngx_http_google_filter_module
1
2
  1. 查看nginx编译安装时安装了哪些模块
# nginx -V nginx version: nginx/1.15.3
built by gcc 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)
built with OpenSSL 1.1.0f  25 May 2017
TLS SNI support enabled
 configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=/data/software/ngx_http_substitutions_filter_module
1
2
3
4
5
可以看出编译安装使用了--prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=/data/software/ngx_http_substitutions_filter_module这些参数。--add-module=/data/software/ngx_http_substitutions_filter_module是之前编译添加ngx_http_substitutions_filter_module模块时添加
1
  1. 加入需要安装的模块,重新编译,如这里添加–add-module=/data/software/ngx_http_google_filter_module
# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=/data/software/ngx_http_substitutions_filter_module --add-module=/data/software/ngx_http_google_filter_module
# make    //千万不要make install,不然就真的覆盖了
1
2
  1. 替换nginx二进制文件:
# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
# cp ./objs/nginx /usr/local/nginx/sbin/
1
2

参考链接 (opens new window)

微信 支付宝
上次更新: 2023/07/10, 21:25:36

← keepalived的原理和web服务高可用实践 nginx配置下载站点→

最近更新
01
shell脚本模块集合
05-13
02
生活小技巧(认知版)
04-29
03
生活小技巧(防骗版)
04-29
更多文章>
Theme by Vdoing | Copyright © 2019-2025 | 点击查看十年之约 | 鄂ICP备2024072800号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式