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

make[1]: 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命令了。

相关推荐
bantinghy25 分钟前
Linux进程单例模式运行
linux·服务器·单例模式
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos
AWS官方合作商9 天前
AWS ACM 重磅上线:公有 SSL/TLS 证书现可导出,突破 AWS 边界! (突出新功能的重要性和突破性)
服务器·https·ssl·aws
算法练习生9 天前
Linux文件元信息完全指南:权限、链接与时间属性
linux·运维·服务器
忘了ʷºᵇₐ9 天前
Linux系统能ping通ip但无法ping通域名的解决方法
linux·服务器·tcp/ip
weixin_425023009 天前
Spring Boot使用MCP服务器
服务器·spring boot·后端·spring ai·mcp
小生云木9 天前
Linux离线编译安装nginx
linux·运维·nginx
qq_200337399 天前
python输出值,写入test.yaml文件
java·服务器·前端
成工小白9 天前
【Linux】文件系统
linux·运维·服务器
PHP武器库9 天前
[Hestia]开源网络服务器控制面板,快速、可靠、开源
运维·服务器·网络·开源·php