推荐大家向 openresty 转型,我在编译过程中主要遇到以下 4 个小问题
[root@gop-sg-192-168-56-103 wangao]# tailf /var/log/nginx/error.log
# 问题 1
2019/11/04 11:59:56 [alert] 2749#2749: detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)
# 问题 2
2019/11/04 11:59:56 [alert] 2749#2749: failed to load the 'resty.core' module ( https://github.com/openresty/lua-resty-core); ensure you are using an OpenResty release from https://openresty.org/en/download.html (reason: module 'resty.core' not found:
no field package.preload['resty.core']
no file './resty/core.lua'
no file '/usr/local/share/luajit-2.0.5/resty/core.lua'
no file '/usr/local/share/lua/5.1/resty/core.lua'
no file '/usr/local/share/lua/5.1/resty/core/init.lua'
no file './resty/core.so'
no file '/usr/local/lib/lua/5.1/resty/core.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file './resty.so'
no file '/usr/local/lib/lua/5.1/resty.so'
no file '/usr/local/lib/lua/5.1/loadall.so') in /etc/nginx/nginx.conf:117
# 问题 3
[root@gop-sg-192-168-56-103 wangao]# systemctl status nginx
● nginx.service - The NGINX HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2019-08-19 01:36:46 +08; 2 months 17 days ago
Process: 1105 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 1071 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 1111 (nginx)
Tasks: 2
CGroup: /system.slice/nginx.service
├─1111 nginx: master process /usr/sbin/nginx
└─1112 nginx: worker process
Aug 19 01:36:46 gop-sg-192-168-56-103 systemd[1]: Starting The NGINX HTTP and reverse proxy server...
Aug 19 01:36:46 gop-sg-192-168-56-103 nginx[1071]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Aug 19 01:36:46 gop-sg-192-168-56-103 nginx[1071]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Aug 19 01:36:46 gop-sg-192-168-56-103 systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Aug 19 01:36:46 gop-sg-192-168-56-103 systemd[1]: Started The NGINX HTTP and reverse proxy server.
# 问题 4
nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
# check current os version and kernel
cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
uname -r
3.10.0-862.el7.x86_64
# yum
yum install -y gcc pam-devel git rpm-build pcre-devel openssl openssl-devel geoip-devel
# mkdir
mkdir -p /root/rpmbuild/SOURCES/
mkdir -p /root/rpmbuild/SPECS/
mkdir -p /root/rpmbuild/RPMS/noarch
# download openssl
cd /usr/local/src
wget https://github.com/openssl/openssl/archive/OpenSSL_1_0_2t.tar.gz
tar xf OpenSSL_1_0_2t.tar.gz
mv openssl-OpenSSL_1_0_2t/ openssl-1_0_2t
# install lua
sh luajit2.sh
# confirm these files are correct
[root@gop-sg-192-168-56-103 ~]# tree nginx-rpmbuild-centos7/
nginx-rpmbuild-centos7/
├── build.sh
├── conf_build
│ ├── conf
│ │ ├── django_fastcgi_params
│ │ ├── fastcgi.conf
│ │ ├── fastcgi_params
│ │ ├── koi-utf
│ │ ├── koi-win
│ │ ├── mime.types
│ │ ├── naxsi_core.rules
│ │ ├── nginx.conf
│ │ ├── scgi_params
│ │ ├── sites-available
│ │ │ └── 000_stub_status
│ │ ├── uwsgi_params
│ │ └── win-utf
│ ├── logrotate.d
│ │ └── nginx
│ ├── nginx.conf
│ └── nginx.service
├── luajit2.sh
├── luajit.sh
├── nginx-spec
└── nginx-template
├── conf
│ ├── django_fastcgi_params
│ ├── naxsi_core.rules
│ ├── nginx.conf
│ └── sites-available
│ └── 000_stub_status
├── logrotate.d
│ └── nginx
├── nginx.conf
└── nginx.service
8 directories, 26 files
# https://github.com/openresty/luajit2/releases
LUAVER="v2.1-20190912"
WKDIR="/root/rpmbuild/SOURCES"
cd $WKDIR
wget https://github.com/openresty/luajit2/archive/$LUAVER.tar.gz
tar zxf $LUAVER.tar.gz
rm -f $LUAVER.tar.gz
cd luajit2*
make BUILDMODE=static
make install
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.1
# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
# https://github.com/openresty/lua-nginx-module/issues/8
NGX_VER=1.16.1
BDDIR="/root/rpmbuild/BUILD"
WKDIR="/root/rpmbuild/SOURCES"
CURRENTDIR=`dirname $(readlink -f "$0")`
echo $CURRENTDIR
export LUAJIT_LIB=/usr/local/lib
# export LUAJIT_INC=/usr/local/include/luajit-2.0
export LUAJIT_INC=/usr/local/include/luajit-2.1
cd $BDDIR
rm -rf *
cd $WKDIR
rm -rf *
wget http://nginx.org/download/nginx-$NGX_VER.tar.gz
tar xzf nginx-$NGX_VER.tar.gz
rm -f nginx-$NGX_VER.tar.gz
mv nginx-$NGX_VER nginx-garena-$NGX_VER
cd nginx-garena-$NGX_VER/
mkdir -p contrib
cd contrib/
git clone git://github.com/openresty/headers-more-nginx-module.git
git clone git://github.com/openresty/echo-nginx-module.git
git clone git://github.com/simplresty/ngx_devel_kit.git
# git clone git://github.com/openresty/lua-nginx-module
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 lua-nginx-module
git clone git://github.com/nbs-system/naxsi.git
rm -rf */.git
rm -rf *.tar*
cd ..
cp -r $CURRENTDIR/nginx-template/* $WKDIR/nginx-garena-$NGX_VER/
cp $CURRENTDIR/nginx-spec /root/rpmbuild/SPECS/
# cp /root/rules $WKDIR/nginx-garena-$NGX_VER/debian/
cd $WKDIR
tar zcf nginx-garena-$NGX_VER.tar.gz nginx-garena-$NGX_VER/
cd /root/rpmbuild/SPECS/
rpmbuild -ba nginx-spec
cd /root/rpmbuild/RPMS/noarch
nginx-spec
Name: nginx-garena
Version: 1.16.1
Release: 0
Summary: nginx garena rpm
Source0: nginx-garena-%{version}.tar.gz
License: GPL
Group: Rahul
BuildArch: noarch
BuildRoot: %{_tmppath}/%{name}-buildroot
%description
Garena self-build Nginx.
%define _binaries_in_noarch_packages_terminate_build 0
%prep
%setup -q %{name}-%{version}
%build
CFLAGS="$RPM_OPT_FLAGS" ./configure --prefix=/usr/share/nginx/ \
--with-ld-opt="-Wl,-rpath,/usr/local/lib" \
--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.pid \
--lock-path=/var/lock/nginx.lock \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--with-pcre-jit \
--with-http_flv_module \
--with-http_mp4_module \
--with-file-aio \
--with-http_v2_module \
--with-stream \
--with-stream_ssl_module \
--with-http_auth_request_module \
--with-http_slice_module \
--with-threads \
--with-http_gunzip_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_geoip_module \
--with-http_ssl_module \
--with-openssl=/usr/local/src/openssl-1_0_2t \
--with-http_addition_module \
--with-http_geoip_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-ipv6 \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--add-module=contrib/headers-more-nginx-module \
--add-module=contrib/echo-nginx-module \
--add-module=contrib/ngx_devel_kit \
--add-module=contrib/lua-nginx-module \
--add-module=contrib/naxsi/naxsi_src
make -j8
%install
[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT
make DESTDIR=$RPM_BUILD_ROOT install
install -m 0755 -d $RPM_BUILD_ROOT/etc/nginx/sites-enabled
install -m 0755 -d $RPM_BUILD_ROOT/etc/nginx/sites-available
install -m 0755 -d $RPM_BUILD_ROOT/var/log/nginx
install -m 0755 -d $RPM_BUILD_ROOT/var/lib/nginx
install -D -m 644 conf/sites-available/000_stub_status $RPM_BUILD_ROOT/etc/nginx/sites-available/000_stub_status
install -D -m 644 conf/django_fastcgi_params $RPM_BUILD_ROOT/etc/nginx/django_fastcgi_params
install -D -m 644 conf/naxsi_core.rules $RPM_BUILD_ROOT/etc/nginx/naxsi_core.rules
install -D -m 644 conf/sites-available/000_stub_status $RPM_BUILD_ROOT/etc/nginx/sites-enabled/000_stub_status
install -D -m 644 logrotate.d/nginx $RPM_BUILD_ROOT/etc/logrotate.d/nginx
install -D -m 644 nginx.service $RPM_BUILD_ROOT/usr/lib/systemd/system/nginx.service
%clean
[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT
%post
useradd -s /sbin/nologin -d /var/www www-data
chown -R www-data.www-data /var/log/nginx /var/lib/nginx
systemctl enable nginx
echo %{name}-%{version} is successfully installed.
systemctl start nginx
%files
%defattr(-,root,root)
%dir /etc/nginx
/etc/nginx/*
%dir /usr/src/debug/nginx-garena-%{version}
/usr/src/debug/nginx-garena-%{version}/*
/usr/sbin/nginx
%dir /usr/share/nginx
/usr/share/nginx/*
/etc/logrotate.d/nginx
/usr/lib/systemd/system/nginx.service
/usr/lib/debug/*
/usr/lib/debug/.build-id/*
%dir /var/log/nginx
%dir /var/lib/nginx
%config(noreplace) /etc/nginx/nginx.conf
logrotate.d/nginx
/var/log/nginx/*.log /var/log/nginx/*/*.log{
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
[ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
endscript
}
nginx.conf
user www-data;
worker_processes auto;
#worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 655650;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 10240;
}
http {
# include /etc/nginx/naxsi_core.rules;
include mime.types;
default_type application/octet-stream;
log_format garena '$remote_addr - $remote_user [$time_iso8601] "$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $request_time $upstream_response_time "$http_x_forwarded_for" "$geoip_country_code" "$host"';
log_format garena_post '$remote_addr - $remote_user [$time_iso8601] "$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $request_time $upstream_response_time "$http_x_forwarded_for" "$geoip_country_code" "$host" "$request_body"';
log_format compact '$time_iso8601|$remote_addr|$geoip_country_code|$http_x_forwarded_for|$status|$request_time|$upstream_response_time|$request_length|$body_bytes_sent|$host|$request|$http_referer|$http_user_agent';
log_format compact_post '$time_iso8601|$remote_addr|$geoip_country_code|$http_x_forwarded_for|$status|$request_time|$upstream_response_time|$request_length|$body_bytes_sent|$host|$request|$http_referer|$http_user_agent|$request_body';
# access_log logs/access.log main;
sendfile on;
# tcp_nopush on;
keepalive_timeout 30;
fastcgi_keep_conn on;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_proxied any;
gzip_buffers 16 8k;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css application/json;
gzip_vary on;
include /etc/nginx/sites-enabled/*;
set_real_ip_from 10.0.0.0/8;
real_ip_header X-Forwarded-For;
# real_ip_recursive on;
# geoip_country /usr/share/GeoIP/GeoIP.dat;
server_tokens off; # returns "Server: nginx"
more_clear_headers Server; # doesn't return "Server: " header at all
}
nginx.service
https://www.nginx.com/resources/wiki/start/topics/examples/initscripts/
[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
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=process
KillSignal=SIGQUIT
TimeoutStopSec=5
PrivateTmp=true
[Install]
WantedBy=multi-user.target
sh build.sh
extracting debug info from /root/rpmbuild/BUILDROOT/nginx-garena-1.16.1-0.x86_64/usr/sbin/nginx
dwz: Too few files for multifile optimization
/usr/lib/rpm/sepdebugcrcfix: Updated 1 CRC32s, 0 CRC32s did match.
12776 blocks
+ /usr/lib/rpm/check-buildroot
+ /usr/lib/rpm/redhat/brp-compress
+ /usr/lib/rpm/redhat/brp-strip-static-archive /usr/bin/strip
+ /usr/lib/rpm/brp-python-bytecompile /usr/bin/python 1
+ /usr/lib/rpm/redhat/brp-python-hardlink
+ /usr/lib/rpm/redhat/brp-java-repack-jars
Processing files: nginx-garena-1.16.1-0.noarch
warning: File listed twice: /etc/nginx/nginx.conf
Provides: config(nginx-garena) = 1.16.1-0 nginx-garena = 1.16.1-0
Requires(interp): /bin/sh
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Requires(post): /bin/sh
Requires: libGeoIP.so.1()(64bit) libc.so.6()(64bit) libc.so.6(GLIBC_2.10)(64bit) libc.so.6(GLIBC_2.11)(64bit) libc.so.6(GLIBC_2.14)(64bit) libc.so.6(GLIBC_2.17)(64bit) libc.so.6(GLIBC_2.2.5)(64bit) libc.so.6(GLIBC_2.3)(64bit) libc.so.6(GLIBC_2.3.2)(64bit) libc.so.6(GLIBC_2.3.4)(64bit) libc.so.6(GLIBC_2.4)(64bit) libc.so.6(GLIBC_2.7)(64bit) libcrypt.so.1()(64bit) libcrypt.so.1(GLIBC_2.2.5)(64bit) libdl.so.2()(64bit) libdl.so.2(GLIBC_2.2.5)(64bit) libgcc_s.so.1()(64bit) libgcc_s.so.1(GCC_3.0)(64bit) libgcc_s.so.1(GCC_3.3)(64bit) libm.so.6()(64bit) libm.so.6(GLIBC_2.2.5)(64bit) libpcre.so.1()(64bit) libpthread.so.0()(64bit) libpthread.so.0(GLIBC_2.2.5)(64bit) libpthread.so.0(GLIBC_2.3.2)(64bit) libz.so.1()(64bit) rtld(GNU_HASH)
warning: Arch dependent binaries in noarch package
Checking for unpackaged file(s): /usr/lib/rpm/check-files /root/rpmbuild/BUILDROOT/nginx-garena-1.16.1-0.x86_64
Wrote: /root/rpmbuild/SRPMS/nginx-garena-1.16.1-0.src.rpm
Wrote: /root/rpmbuild/RPMS/noarch/nginx-garena-1.16.1-0.noarch.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.Qc7JbE
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd nginx-garena-1.16.1
+ '[' /root/rpmbuild/BUILDROOT/nginx-garena-1.16.1-0.x86_64 '!=' / ']'
+ rm -rf /root/rpmbuild/BUILDROOT/nginx-garena-1.16.1-0.x86_64
+ exit 0
[root@sg-gop-10-71-49-5 wangao]# nginx -V
nginx version: nginx/1.16.1
built with OpenSSL 1.0.2t 10 Sep 2019
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx/ --with-ld-opt=-Wl,-rpath,/usr/local/lib --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.pid --lock-path=/var/lock/nginx.lock --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-pcre-jit --with-http_flv_module --with-http_mp4_module --with-file-aio --with-http_v2_module --with-stream --with-stream_ssl_module --with-http_auth_request_module --with-http_slice_module --with-threads --with-http_gunzip_module --with-http_random_index_module --with-http_secure_link_module --with-http_geoip_module --with-http_ssl_module --with-openssl=/usr/local/src/openssl-1_0_2t --with-http_addition_module --with-http_geoip_module --with-http_gzip_static_module --with-http_realip_module --with-ipv6 --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --add-module=contrib/headers-more-nginx-module --add-module=contrib/echo-nginx-module --add-module=contrib/ngx_devel_kit --add-module=contrib/lua-nginx-module --add-module=contrib/naxsi/naxsi_src
# Prettier
https://serverfault.com/questions/223509/how-can-i-see-which-flags-nginx-was-compiled-with
[root@sg-gop-10-71-49-5 wangao]# 2>&1 nginx -V | xargs -n1
nginx
version:
nginx/1.16.1
built
with
OpenSSL
1.0.2t
10
Sep
2019
TLS
SNI
support
enabled
configure
arguments:
--prefix=/usr/share/nginx/
--with-ld-opt=-Wl,-rpath,/usr/local/lib
--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.pid
--lock-path=/var/lock/nginx.lock
--http-client-body-temp-path=/var/lib/nginx/body
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi
--http-proxy-temp-path=/var/lib/nginx/proxy
--http-scgi-temp-path=/var/lib/nginx/scgi
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi
--with-pcre-jit
--with-http_flv_module
--with-http_mp4_module
--with-file-aio
--with-http_v2_module
--with-stream
--with-stream_ssl_module
--with-http_auth_request_module
--with-http_slice_module
--with-threads
--with-http_gunzip_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_geoip_module
--with-http_ssl_module
--with-openssl=/usr/local/src/openssl-1_0_2t
--with-http_addition_module
--with-http_geoip_module
--with-http_gzip_static_module
--with-http_realip_module
--with-ipv6
--without-mail_pop3_module
--without-mail_imap_module
--without-mail_smtp_module
--add-module=contrib/headers-more-nginx-module
--add-module=contrib/echo-nginx-module
--add-module=contrib/ngx_devel_kit
--add-module=contrib/lua-nginx-module
--add-module=contrib/naxsi/naxsi_src
[root@sg-gop-10-71-49-5 wangao]# 2>&1 nginx -V | xargs -n1 | grep ssl
--with-stream_ssl_module
--with-http_ssl_module
--with-openssl=/usr/local/src/openssl-1_0_2t
[root@sg-gop-10-71-49-5 wangao]# 2>&1 nginx -V | xargs -n1 | grep lua
--add-module=contrib/lua-nginx-module
2019 年 11 月 04 日 - 更新 openresty/lua-nginx-module
2019 年 01 月 16 日 - 初稿
阅读原文 - https://wsgzao.github.io/post/rpmbuild/
重复的内容就不再发了,需要了解基础知识可以浏览原文