Nginx 源码安装

在 CentOS 系统下,虽然可以通过 Yum 工具一键下载 Nginx ,但是当我们需求安装第三方模块、开启某些隐藏功能的时候,就需要我们自己手动下载源码,并编译安装 Nginx 来定制自己的 Nginx 。本文主要演示如何在 CentOS 系统下源码编译安装 Nginx,并安装第三方模块。

准备工作

1. 安装编译工具、依赖包

当前系统为 CentOS7 64 位,首先安装缺少的依赖包:

yum update

yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

yum -y install unzip patch # 安装第三方模块会使用到

这些软件也可以通过下载源码来编译安装,只是要注意编译时默认安装的目录,确保在安装 Nginx 时正确指定相关依赖的安装目录。

2. 新建匿名用户和用户组

新建的用户组和用户主要是在编译配置的时候指定 Nginx 运行的用户和用户组:

groupadd -r nginx

useradd -s /sbin/nologin -g nginx -r nginx

Nginx 编译安装

1. 下载源码包

# 下载最新稳定版本
wget http://nginx.org/download/nginx-1.12.2.tar.gz 

# 下载负载均衡健康检查的第三方模块
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

# 重命名
mv master.zip nginx_upstream_check_module.zip

2. 解压

tar -zxvf nginx-1.12.2.tar.gz

unzip nginx_upstream_check_module.zip

3. 配置编译参数

集成 nginx_upstream_check_module 第三方模块至 Nginx 中:

cd nginx-1.12.2
patch -p1 < /usr/local/src/nginx_http_upstream_check_module/check_1.12.1+.patch  # 安装对应版本的补丁

配置 Nginx 编译参数:

./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--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 \
--add-module=../nginx_upstream_check_module 

# --with-http_gunzip_module 开启隐藏模块
# --add-module= 安装第三发模块

4. 编译并安装

make && make install

5. 检查是否安装成功

# 查看 Nginx 版本号
nginx -v   

# 查看 Nginx 配置信息是否正确
nginx -t 

设置开机启动系统服务

1. 建立服务文件

vim /lib/systemd/system/nginx.service

2. 配置信息

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
 
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
 
[Install]
WantedBy=multi-user.target

3. 修改文件权限

chmod 754 /lib/systemd/system/nginx.service

4. 设置为开机启动,同时防火墙开启80端口

# 启动 Nginx
systemctl start nginx   
# 设置开机启动
systemctl enable nginx  

# 防火墙开启80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent 
# 重启防火墙
systemctl restart firewalld.service 

参考文章:

  1. nginx 服务器安装及配置文件详解
  2. CentOS7.0 下编译安装Nginx 1.10.0
  3. CentOS 7自定义开机启动系统服务
  4. nginx使用yum/编译安装,心跳检查模块

PHP 学习总结
研读官网文档和网络优秀个人文章,对 PHP 编程相关作出学习总结。

Stay hungry, stay foolish.

846 声望
84 粉丝
0 条评论
推荐阅读
PHP 微服务集群搭建
微服务架构是一种架构模式,它提倡将单一应用程序划分成一组小的服务,服务之间相互协调、互相配合,为用户提供最终价值。每个服务运行在其独立的进程中,服务和服务之间采用轻量级的通信机制相互沟通。每个服务...

Jochen112阅读 14.2k评论 6

如何选择适合你的微服务 API 网关:对比 Kong、APISIX、Tyk、Apigee 和其他网关
API 网关并非一个新兴的概念,在十几年前就已经存在了,它的作用主要是作为流量的入口,统一的处理和业务相关的请求,让请求更加安全、快速和准确的得到处理。它有以下传统的功能:

API7_技术团队8阅读 8.6k评论 2

记一次使用gdb诊断gc问题全过程
上次解决了GC长耗时问题后,系统果然平稳了许多,这是之前的文章《GC耗时高,原因竟是服务流量小?》 然而,过了一段时间,我检查GC日志时,又发现了一个GC问题,如下: 从这个图中可以发现,我们GC有一些尖峰,...

扣钉日记2阅读 1.1k

封面图
使用kubeasz部署高可用kubernetes集群
本实验采用kubeasz作为kubernetes环境部署工具,它是一个基于二进制方式部署和利用ansible-playbook实现自动化来快速部署高可用kubernetes集群的工具,详细介绍请查看kubeasz官方。本实验用到的所有虚拟机默认软...

李朝阳4阅读 728

有了 NGINX 和 Kong,为什么还需要 Apache APISIX?
2021 年 5 月,云原生社区技术沙龙·广州站,API7.ai(支流科技)联合创始人 & CTO,Apache APISIX PMC 王院生在活动上做了《有了 NGINX 和 Kong,为什么还需要 Apache APISIX》的分享,以下是现场分享的文字版。...

API7_技术团队1阅读 4.2k

Nginx 配置常用参数,看这一篇就够了
最近在全面学习Nginx,当作笔记了,如有错误,欢迎指出或深入交流。主模块 {代码...} 事件模块 {代码...} http部分 {代码...} 部分参数详细说明server_name {代码...} location {代码...} location表达式类型 {代...

开源到2阅读 1.9k

化虹为桥 - Nginx 如何代理 UDP “连接”
众所周知,UDP 并不像 TCP 那样是基于连接的。但有些时候,我们需要往一个固定的地址发送多个 UDP 来完成一个 UDP 请求。为了保证服务端能够知道这几个 UDP 包构成同一个会话,我们需要在发送 UDP 包时绑定某个端...

spacewander4阅读 1.6k

Stay hungry, stay foolish.

846 声望
84 粉丝
宣传栏