高性能web服务器3——Nginx编译安装

Nginx 编译安装

准备环境

  1. 系统要求
    • 操作系统:Linux 或类 Unix 系统。
    • 编译器:GCC 或其他兼容的 C 编译器。

源码安装需要提前准备标准的编译器,GCC的全称是(GNU Compiler collection),其有GNU开发,并以
GPL即LGPL许可,是自由的类UNIX即苹果电脑Mac OS X操作系统的标准编译器,因为GCC原本只能处理C语言,所以原名为GNU C语言编译器,后来得到快速发展,可以处理C++,Fortran,pascal,objective C,java以及Ada等其他语言,此外还需要Automake工具,以完成自动创建Makefile的工作,Nginx的一些模块需要依赖第三方库,比如: pcre(支持rewrite),zlib(支持gzip模块)和openssl(支持ssl模块)等

  1. 安装依赖包
    • PCRE (Perl Compatible Regular Expressions):用于正则表达式支持。
    • zlib:用于 gzip 压缩支持。
    • OpenSSL:用于 SSL/TLS 支持。
    • 其他可能需要的库(例如:GeoIP)。

例如,在 Debian/Ubuntu 上安装这些依赖项:

 sudo apt-get update
 sudo apt-get install build-essential libpcre3-dev zlib1g-dev libssl-dev

在RHEL上安装依赖项`

 dnf install gcc pcre-devel zlib-devel openssl-devel -y

下载源码

  1. 下载 Nginx 源码
    • 访问 Nginx 官方网站或 GitHub 仓库获取最新版本的源代码。
    • 例如,下载最新稳定版:
[root@Nginx nginx]# tar zxf nginx-1.24.0.tar.gz
[root@Nginx nginx-1.24.0]# useradd -s /sbin/nologin -M nginx
[root@Nginx nginx]# cd nginx-1.24.0/
[root@Nginx nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README

配置编译选项

  1. 编译前配置

    • 进入源码目录后,可以通过 ./configure 脚本来指定编译选项。
    • 常见的编译选项包括启用第三方模块、设置安装路径等。

    例如,配置 Nginx 以启用 gzip 压缩、PCRE 支持和 SSL/TLS 支持:

[root@Nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx \ # 指定nginx运行用户
--group=nginx \ # 指定nginx运行组
--with-http_ssl_module \ # 支持https://
--with-http_v2_module \ # 支持http版本2
--with-http_realip_module \ # 支持ip透传
--with-http_stub_status_module \ # 支持状态页面
--with-http_gzip_static_module \ # 支持压缩
--with-pcre \ # 支持正则
--with-stream \ # 支持tcp反向代理
--with-stream_ssl_module \ # 支持tcp的ssl加密
--with-stream_realip_module # 支持tcp的透传ip

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --add-module=/root/echo-nginx-module-0.63 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module \

编译与安装

  1. 编译 Nginx

    • 执行 make 命令来编译 Nginx。
    make
    
  2. 安装 Nginx

    • 使用 make install 命令来安装 Nginx 到指定位置。
     make install
    

验证安装

启动 Nginx

  • 在安装目录下启动 Nginx,或者根据系统的初始化脚本启动 Nginx。
/usr/local/nginx/sbin/nginx

验证 Nginx

  • 检查 Nginx 是否正在运行。
ps aux | grep nginx
[root@Nginx nginx-1.24.0]# ls /usr/local/nginx/
conf html logs sbin #nginx完成安装以后,有四个主要的目录

conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他
的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params
两个文件,配置文件一般都有一个样板配置文件,是以.default为后缀,使用时可将其复制并将default后缀
去掉即可。
html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web
文件是默认的错误页面提示页面。
logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比
如/var/logs/nginx里面。
sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。

  • 访问 Nginx 的默认页面,确保 Nginx 正确安装并运行。
curl http://localhost

启动脚本与服务管理

  1. 创建启动脚本

    • 对于不同的 Linux 发行版,可能需要创建相应的系统服务文件(如 systemd 单元文件)以便管理 Nginx。
    • 将 Nginx 添加到开机启动列表。
  2. 配置防火墙

    • 如果系统有防火墙,需要打开相应的端口(通常是 80 和 443)。
  3. 配置 SELinux 或 AppArmor

    • 如果系统启用了 SELinux 或 AppArmor,可能需要调整策略以允许 Nginx 正常工作。

4.验证版本及编译参数

[root@Nginx ~]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin
[root@Nginx ~]# source ~/.bash_profile
[root@Nginx ~]# nginx -V
nginx version: nginx/1.24.0
built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --group=nginx --with-http_ssl_module --with-http_v2_module -
-with-http_realip_module --with-http_stub_status_module --withhttp_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --
with-stream_realip_module

编译了nginx环境变量就能够使用nginx命令

[root@Nginx ~]# nginx -v
nginx version: nginx/1.18.0
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 #
发送信号,reload信号 会生成新的worker,但master不会重新生成
-p prefix : set prefix path (default: /etc/nginx/) #指定Nginx 目录
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #
配置文件路径
-g directives : set global directives out of configuration file #设置全局指令,注意和
配置文件不要同时配置,否则冲突

示例
在这里插入图片描述

  • 22
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值