源码下载
安装wget并下载nginx源码包, 去 http://nginx.org/download/ 可以找到历史版本的包
[root@localhost ~]# yum install -y wget && wget http://nginx.org/download/nginx-1.17.9.tar.gz
新增用户
groupadd www
useradd -g www www
安装依赖环境
# pcre:rewrite模块依赖,zlib:gzip模块需要
[root@localhost ~]# yum install -y gcc pcre pcre-devel zlib zlib-devel
部分编译参数说明
参数 | 描述 |
---|---|
--prefix | 定义nginx安装路径 |
--sbin-path | 定义nginx可执行文件路径 |
--modules-path | nginx模块路径 |
--conf-path | nginx配置文件路径 |
--error-log-path、--http-log-path | 错误和访问日志路径 |
--with-openssl | 定义编译使用的openssl,默认为系统自带openssl |
--with-http_ssl_module | ssl模块 |
开始编译
[root@localhost ~]# ./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/usr/local/nginx/nginx.conf \
--error-log-path=/usr/local/nginx/error.log \
--http-log-path=/usr/local/nginx/access.log \
--pid-path=/usr/local/nginx/nginx.pid \
--lock-path=/usr/local/nginx/nginx.lock \
--http-client-body-temp-path=/usr/local/nginx/client_temp \
--http-proxy-temp-path=/usr/local/nginx/proxy_temp \
--http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp \
--http-scgi-temp-path=/usr/local/nginx/scgi_temp \
--user=www \
--group=www \
--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

上面的编译是使用了系统默认的openssl,如果想使用更高版本的openssl进行编译的话。可以加上--with-openssl=openssl安装路径
来自定义。高版本的openssl安装可以参考 这篇文章 .
--with-openssl
参数虽然可以指定 OpenSSL 路径,但只支持 OpenSSL 的源代码,不支持已编译好的 OpenSSL。每回更新 nginx 都要重新编译 OpenSSL 很麻烦 。以下是解决方案
打开nginx源码解压目录下的auto/lib/openssl/conf
,找到所有带.openssl
的行
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"
改成下面的样子,去掉.openssl/
CORE_INCS="$CORE_INCS $OPENSSL/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libcrypto.a"
进行编译
[root@localhost ~]# ./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/usr/local/nginx/nginx.conf \
--error-log-path=/usr/local/nginx/error.log \
--http-log-path=/usr/local/nginx/access.log \
--pid-path=/usr/local/nginx/nginx.pid \
--lock-path=/usr/local/nginx/nginx.lock \
--http-client-body-temp-path=/usr/local/nginx/client_temp \
--http-proxy-temp-path=/usr/local/nginx/proxy_temp \
--http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp \
--http-scgi-temp-path=/usr/local/nginx/scgi_temp \
--user=www \
--group=www \
--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-openssl=/usr/local/openssl \
--with-stream_ssl_preread_module

安装
[root@localhost ~]# make && make install
安装完成后,检查详细信息
# /usr/sbin/nginx是编译时--sbin-path定义的
[root@localhost ~]# /usr/sbin/nginx -V

总结
以上是nginx编译安装全过程。编译安装相比yum费时,但是可以很好的根据需求自行增加或去掉某些模块,有高度的定制性。但是正式使用yum安装的也能够满足需求,需要yum安装的可以看这篇文章
参考
https://www.sinosky.org/compile-nginx-with-a-custom-openssl-library.html
https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source
网友评论