From 1be51ac90693ad583a2e61e77b945d294a31478c Mon Sep 17 00:00:00 2001 From: modeco80 Date: Mon, 16 Sep 2024 00:49:57 -0400 Subject: [PATCH] Completely revamp installation script Instead of cluttering the $PREFIX during build (and requiring root to do so), we now install everything into a temporary directrory that is copied in piecemeal to the prefix). Additionally, the installer will now, on a fresh installation only, copy the base config files from /conf to $PREFIX/etc/nginx. A existing installation will not have this performed (unlike the upstream nginx installation rules), so your configuration will be safe. --- .gitignore | 2 ++ README.md | 4 ++-- build | 34 ++++++++++++++++++++++------------ config | 13 +++++++++++++ install | 47 ++++++++++++++++++++++++++++++++--------------- 5 files changed, 71 insertions(+), 29 deletions(-) create mode 100644 .gitignore create mode 100644 config diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9453e9a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# temporary +install_root/ diff --git a/README.md b/README.md index 7634382..c5739e1 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Essentially, this is OpenResty core (and a few Lua modules we actually use), and $ git clone --recursive https://git.computernewb.com/nginx-cn $ cd nginx-cn $ ./build -# ./install -# ... (copy the base config from conf/ into /opt/nginx/etc/nginx and adjust/add to suit your configuration) +# sudo ./install +# ... (adjust the configuration base to suit your needs) # profit? ``` diff --git a/build b/build index 9998f1c..aa0eb69 100755 --- a/build +++ b/build @@ -1,11 +1,12 @@ #!/bin/bash -# installation prefix. unfortunately we have to vendor -# certain dependencies (luajit) so this /opt path is -# (probably) going to unfortunately stick. -PREFIX="/opt/nginx" +. ./config -TOP="$(pwd)" + +# make install prefix +[[ ! -d "${INSTALL_PREFIX}" ]] && { + mkdir -p ${INSTALL_PREFIX} +} # clean Nginx sources beforehand. pushd sources/nginx/ @@ -13,7 +14,7 @@ pushd sources/nginx/ git clean -dxf popd -local BASE_CONFIGURE_FLAGS=( +BASE_CONFIGURE_FLAGS=( # Build everything with Clang + ThinLTO --with-cc="clang" --with-cc-opt="-pipe -march=native -mtune=native -flto=thin" @@ -75,12 +76,12 @@ popd # build and install luajit and the resty lua libraries into prefix pushd $TOP/sources/luajit2 git clean -dxf - make PREFIX=$PREFIX -j49 - sudo make PREFIX=$PREFIX install + make PREFIX=$INSTALL_PREFIX -j49 + make PREFIX=$INSTALL_PREFIX install popd # resty libs that can be installed -local RESTY_LIBS=( +RESTY_LIBS=( lua-resty-{core,lrucache,websocket} ) @@ -88,15 +89,15 @@ local RESTY_LIBS=( for lib in ${RESTY_LIBS[@]}; do echo "installing $lib" pushd $TOP/sources/$lib - sudo make install PREFIX=$PREFIX + make install PREFIX=$INSTALL_PREFIX popd done pushd $TOP/sources/nginx # needed for lua-nginx-module -export LUAJIT_INC="$PREFIX/include/luajit-2.1" -export LUAJIT_LIB="$PREFIX/lib" +export LUAJIT_INC="$INSTALL_PREFIX/include/luajit-2.1" +export LUAJIT_LIB="$INSTALL_PREFIX/lib" # TODO: see ./install for --sbin-path ./auto/configure \ @@ -118,3 +119,12 @@ export LUAJIT_LIB="$PREFIX/lib" make -j $(($(nproc)+1)) popd + +# Strip nginx and install it into the staging prefix +pushd $TOP/sources + cp nginx/objs/nginx nginx/objs/nginx.unstripped + strip nginx/objs/nginx + + [[ ! -d "${INSTALL_PREFIX}/bin" ]] && mkdir -p ${INSTALL_PREFIX}/bin + cp nginx/objs/nginx $INSTALL_PREFIX/bin/nginx +popd diff --git a/config b/config new file mode 100644 index 0000000..7ef2edf --- /dev/null +++ b/config @@ -0,0 +1,13 @@ +# Shared configuration for build/install scripts + +# Installation prefix. unfortunately we have to vendor +# certain dependencies (luajit) for OpenResty, +# so this /opt path is going to unfortunately stick +# Change to what you like I suppose, but make sure to update the systemd files.. +PREFIX="/opt/nginx" + +TOP="$(pwd)" + +# The staging prefix used to install everything before actually installing it to +# $PREFIX. +INSTALL_PREFIX="${TOP}/install_root" diff --git a/install b/install index 86a2e63..6771c3a 100755 --- a/install +++ b/install @@ -1,23 +1,40 @@ #!/bin/bash -# Installs nginx-cn +# Installs nginx-cn into the configured PREFIX. +# +# N.B: We do not use nginx's `make install` because it's very very bad +# and it overwrites $PREFIX/etc unconditionally. It's bad design choice, +# and I personally do not like it. -TOP="$(pwd)" +. ./config -[[ ! -d "$TOP/sources/nginx/objs" ]] && { +# Check for the install prefix directory. If it doesn't exist, +# then a build likely hasn't been done, so we can't proceed. +[[ ! -d "${INSTALL_PREFIX}" ]] && { echo "You haven't built nginx-cn yet. Do so with ./build" exit 1 } -# N.B: We do not use nginx's `make install` because it's very very bad -# and it overwrites $PREFIX/etc unconditionally. It's bad. I don't like it. -pushd $TOP/sources -sudo systemctl stop nginx-cn - # strip binary of unneeded fluff (saving the original) - cp nginx/objs/nginx nginx/objs/nginx.unstripped - strip nginx/objs/nginx - sudo cp nginx/objs/nginx /opt/nginx/bin -sudo systemctl start nginx-cn -#sudo make install - echo "Binary installed. You should probably copy the sample configuration to /opt/nginx/etc/nginx." -popd +# We're replacing an existing installation. +# Make sure not to overwrite a user's existing configuration +[[ -d "${PREFIX}/etc/nginx" ]] && { + echo "You seem to have a configuration in your PREFIX." + echo "I will not touch it." + # Remove old files and replace them with the install prefix versions + rm -rf ${PREFIX}/{bin,lib,share} + cp -r ${INSTALL_PREFIX}/bin ${PREFIX}/bin + cp -r ${INSTALL_PREFIX}/lib ${PREFIX}/lib + cp -r ${INSTALL_PREFIX}/share ${PREFIX}/share +} + +# A new installation. +# We SHOULD copy over the configuration now. +[[ ! -d "${PREFIX}" ]] && { + mkdir -p ${PREFIX} ${PREFIX}/etc/nginx + cp -r ${INSTALL_PREFIX}/bin ${PREFIX}/bin + cp -r ${INSTALL_PREFIX}/lib ${PREFIX}/lib + cp -r ${INSTALL_PREFIX}/share ${PREFIX}/share + cp -r ${TOP}/conf ${PREFIX}/etc/nginx/ +} + +echo "nginx-cn has been installed/updated."