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.
This commit is contained in:
Lily Tsuru 2024-09-16 00:49:57 -04:00
parent 0435cc9a2c
commit 1be51ac906
5 changed files with 71 additions and 29 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
# temporary
install_root/

View file

@ -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?
```

34
build
View file

@ -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

13
config Normal file
View file

@ -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"

47
install
View file

@ -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."