Ubuntu之源码编译安装nginx

参考:Ubuntu之源码编译安装nginx_ubuntu编译安装nginx-CSDN博客

1.下载源码后进入源码目录,如下:

cd /home/jq/wf/nginx-1.26.1

2.下载相应依赖库:

apt-get install libpcre3-dev

apt-get install openssl libssl-dev

apt-get install zlib1g-dev

3.运行配置命令:(--prefix指定make install 后的目录,也就是nginx生成目录)

./configure --with-http_ssl_module --with-debug --prefix=/home/jq/wf/nginx-1.26.1/bin

打印如下:

...

checking for OpenSSL library ... found

checking for zlib library ... found

creating objs/Makefile

Configuration summary

  • using system PCRE2 library

  • using system OpenSSL library

  • using system zlib library

nginx path prefix: "/"

nginx binary file: "//sbin/nginx"

nginx modules path: "//modules"

nginx configuration prefix: "//conf"

nginx configuration file: "//conf/nginx.conf"

nginx pid file: "//logs/nginx.pid"

nginx error log file: "//logs/error.log"

nginx http access log file: "//logs/access.log"

nginx http client request body temporary files: "client_body_temp"

nginx http proxy temporary files: "proxy_temp"

nginx http fastcgi temporary files: "fastcgi_temp"

nginx http uwsgi temporary files: "uwsgi_temp"

nginx http scgi temporary files: "scgi_temp"

4.运行make命令: make

打印如下:

...

objs/src/http/modules/ngx_http_upstream_zone_module.o \

objs/ngx_modules.o \

-ldl -lpthread -lcrypt -lpcre2-8 -lssl -lcrypto -ldl -lpthread -lz \

-Wl,-E

sed -e "s|%%PREFIX%%|/home/jq/wf/nginx-1.26.1/bin|" \

-e "s|%%PID_PATH%%|/home/jq/wf/nginx-1.26.1/bin/logs/nginx.pid|" \

-e "s|%%CONF_PATH%%|/home/jq/wf/nginx-1.26.1/bin/conf/nginx.conf|" \

-e "s|%%ERROR_LOG_PATH%%|/home/jq/wf/nginx-1.26.1/bin/logs/error.log|" \

< man/nginx.8 > objs/nginx.8

make1: Leaving directory '/home/jq/wf/nginx-1.26.1'

sed后面的路径那里就是编译出来的目录

5.运行make install 安装命令: make install

到/home/jq/wf/nginx-1.26.1目录下,发现生成了bin文件夹,再进入bin文件夹看到如下:

进入sbin目录,就是生成的nginx文件

输入: ./nginx,运行nginx后,输入ps aux | grep 'nginx',可以看到已经有nginx进程

在浏览器中输入http://127.0.0.1/

6.如果有自定义的handler和自定义的filter,则需要在源码中找个地方放置handler和filter模块,比如在源码的根目录下的objs下,创建一个目录,用于放置这2个模块代码

additon_filter目录下是这样:

config里面的配置文件内容:

ngx_addon_name=ngx_http_encrypt_filter_module

HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_encrypt_filter_module"

NGX_ADDON_SRCS="NGX_ADDON_SRCS ngx_addon_dir/ngx_http_encrypt_filter_module.c"

addition_module下的目录是这样:

config里面的内容:

ngx_addon_name=ngx_http_encrypt_module

HTTP_MODULES="$HTTP_MODULES ngx_http_encrypt_module"

NGX_ADDON_SRCS="NGX_ADDON_SRCS ngx_addon_dir/ngx_http_encrypt_module.c"

然后再在编译时的./configure命令中添加项 --add-module,如下:

./configure --with-http_ssl_module --with-debug --prefix=/home/jq/wf/nginx-1.26.1/bin \

--add-module="objs/addtion_module/addition_filter objs/addtion_module/addition_module"

就可以运行以上配置命令了。

7.如果还有自己定义的.h和.c文件,如需要用到cjson库的cJson.h和cJson.c,还有自定义的.h和.c文件,如test.h和test.c,需要将cJson.h和cJson.c放到源码的core目录下,如果test.h/.c里面没有用到nginx其他模块的api,也可以放到core目录下,如果用到了其他模块,比如nginx http的api,比如我的就用到了#include <ngx_http.h>,那么就将test.h/.c放到http目录下,然后修改源码的auto目录的sources,将cjson.h 文件加到CORE_DEPS项,将cjson.c文件添加到CORE_SRCS项;修改auto/modules文件将test.h添加到http的ngx_module_deps项, test.c添加到ngx_module_srcs项,即ngx_module_type=HTTP下面的部分里面添加。如下的modules文件位置:

修改完后,就可以运行./confiure命令和make,make install命令了。

相关推荐
aixingkong9211 小时前
Nvidia为例 XPU的Multi-Host、Socket Direct 、GPUDirect、RDMA 技术点分析
服务器·网络·硬件架构·硬件工程
百万蹄蹄向前冲9 小时前
双端同步!云服务器装最新Node.js v26.10全过程追踪
服务器·人工智能·node.js
春天花会开13110 小时前
内网 HTTPS 部署实战:私有 CA + Nginx 反向代理全流程复盘
运维·nginx·https
虎头金猫6 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
还是大剑师兰特6 天前
解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404
nginx·大剑师
xing-xing6 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
-梅6 天前
linux(8) 软硬链接
linux·运维·服务器
Wang's Blog6 天前
Java 项目实战: 外卖平台-文件下载与ServletOutputStream回写浏览器
服务器·项目开发
琥珀色糖6 天前
SimlpeHttp
linux·服务器
第十人i6 天前
Linux 日志管理系统:journald 配置与优化
linux·服务器