CentOS 7.3编译安装Nginx 1.12.2

1. Nginx简介
Nginx (发音为[engine x])专为性能优化而开发,其最知名的优点是它的稳定性和低系统资源消耗,以及对并发连接的高处理能力(单台物理服务器可支持30000~50000个并发连接), 是一个高性能的 HTTP 和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服。

Linux系统:CentOS 7.3

2. 安装准备
2.1 gcc安装 

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

[root@nginx ~]# yum -y install gcc-c++

2.2 pcre安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。

[root@nginx ~]# yum  -y install  pcre pcre-devel

2.3 zlib安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

[root@nginx ~]# yum -y install zlib zlib-devel

2.4 OpenSSL安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

[root@nginx ~]# yum -y install openssl openssl-devel

3. Nginx安装

3.1 Nginx版本

下载网址:https://nginx.org/en/download.html

选择最新的稳定版nginx-1.12.2
版本说明:

Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版
Stable version:最新稳定版,生产环境上建议使用的版本
Legacy versions:遗留的老版本的稳定版

3.2 Nginx下载

使用wget命令下载

[root@nginx ~]# wget -c https://nginx.org/download/nginx-1.12.2.tar.gz

如没有wget命令则安装:

[root@nginx ~]# yum -y install wget

3.3 解压

[root@nginx ~]# tar -zxvf nginx-1.12.2.tar.gz

3.4 安装配置

3.4.1 新建nginx用户和组

[root@nginx include]# groupadd nginx
[root@nginx include]# useradd -g nginx -d /home/nginx nginx
[root@nginx include]# passwd nginx

3.4.2第三方模块安装

本文以安装第三方模块sticky为例,版本为1.,2.5,下载地址:

可以到主机宝贝资源站下载:

具体下载目录在 /2018年资料/9月/27日/CentOS 7.3编译安装Nginx 1.12.2/

上传解压:

[root@nginx ~]# tar -zxvf nginx-goodies-nginx-sticky-module-ng-08a395c66e42..gz
[root@nginx ~]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-1.2.5

3.4.3 安装

[root@nginx ~]# cd nginx-1.12.2
[root@nginx nginx-1.12.2]# ./configure –add-module=/root/nginx-sticky-1.2.5

指定用户、路径和模块配置(可选):

./configure \
–user=nginx –group=nginx \          #安装的用户组
–prefix=/usr/local/nginx \          #指定安装路径
–with-http_stub_status_module \        #监控nginx状态,需在nginx.conf配置
–with-http_ssl_module \            #支持HTTPS
–with-http_sub_module \            #支持URL重定向
–with-http_gzip_static_module          #静态压缩
–add-module=/root/nginx-sticky-1.2.5          #安装sticky模块

3.5 编译

[root@nginx nginx-1.12.2]# make && make install

报错:

/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c: 在函数‘ngx_http_sticky_misc_sha1’中:
/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:15: 错误:‘SHA_DIGEST_LENGTH’未声明(在此函数内第一次使用)
  u_char hash[SHA_DIGEST_LENGTH];
              ^
/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:15: 附注:每个未声明的标识符在其出现的函数内只报告一次
/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:10: 错误:未使用的变量‘hash’ [-Werror=unused-variable]
  u_char hash[SHA_DIGEST_LENGTH];
          ^
/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c: 在函数‘ngx_http_sticky_misc_hmac_sha1’中:
/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:242:15: 错误:‘SHA_DIGEST_LENGTH’未声明(在此函数内第一次使用)
  u_char hash[SHA_DIGEST_LENGTH];

解决方法:

修改ngx_http_sticky_misc.c文件,新增#include <openssl/sha.h>和#include <openssl/md5.h>模块

[root@nginx nginx-1.12.2]# sed -i ’12a #include <openssl/sha.h>’ /root/nginx-sticky-1.2.5/ngx_http_sticky_misc.c
[root@nginx nginx-1.12.2]# sed -i ’12a #include <openssl/md5.h>’ /root/nginx-sticky-1.2.5/ngx_http_sticky_misc.c

重新编译:

[root@nginx nginx-1.12.2]# make && make install

3.6 nginx命令全局执行设置

[root@nginx bin]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx

4. Nginx相关命令

4.1 版本查看

[root@nginx ~]# nginx  -v
nginx version: nginx/1.12.2

4.2 查看加载的模块

[root@nginx ~]# nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
configure arguments: –add-module=/root/nginx-sticky-1.2.5/

4.3 启停命令

4.3.1 启动

[root@nginx nginx-1.12.2]# nginx

4.3.2 停止

[root@nginx nginx-1.12.2]# nginx -s stop
[root@nginx nginx-1.12.2]# nginx -s quit

4.3.3 动态加载

[root@nginx nginx-1.12.2]# ngins -s reload

4.3.4 测试配置文件nginx.conf正确性

[root@nginx ~]# nginx  -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

nginx -s reload:动态加载,当配置文件nginx.conf有变化时执行该命令动态加载。

4.4 开机自启动

编辑/etc/rc.d/rc.local文件,新增一行/usr/local/nginx/sbin/nginx

[root@nginx rc.d]# cd /etc/rc.d
[root@nginx rc.d]# sed -i ’13a /usr/local/nginx/sbin/nginx’ /etc/rc.d/rc.local
[root@nginx rc.d]# chmod u+x rc.local

5. 更改默认端口

编辑配置文件/usr/local/nginx/conf/nginx.conf,将默认端口80修改为81:

[root@nginx ~]# view /usr/local/nginx/conf/nginx.conf

加载配置:

[root@nginx ~]# nginx -s reload

6. 访问Nginx

6.1 关闭防火墙

[root@nginx ~]# firewall-cmd –state
running
[root@nginx ~]# systemctl stop firewalld.service
[root@nginx ~]# firewall-cmd –state
not running

6.2 访问Nginx

http://localhost:81

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/222335.html<

(0)
运维的头像运维
上一篇2025-04-15 11:03
下一篇 2025-04-15 11:04

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注