nginx源码编译

准备

下载nginx源码包

在这里插入图片描述

  • 这里我下载的是nginx-1.24.0.tar.gz版本
  • 下载完之后,我们使用mobaxtermnginx-1.24.0.tar.gz上传到我们的Linux服务器/root/目录下
[root@nginx ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg      vmset-rhel9-mountYum.sh
模板  图片  下载  桌面  nginx-1.24.0.tar.gz

关闭firewalld和selinux

[root@nginx ~]# systemctl is-active firewalld
inactive
[root@nginx ~]# getenforce
Disabled

安装依赖环境

[root@nginx ~]# yum install gcc gcc-g++ pcre-devel openssl-devel zlib-devel make -y

安装

解压

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

[root@nginx ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg  nginx-1.24.0.tar.gz
模板  图片  下载  桌面  nginx-1.24.0     vmset-rhel9-mountYum.sh

关闭nginxdebug功能

[root@nginx ~]# vim nginx-1.24.0/auto/cc/gcc

在这里插入图片描述

执行./configure进行环境检测

  • 先进入到解压好的nginx目录中
[root@nginx ~]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \   #--prefix表示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加密
  • configure用来进行环境检测
  • 检测通过会生成Makefile文件和objs目录
[root@nginx nginx-1.24.0]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
  • 如果想重新检测,可以使用make clean,他就会删除Makefileobjs
[root@nginx nginx-1.24.0]# make clean
rm -rf Makefile objs

添加nginx系统用户

  • 由于我们在./configure的时候,指定了user运行用户group运行组,所以我们要添加nginx用户
[root@nginx ~]# useradd -s /sbin/nologin -M nginx

使用make编译

[root@nginx nginx-1.24.0]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@nginx nginx-1.24.0]# make
  • 使用make执行编译后,objs目录里面就会出现编译后生成的文件,这些文件才是我们软件运行时真正需要的文件
  • 编译的过程nginx中的源文件系统中C语言的头文件进行对接
  • 编译完成会在objs目录下生成nginx的启动脚本nginx所需要的模块。其中启动脚本为nginx,模块放在objs/src目录下

在这里插入图片描述

  • 当然,我们在make编译时也可以使用-j指定cpu的核心数提高编译速率
[root@nginx nginx-1.24.0]# make -j2   #表示使用2个cpu核心(处理器),不加j2默认使用一个核心

使用make install安装

  • make install本质:将objs目录中的文件复制到prefix指定的目录中
[root@nginx nginx-1.24.0]# make install

进入到prefix指定的目录中查看

[root@nginx ~]# cd /usr/local/nginx/
[root@nginx nginx]# ls
conf  html  logs  sbin

启动nginx服务

[root@nginx sbin]# ./nginx
[root@nginx sbin]# ps -aux | grep nginx
avahi        771  0.0  0.1  15524  6280 ?        Ss   00:43   0:00 avahi-daemon: running [nginx.                      local]
root       39425  0.0  0.0   9832   928 ?        Ss   01:33   0:00 nginx: master process ./nginx
nginx      39426  0.0  0.1  13720  4572 ?        S    01:33   0:00 nginx: worker process
root       39428  0.0  0.0 221812  2320 pts/0    S+   01:33   0:00 grep --color=auto nginx

关闭nginx

[root@nginx sbin]# ./nginx -s stop
[root@nginx sbin]# ps -aux | grep nginx
root       39437  0.0  0.0 221812  2316 pts/0    S+   01:35   0:00 grep --color=auto nginx

添加nginx环境变量

[root@nginx ~]# vim ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
export PATH=$PATH:/usr/local/nginx/sbin   #在最后一行添加
[root@nginx ~]# source ~/.bash_profile   #重新加载
[root@nginx ~]# echo $PATH
/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/nginx/sbin

卸载nginx

[root@nginx ~]# rm -rf /usr/local/nginx/
[root@nginx ~]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# make clean
rm -rf Makefile objs
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

^~^前行者~~~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值