在 ubuntu 18.04 上安装 NGINX 包时出错

在 ubuntu 18.04 上安装 NGINX 包时出错

我是 Ubuntu 新手。我需要在 nginx 服务器上启用 brotli 压缩。

我正在尝试通过以下链接安装带有 BROTLI 的 NGINX。 https://www.howtoforge.com/tutorial/how-to-install-nginx-with-brotli-compression-on-ubuntu-1804/

当我尝试按照链接安装构建包时出现错误。

root@sachin-virtual-machine:/usr/local/src# ls
nginx-1.14.2                           nginx_1.14.2-1~bionic.dsc
nginx_1.14.2-1~bionic_amd64.buildinfo  nginx_1.14.2.orig.tar.gz
nginx_1.14.2-1~bionic_amd64.changes    nginx-dbg_1.14.2-1~bionic_amd64.deb
nginx_1.14.2-1~bionic_amd64.deb        ngx_brotli
nginx_1.14.2-1~bionic.debian.tar.xz
root@sachin-virtual-machine:/usr/local/src# sudo dpkg -i *.deb
dpkg: regarding nginx_1.14.2-1~bionic_amd64.deb containing nginx:
nginx conflicts with nginx-common
nginx-common (version 1.14.0-0ubuntu1.2) is present and installed.

dpkg: error processing archive nginx_1.14.2-1~bionic_amd64.deb (--install):
conflicting packages - not installing nginx
(Reading database ... 175249 files and directories currently installed.)
Preparing to unpack nginx-dbg_1.14.2-1~bionic_amd64.deb ...
Unpacking nginx-dbg (1.14.2-1~bionic) over (1.14.2-1~bionic) ...
dpkg: dependency problems prevent configuration of nginx-dbg:
nginx-dbg depends on nginx (= 1.14.2-1~bionic); however:
Version of nginx on system is 1.14.0-0ubuntu1.2.
Version of nginx on system, provided by nginx-core:amd64, is <none>.

dpkg: error processing package nginx-dbg (--install):
dependency problems - leaving unconfigured
Errors were encountered while processing:
nginx_1.14.2-1~bionic_amd64.deb
nginx-dbg
root@sachin-virtual-machine:/usr/local/src#

请指导我。

答案1

您已经安装了 nginx,因此您必须删除该版本才能从源代码构建和安装。

  1. 检查新安装的 nginx 并将其删除

    dpkg -l nginx*
    
    • 您应该看到版本1.14.2,用 将其删除sudo dpkg remove nginx-1.14.2,并确保使用正确的版本号。
  2. 现在就这么做sudo apt update,并sudo apt upgrade确保它们没有问题。

  3. 现在删除旧版本sudo apt purge <nginx-1.14.0>

  4. 现在构建新版本并安装,如果有任何问题请运行sudo apt install -f

答案2

如果您已经在运行 NGINX 并且想要添加 Brotli 支持,您可以通过构建和安装动态模块来实现。这需要遵循许多步骤。为了实现自动化,以下是我在构建时使用的 bash 脚本:

#!/bin/bash
# https://www.majlovesreg.one/tag/code/
# https://www.majlovesreg.one/adding-brotli-to-a-built-nginx-instance

# For custom NGINX version, use:
# ngver=1.14.2
# For passing via the command line, use:
# ngver=$1
# For automated detection of installed NGINX, use:
ngver=$(nginx -v 2>&1 | grep -oP '(?<=/).*')

moddir=/usr/share/nginx/modules
builddir=$(mktemp -d)

# Install needed development packages if not yet installed in the system
# sudo apt -y install git libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev

# Build in tmp directory
cd ${builddir}

# Download and unpack NGINX
wget https://nginx.org/download/nginx-${ngver}.tar.gz && { tar zxf nginx-${ngver}.tar.gz && rm nginx-${ngver}.tar.gz; } || { echo '!! download failed, exiting...'; exit 1; }

# Download, initialize, and make Brotli for NGINX
git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli && git submodule update --init && cd ../nginx-${ngver}
nice -n 19 ionice -c 3 ./configure --with-compat --add-dynamic-module=../ngx_brotli || { echo '!! configure failed, exiting...'; exit 2; }
nice -n 19 ionice -c 3 make modules || { echo '!! make failed, exiting...'; exit 3; }

# Replace brotli in modules directory, i.e. /usr/share/nginx/modules/
[ -f ${moddir}/ngx_http_brotli_filter_module.so ] && sudo mv ${moddir}/ngx_http_brotli_filter_module.so ${moddir}/ngx_http_brotli_filter_module.so.old
[ -f ${moddir}/ngx_http_brotli_static_module.so ] && sudo mv ${moddir}/ngx_http_brotli_static_module.so ${moddir}/ngx_http_brotli_static_module.so.old
sudo cp objs/*.so ${moddir}/
sudo chmod 644 ${moddir}/ngx_http_brotli_filter_module.so
sudo chmod 644 ${moddir}/ngx_http_brotli_static_module.so
cd ${builddir}

# If new module works well, clean up build files
sudo nginx -t && { sudo rm -r ${builddir}/ngx_brotli; rm -r ${builddir}/nginx-${ngver}; } || { echo '!! nginx conf failed, exiting...'; exit 4; }

# Restart NGINX
systemctl is-active nginx && sudo systemctl restart nginx || sudo systemctl start nginx
echo
systemctl --no-pager status nginx
echo
echo 'Build and install of ngx_brotli sucessful!'
echo

此后,您将需要通过添加以下内容来加载动态模块nginx.conf

load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

该答案取自以下网页:https://www.majlovesreg.one/adding-brotli-to-a-built-nginx-instance

相关内容