Skip to content

Instantly share code, notes, and snippets.

@mpstein
Created June 16, 2020 15:26
Show Gist options
  • Save mpstein/451e1f2dbcc34afb75a2332a51167be8 to your computer and use it in GitHub Desktop.
Save mpstein/451e1f2dbcc34afb75a2332a51167be8 to your computer and use it in GitHub Desktop.
Fetch, build, and install the latest nginx with the latest OpenSSL for RaspberryPi with optional TLS1.3 support
#!/usr/bin/env bash
#From @geekmuse
## names of latest versions of each package
VERSION_PCRE=pcre-8.43
VERSION_OPENSSL=OpenSSL_1_1_1c
VERSION_NGINX=nginx-1.17.1
SOURCE_PCRE=ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
SOURCE_OPENSSL=https://www.openssl.org/source/
SOURCE_NGINX=http://nginx.org/download/
TLS13="false"
if [ ! -z "${1}" ]; then
TLS13="${1}"
fi
if [[ "${TLS13}" == "true" ]]; then
VERSION_OPENSSL=tls1.3-draft-19
SOURCE_OPENSSL=https://github.com/openssl/openssl.git
fi
if [ ! -z "${2}" ]; then
VERSION_NGINX="nginx-${2}"
fi
# URLs to the source directories
export VERSION_OPENSSL
export SOURCE_OPENSSL
export SOURCE_PCRE
export SOURCE_NGINX
export VERSION_PCRE
export VERSION_NGINX
# make a 'today' variable for use in back-up filenames later
today=$(date +"%Y-%m-%d")
# clean out any files from previous runs of this script
rm -rf build
rm -rf /etc/nginx-default
mkdir build
# ensure that we have the required software to compile our own nginx
apt-get -y install curl wget build-essential
# grab the source files
wget -P ./build $SOURCE_PCRE$VERSION_PCRE.tar.gz
if [[ "${TLS13}" == "true" ]]; then
git clone -b "${VERSION_OPENSSL}" "${SOURCE_OPENSSL}" "build/${VERSION_OPENSSL}"
else
wget -P ./build $SOURCE_OPENSSL$VERSION_OPENSSL.tar.gz --no-check-certificate
fi
wget -P ./build $SOURCE_NGINX$VERSION_NGINX.tar.gz
# expand the source files
cd build
tar xzf $VERSION_NGINX.tar.gz
if [[ "${TLS13}" != "true" ]]; then
tar xzf $VERSION_OPENSSL.tar.gz
fi
tar xzf $VERSION_PCRE.tar.gz
cd ../
# set where OpenSSL and nginx will be built
export BPATH=$(pwd)/build
export STATICLIBSSL="$BPATH/staticlibssl"
# build static openssl
cd $BPATH/$VERSION_OPENSSL
rm -rf "$STATICLIBSSL"
mkdir "$STATICLIBSSL"
make clean
./config --prefix=$STATICLIBSSL no-shared \
&& make depend \
&& make \
&& make install_sw
# rename the existing /etc/nginx directory so it's saved as a back-up
mv /etc/nginx /etc/nginx-$today
# build nginx, with various modules included/excluded
cd $BPATH/$VERSION_NGINX
mkdir -p $BPATH/nginx
if [[ "${TLS13}" == "true" ]]; then
./configure --with-cc-opt="-I $STATICLIBSSL/include -I/usr/include" \
--with-ld-opt="-L $STATICLIBSSL/lib -Wl,-rpath -lssl -lcrypto -ldl -lz" \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-pcre=$BPATH/$VERSION_PCRE \
--with-http_ssl_module \
--with-http_v2_module \
--with-file-aio \
--with-openssl=$BPATH/$VERSION_OPENSSL \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-openssl-opt=enable-tls1_3 \
--without-mail_pop3_module \
--without-mail_smtp_module \
--without-mail_imap_module \
&& make && make install
else
./configure --with-cc-opt="-I $STATICLIBSSL/include -I/usr/include" \
--with-ld-opt="-L $STATICLIBSSL/lib -Wl,-rpath -lssl -lcrypto -ldl -lz" \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-pcre=$BPATH/$VERSION_PCRE \
--with-http_ssl_module \
--with-http_v2_module \
--with-file-aio \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--without-mail_pop3_module \
--without-mail_smtp_module \
--without-mail_imap_module \
&& make && make install
fi
# rename the compiled 'default' /etc/nginx directory so its accessible as a reference to the new nginx defaults
mv /etc/nginx /etc/nginx-default
# now restore the previous version of /etc/nginx to /etc/nginx so the old settings are kept
mv /etc/nginx-$today /etc/nginx
echo "All done.";
echo "This build has not edited your existing /etc/nginx directory.";
echo "If things aren't working now you may need to refer to the";
echo "configuration files the new nginx ships with as defaults,";
echo "which are available at /etc/nginx-default";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment