安装依赖软件
CentOS 7
yum -y install epel-release
yum update
yum -y install lua-devel patch libtool libtool-ltdl libtool-ltdl-devel libxslt-devel libxslt libxml2 libxml2-devel gd-devel gd GeoIP GeoIP-devel GeoIP-data gperftools gcc gcc-c++ zlib-devel zlib make cmake autoconf openssl openssl-devel libwebp-devel libwebp libvpx perl-devel perl-ExtUtils-Embed autoconf automake gnome-common
Ubuntu
apt-get update
apt-get -y install libgd-dev build-essential zlib1g-dev libpcre3 libpcre3-dev libssl-dev libxslt1-dev libxml2-dev libgeoip-dev libgoogle-perftools-dev libperl-dev libtool openssl gcc g++ google-perftools
PCRE
wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz
tar -xf pcre-8.41.tar.gz -C /usr/local/ && cd /usr/local/pcre-8.41
./configure --enable-utf8 ; echo $? // --enable-utf8:对utf-8的支持
make ; echo $?
make install ; echo $?
openssl
wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1.tar.gz
tar -xf OpenSSL_1_1_1.tar.gz && mv openssl-OpenSSL_1_1_1 /usr/local/openssl-1.1.1
创建 Nginx 运行用户
groupadd www
useradd www -g www -s /sbin/nologin -M
官网下载 Nginx 源码包
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -xf nginx-1.16.1.tar.gz && cd nginx-1.16*
扩展模块 1---- headers
git clone https://github.com/openresty/headers-more-nginx-module.git /usr/local/ngx_modules/ngx_headers
扩展模块 2 ---- echo
git clone https://github.com/openresty/echo-nginx-module.git /usr/local/ngx_modules/ngx_echo
扩展模块 3 ---- brotli
git clone https://github.com/google/ngx_brotli.git /usr/local/ngx_modules/ngx_brotli
扩展模块 4 ---- cloudflare-zlib
git clone https://github.com/cloudflare/zlib.git /usr/local/ngx_modules/cloudflare_zlib
扩展模块 5 ---- sticky
wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
tar xf master.tar.gz
mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 /usr/local/ngx_modules/ngx_sticky
扩展模块 6 ---- luajit
wget https://github.com/openresty/luajit2/archive/v2.1-20200102.tar.gz
tar xf v2.1-20200102.tar.gz && mv luajit2-2.1-20200102 /usr/local/ngx_modules/luajit2-2.1
cd /usr/local/ngx_modules/luajit2-2.1
make && make install
echo '/usr/local/lib' >> /etc/ld.so.conf.d/local.conf
export LUA_INCLUDE_DIR=/usr/local/include/luajit-2.1
ldconfig
扩展模块 7 ---- lua-cjson
wget https://github.com/openresty/lua-cjson/archive/2.1.0.7.tar.gz
tar xf 2.1.0.7.tar.gz && mv lua-cjson-2.1.0.7 /usr/local/ngx_modules/
cd /usr/local/ngx_modules/lua-cjson-2.1.0.7
make && make install
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.1
扩展模块 8 ---- ngx_devel_kit
wget https://github.com/vision5/ngx_devel_kit/archive/v0.3.1.tar.gz
tar -xf v0.3.1.tar.gz && mv ngx_devel_kit-0.3.1 /usr/local/ngx_modules/
扩展模块 9 ---- lua-nginx
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.14.tar.gz
tar -xf v0.10.14.tar.gz && mv lua-nginx-module-0.10.14 /usr/local/ngx_modules/
为了方便以后重新编译Nginx,将lua变量写入/etc/profile 中
#Lua
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.1
export LUA_INCLUDE_DIR=/usr/local/include/luajit-2.1
扩展模块 10 ---- TCMalloc;Ubuntu 系统可以直接忽略该步骤,以上apt中已安装
# 先编译安装libunwind
wget http://mirror.yongbok.net/nongnu/libunwind/libunwind-1.1.tar.gz
tar -xf libunwind-1.1.tar.gz && cd libunwind-1.1
CFLAGS=-fPIC ./configure
make CFLAGS=-fPIC
make CFLAGS=-fPIC install
wget https://github.com/gperftools/gperftools/archive/gperftools-2.7.tar.gz
tar -xf gperftools-2.7.tar.gz && cd gperftools-gperftools-2.7/
./autogen.sh
./configure --enable-frame-pointers
make && make install ; echo $?
# 编译时添加 "--with-google_perftools_module"
# 将TCMalloc库加载到Linux系统中
echo '/usr/local/lib' >> /etc/ld.so.conf.d/local.conf
ldconfig
编译 Nginx
./configure \
--add-module=/usr/local/ngx_modules/ngx_echo \
--add-module=/usr/local/ngx_modules/ngx_brotli \
--add-module=/usr/local/ngx_modules/ngx_concat \
--add-module=/usr/local/ngx_modules/ngx_headers_info \
--add-module=/usr/local/ngx_modules/ngx_sticky_goodies \
--add-module=/usr/local/ngx_modules/ngx_devel_kit-0.3.1 \
--add-module=/usr/local/ngx_modules/lua-nginx-module-0.10.14 \
--user=www \
--group=www \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_dav_module \
--with-http_sub_module \
--with-http_slice_module \
--with-http_geoip_module \
--with-http_realip_module \
--with-http_gunzip_module \
--with-http_addition_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_degradation_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_random_index_module \
--with-http_perl_module=dynamic \
--with-http_xslt_module=dynamic \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-stream_geoip_module \
--with-debug \
--with-compat \
--with-threads \
--with-file-aio \
--with-google_perftools_module \
--with-ld-opt=-ltcmalloc_minimal \
--with-zlib=/usr/local/ngx_modules/Cloudflare-zlib \
--with-pcre-jit \
--with-pcre-opt=-fPIC \
--with-pcre=/usr/local/pcre-8.41 \
--with-openssl=/usr/local/openssl-1.1.1 \
--with-openssl-opt='enable-tls1_3 -fPIC' \
--without-poll_module \
--without-select_module \
--without-http_scgi_module \
--without-http_uwsgi_module \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/data/nginx/PID/nginx.pid \
--lock-path=/data/nginx/PID/nginx.lock \
--http-log-path=/data/nginx/logs/access.log \
--error-log-path=/data/nginx/logs/error.log \
--http-scgi-temp-path=/data/nginx/tmps/scgi \
--http-proxy-temp-path=/data/nginx/tmps/proxy \
--http-uwsgi-temp-path=/data/nginx/tmps/uwsgi \
--http-fastcgi-temp-path=/data/nginx/tmps/fastcgi \
--http-client-body-temp-path=/data/nginx/tmps/client_body ; echo $?
make -j 2 ; echo $?
make install ; echo $?
创建目录
mkdir -pv /data/nginx/{tcmalloc,logs/hack,PID,proxy_cache,proxy_temp,tmp/client_body,tmp/fastcgi}
chown -R www.www /data/
chmod 0777 /data/nginx/tcmalloc
创建 systemd 服务文件
# CentOS
/usr/lib/systemd/system/nginx.service
# Ubuntu
/lib/systemd/system/nginx.service
——————————————————————————————————————————————————————————————————————————————————————————
tee /usr/lib/systemd/system/nginx.service <<-'EOF'
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/data/nginx/PID/nginx.pid
ExecStartPre=/bin/rm -f /data/nginx/PID/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# 注意事项
# PIDFile:要和编译时的配置保持一致
# rm:which rm
管理
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx
systemctl force-reload nginx
systemctl status nginx
指定动态模块路径
--modules-path=/usr/local/ngx_modules/dynamic