#!/usr/bin/env bash
#
# 编译成功的前提是openssl能正确编译
# test on centos7@2018-06-29
#
export BUILD_DIR=/tmp/nginx-static-openssl/build
export VERSION_NGINX=nginx-1.14.0
export VERSION_OPENSSL=openssl-1.1.1-pre8
export SOURCE_NGINX=http://nginx.org/download/
export SOURCE_OPENSSL=https://www.openssl.org/source/
function setup() {
rm -rf ${BUILD_DIR}
mkdir -p ${BUILD_DIR}
if [ -n "$(command -v yum)" ]; then yum -y groupinstall "Development Tools" && yum -y install libaio-devel libaio perl-CGI cpan perl-Module-Load-Conditional perl-core; fi
if [ -n "$(command -v apt-get)" ]; then apt-get install build-essential; fi
}
function download_sources() {
echo "Download sources"
pushd ${BUILD_DIR}
curl --remote-name "${SOURCE_NGINX}${VERSION_NGINX}.tar.gz"
curl --remote-name "${SOURCE_OPENSSL}${VERSION_OPENSSL}.tar.gz"
popd
}
function extract_sources() {
echo "Extracting sources"
pushd ${BUILD_DIR}
tar -xf "${VERSION_NGINX}.tar.gz"
tar -xf "${VERSION_OPENSSL}.tar.gz"
popd
}
function compile_nginx() {
echo "Configure & Build nginx"
pushd "${BUILD_DIR}/${VERSION_NGINX}"
make clean
./configure \
--prefix=/etc/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 --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-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-openssl-opt=enable-tls1_3 --with-openssl=${BUILD_DIR}/${VERSION_OPENSSL}
make -j1
popd
}
echo "Building ${VERSION_NGINX} with static ${VERSION_OPENSSL}"
setup && download_sources && extract_sources && compile_nginx
retval=$?
echo ""
if [ $retval -eq 0 ]; then
echo "Your nginx binary is located at ${BUILD_DIR}/${VERSION_NGINX}/objs/nginx"
echo "Listing dynamically linked libraries ..."
ldd ${BUILD_DIR}/${VERSION_NGINX}/objs/nginx
echo ""
${BUILD_DIR}/${VERSION_NGINX}/objs/nginx -V
else
echo "Ooops, build failed. Check output!"
fi