源代码编译安装Nginx
文章目录
源代码编译安装Nginx@[toc]前言Nginx的常用编译选项通用配置选项配置优化选项HTTP配置选项HTTP模块配置选项邮件代理配置选项反向代理配置选项其它配置选项
编译安装Nginx准备编译环境开始源代码编译将编译好的Nginx设置为系统服务CentOS 6CentOS 7
编译Nginx支持第三方模块添加对Lua的支持
前言
Nginx是一个免费、开源、高性能的HTTP服务和反向代理软件,也是一个IMAP/POP3代理服务,是为数不多解决了C10K问题的HTTP服务器之一。Nginx以其高性能、高并发、高度模块化、低内存消耗等特点闻名,具有多种Web服务器功能特性,如:负载均衡、缓存、访问控制、反向代理以及高效整合各种应用的能力,这些特性使得Nginx很适合于现代互联网架构。
Nginx作为一款优秀的开源软件,安装方法主要有以下两种,接下来介绍的是编译安装Nginx的方法;
使用包管理器进行安装
sudo apt-get install nginx #基于deb包
sudo yum install nginx #基于rpm包
使用源代码编译安装
cd ./build/nginx-<version-number> && ./configure
make && sudo make install
Nginx的常用编译选项
Nginx的编译选项众多,通过Nginx编译脚本的帮助选项,可以查看支持的所有编译选项,并且针对给出的每一个选项,都做了简单的介绍,通过下面的命令可以来了解一下。
# wget http://nginx.org/download/nginx-1.12.2.tar.gz #通过wget将源码包下载到本地
# tar xvf nginx-1.12.2.tar.gz
# cd nginx-1.12.2
# ./configure --help #可以查看到所有支持的编译选项,并有简单的使用介绍
由于篇幅原因不在这里对所有的选项都做说明,接下来我会参考RPM包中Nginx的编译选项,来对一些主要的编译选项做些介绍,使用nginx -V命令可以查看RPM包中Nginx使用的编译选项都有哪些;
通用配置选项
选项说明
–prefix=PATHNginx安装的根路径,其它安装选项不明确指定安装路径,默认安装在此路径下–sbin-path=PATH指定Nginx二进制文件的路径,如果没有指定,默认使用–prefix选项指定的路径–modules-path=PATH指定模块文件放置的路径–conf-path=PATH命令行未明确指定配置文件时,使用的配置文件所在路径–error-log-path=PATH指定错误日志的路径–pid-path=PATH指定写入nginx master进程pid的文件,通常在/var/run/下–lock-path=PATH共享存储器互斥锁文件的路径–user=USER运行worker进程的用户–group=GROUP运行worker进程的组–with-file-aio为Linux 2.6.22+ 系统启用异步I/O–with-debug这个选项用于启用调试日志,在生产环境的系统中不推荐使用该选项
配置优化选项
选项说明
–with-cc-opt=OPTIONS设置将被添加到CFLAGS变量的附加参数–with-ld-opt=OPTIONS设置将在链接期间使用的额外参数
HTTP配置选项
选项说明
–http-log-path=PATHhttp访问日志的默认路径–http-client-body-temp-path=PATH设置临时存储http客户端请求报文的目录路径–http-proxy-temp-path=PATH设置使用代理后存放临时文件的路径–http-fastcgi-temp-path=PATH设置fastcgi后存放临时文件的路径–http-uwsgi-temp-path=PATH设置uwsgi后存放临时文件的路径–http-scgi-temp-path=PATH设置scgi后存放临时文件的路径
HTTP模块配置选项
选项说明
–with-http_auth_request_module启用构建ngx_http_auth_request_module模块,该模块基于子请求的结果实现客户端授权。默认情况下不生成此模块。–with-http_ssl_module使用https协议进行通信,对通信的流量进行加密,需要依赖OpenSSL库。–with-http_v2_module构建一个支持http协议2.0版本的模块,该模块默认是不生成的。–with-http_realip_moduleNginx在七层负载均衡器或者其它设备之后时,启用此模块,可以获取调度器转发的请求报文首部中真实客户端的IP。–with-http_addition_module这是一个作为输出过滤器的模块,能够在一个请求经过location前或后时在该location自身添加内容。–with-http_xslt_module=dynamic该模块用于处理XML响应转换,基于一个或多个XSLT格式,需要依赖libxml2和libxslt库。–with-http_image_filter_module=dynamic该模块作为图像过滤器使用,在将图像发送给客户之前进行处理,需要依赖libgd库。–with-http_geoip_module=dynamic使用该模块能,可以设置各种变量在配置文件中的区段使用,能够基于客户端的ip地址查找代理位置,需要依赖MaxMind GeoIP库和相应的预编译数据库文件。–with-http_sub_module该模块实现替代过滤,在响应中用一个字符串替代另一个字符串,Note:使用该模块隐式禁用标头缓存。–with-http_dav_module启用这个模块将激活使用WebDAV的配置指令,Note:建议该模块只有在需要时才进行启用,如果配置不正确可能会带来安全隐患。–with-http_flv_module如果需要提供Flash流媒体视频文件,需要启用该模块提供伪流媒体。–with-http_mp4_module该模块支持H.264/AA文件伪流媒体–with-http_gunzip_module对于不支持gzip编码的客户,该模块用于为客户解压缩预压缩内容。–with-http_gzip_static_module当被调用的资源是没有.gz结尾格式的文件时,如果想支持发送预压缩版本的静态文件,可以使用该模块。–with-http_random_index_module启用这个模块可以提供从一个目录中随机选择文件的索引文件。–with-http_secure_link_module该模块提供了一种机制,会将一个散列值链接到一个URL中,只有使用正确的密码才能计算出链接。–with-http_degradation_module可以构建ngx_http_degradation_module模块,默认情况下,此模块是不构建的。–with-http_slice_module构建将请求拆分为子请求的ngx_http_slice_module模块,每个子请求可以返回一定范围的响应,该模块为大的响应提供了更有效的缓存,默认情况下,此模块是不构建的。–with-http_stub_status_module启用这个模块后会收集Nginx自身的状态信息–with-http_perl_module=dynamic构建嵌入式Perl模块,该模块默认不生成。
邮件代理配置选项
选项说明
–with-mail=dynamic动态启用POP3/IMAP4/SMTP代理模块–with-mail_ssl_module构建一个支持SSL/TLS协议的邮件代理服务模块,该模块默认不生成,构建和运行需要依赖OpenSSL库。
反向代理配置选项
选项说明
–with-stream=dynamic动态启用构建stream module模块,作为通用的TCP/UDP代理和负载均衡流模块,该模块默认不构建。–with-stream_ssl_module构建一个支持SSL/TLS协议的stream模块,该模块默认不生成,构建和运行需要依赖OpenSSL库。
其它配置选项
选项说明
–with-ipv6启用IPv6支持,在1.11.x后的版本编译时,已经不需要该编译选项–with-pcre强制使用PCRE库–with-pcre-jit使用即时编译构建PCRE库,支持1.1.12 pcre_jit指令–with-google_perftools_module启用构建ngx_google_perftools_module模块,该模块可使用Google Performance Tools对nginx工作进程进行分析,该模块专供nginx开发人员使用,默认不会生成。
编译安装Nginx
编译Nginx的最大好处是有很大的灵活性,安装位置和需要启用的功能模块都可以根据你的需求进行自定义,一些优化相关的功能是否启用,以及启用后使用的参数也是可以人为指定的。
源码编译Nginx对系统环境是有要求的,需要系统满足某些必要的编译条件,从最基本的gcc编译器到nginx编译要启用的功能依赖到的开发库等,这些开发环境是编译的前提,缺一不可。
Nginx源码包可以到Nginx官网下载:http://nginx.org/en/download.html
准备编译环境
//安装编译过程中需要依赖到的软件包
# yum -y groupinstall "Development Tools"
# yum -y install pcre-devel openssl-devel zlib-devel libxslt-devel gd-devel perl-ExtUtils-Embed perl-devel GeoIP-devel GeoIP-data gperftools-devel
//创建运行Nginx worker进程的用户和组
# useradd -r -s /sbin/nologin nginx
# id nginx
//将Nginx的源码包文件下载并解压到指定目录
# wget http://nginx.org/download/nginx-1.12.2.tar.gz #通过wget将源码包下载到本地
# tar xvf nginx-1.12.2.tar.gz -C /usr/local/src/
# cd /usr/local/src/nginx-1.12.2 #编译过程需要在解压后生成的目录中进行
开始源代码编译
//编译过程中可以使用--with-<module-name>_module选项启用默认没有被安装的模块,也可以使用--without-<module-name>_module选项禁用默认安装的模块;
//下面是参考RPM包的安装选项进行的编译安装
# ./configure --prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body \
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi \
--pid-path=/run/nginx.pid \
--lock-path=/run/lock/subsys/nginx \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-ipv6 \
--with-http_auth_request_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_perl_module=dynamic \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-pcre \
--with-pcre-jit \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-google_perftools_module \
--with-debug \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' \
--with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
# make && make install
将编译好的Nginx设置为系统服务
CentOS 6
//准备服务脚本
# vim /etc/rc.d/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
sysconfig="/etc/sysconfig/$prog"
lockfile="/var/lock/subsys/nginx"
pidfile="/var/run/${prog}.pid"
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f $sysconfig ] && . $sysconfig
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $pidfile $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest_q || return 6
stop
start
}
reload() {
configtest_q || return 6
echo -n $"Reloading $prog: "
killproc -p $pidfile $prog -HUP
echo
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
configtest_q() {
$nginx -t -q -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
# Upgrade the binary with no downtime.
upgrade() {
local oldbin_pidfile="${pidfile}.oldbin"
configtest_q || return 6
echo -n $"Upgrading $prog: "
killproc -p $pidfile $prog -USR2
retval=$?
sleep 1
if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then
killproc -p $oldbin_pidfile $prog -QUIT
success $"$prog online upgrade"
echo
return 0
else
failure $"$prog online upgrade"
echo
return 1
fi
}
# Tell nginx to reopen logs
reopen_logs() {
configtest_q || return 6
echo -n $"Reopening $prog logs: "
killproc -p $pidfile $prog -USR1
retval=$?
echo
return $retval
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest|reopen_logs)
$1
;;
force-reload|upgrade)
rh_status_q || exit 7
upgrade
;;
reload)
rh_status_q || exit 7
$1
;;
status|status_q)
rh_$1
;;
condrestart|try-restart)
rh_status_q || exit 7
restart
;;
*)
echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
exit 2
esac
//将Nginx添加为系统服务,如果安装的位置不在PATH路径之下,需要进行一下添加;
# chmod +x /etc/rc.d/init.d/nginx
# chkconfig --add nginx
# chkconfig nginx on
# service nginx start
CentOS 7
//准备Nginx服务的unit文件
# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
//将Nginx设置为系统服务
# systemctl daemon-reload #重载系统管理配置
# systemctl enable nginx
# systemctl start nginx
编译Nginx支持第三方模块
安装第三模块的一般流程:
找到您要使用的第三方模块将源码包下载到本地准备编译环境解压源码包阅读README文件使用./configure --add-module=<path>选项配置使用此模块make && make install
添加对Lua的支持
# wget http://nginx.org/download/nginx-1.12.2.tar.gz
# wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
# wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz
# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.12.tar.gz
# yum -y groupinstall "Development Tools"
# yum -y install pcre-devel openssl-devel
# tar xvf nginx-1.12.2.tar.gz -C /usr/local/src/
# tar xvf LuaJIT-2.0.5.tar.gz -C /usr/local/src/
# tar xvf v0.3.0.tar.gz -C /usr/local/src/nginx-1.12.2/
# tar xvf v0.10.12.tar.gz -C /usr/local/src/nginx-1.12.2/
# cd /usr/local/src/LuaJIT-2.0.5/
# make PREFIX=/usr/luajit
# make install PREFIX=/usr/luajit
# export LUAJIT_LIB=/usr/luajit/lib
# export LUAJIT_INC=/usr/luajit/include/luajit-2.0
# cd /usr/local/src/nginx-1.12.2/
# useradd -r -s /sbin/nologin nginx
# ./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--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/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre \
--with-debug \
--with-ld-opt="-Wl,-rpath,/usr/luajit/lib" \
--add-module=/usr/local/src/nginx-1.12.2/ngx_devel_kit-0.3.0 \
--add-module=/usr/local/src/nginx-1.12.2/lua-nginx-module-0.10.12
# make -j 2 && make install