学堂 学堂 学堂公众号手机端

nginx源码编译安装

lewis 5年前 (2020-08-09) 阅读数 6 #技术
一、环境信息

ubuntu 20.04

编译环境 gcc make perl(编译openssl需要)

二、软件信息

nginx-1.22.0 openssl-1.1.1q pcre2-10.40 zlib-1.2.12


三、编译安装

openssl 编译生成静态链接文件

生成 make 文件

root@26be474b2382:/data/build/openssl-1.1.1q# ./config --prefix=/data/build/openssl-1.1.1q --openssldir=/data/build/openssl-1.1.1q

执行 make 生成静态链接文件 ( -j 指定线程数)

root@26be474b2382:/data/build/openssl-1.1.1q# make -j 8

执行成功,生成 libssl.a libcrypto.a 链接文件 即可 用于nginx 开启 ssl 模块

不必执行 make install 不做安装,最后要删除

编译安装 nginx

创建允许用户和组 useradd -M -s /sbin/nologin nginx (不需要家目录,不登录 )

root@26be474b2382:/data/build/nginx-1.22.0# useradd -M -s /sbin/nologin nginx

修改 nginx-1.22.0/auto/lib/openssl/conf 文件

CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"

替换为

CORE_INCS="$CORE_INCS $OPENSSL/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/libcrypto.a"

编译命令

配置软件目标位置及开启模块,具体参数参考 nginx 文档 https://nginx.org/en/docs/con...

root@26be474b2382:/data/build/nginx-1.22.0# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-pcre=/data/build/pcre2-10.40 --with-openssl=/data/build/openssl-1.1.1q --with-zlib=/data/build/zlib-1.2.12

执行成功

执行 make 生成 nginx 软件及配置文件

root@26be474b2382:/data/build/nginx-1.22.0# make -j 8

执行 make install 将 nginx 软件及配置文件 安装到指定位置

root@26be474b2382:/data/build/nginx-1.22.0# make install

创建指定的缓存目录 /var/cache/nginx 将其授予 nginx:nginx 用户

root@26be474b2382:/data/build/nginx-1.22.0# mkdir /var/cache/nginx
root@26be474b2382:/data/build/nginx-1.22.0# chown nginx:nginx /var/cache/nginx
四、验证nginx 安装情况
root@26be474b2382:~# nginx -t

至此 nginx 安装完毕, /data/build 文件夹 可以删除

版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门