Ubuntu 22.04从nginx.org装NGINX后无法安装libnginx-mod-http-perl怎么办?
问题描述
在Ubuntu 22.04服务器上,已通过/etc/apt/preferences.d/99nginx配置优先从nginx.org仓库安装NGINX,配置内容如下:
Package: * Pin: origin nginx.org Pin: release o=nginx Pin-Priority: 900
当前已安装的NGINX确实来自nginx.org仓库,apt policy nginx输出验证:
nginx: Installed: 1.23.2-1~jammy Candidate: 1.23.2-1~jammy Version table: *** 1.23.2-1~jammy 900 900 http://nginx.org/packages/mainline/ubuntu jammy/nginx amd64 Packages 100 /var/lib/dpkg/status 1.23.1-1~jammy 900 900 http://nginx.org/packages/mainline/ubuntu jammy/nginx amd64 Packages 1.23.0-1~jammy 900 900 http://nginx.org/packages/mainline/ubuntu jammy/nginx amd64 Packages 1.21.6-1~jammy 900 900 http://nginx.org/packages/mainline/ubuntu jammy/nginx amd64 Packages 1.18.0-6ubuntu14.3 500 500 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages 500 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages 1.18.0-6ubuntu14 500 500 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 Packages
尝试安装libnginx-mod-http-perl时出现依赖冲突,执行apt install libnginx-mod-http-perl报错:
# apt install libnginx-mod-http-perl Reading package lists... Done Building dependency tree... Done Reading state information... Done Some packages could not be installed. This may mean that you have requested an impossible situation or if you are using the unstable distribution that some required packages have not yet been created or been moved out of Incoming. The following information may help to resolve the situation: The following packages have unmet dependencies: nginx : Conflicts: nginx-common but 1.18.0-6ubuntu14.3 is to be installed E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.
同时该模块属于Ubuntu官方nginx-extras包,但该包依赖官方版本NGINX,无法兼容现有安装。
问题原因
Ubuntu官方仓库提供的libnginx-mod-http-perl模块,依赖的是官方维护的nginx-common等基础包,而nginx.org仓库的NGINX包结构与官方完全独立,二者的基础包存在冲突。当你尝试安装官方模块时,APT会试图安装官方的nginx-common来满足依赖,这就和已安装的nginx.org版本NGINX产生冲突,导致安装失败。
解决方法
方案一:检查nginx.org仓库是否提供对应模块
首先确认nginx.org的mainline仓库是否包含该模块,执行以下命令搜索:
apt search libnginx-mod-http-perl
如果搜索结果中存在来自nginx.org仓库的该模块包,直接执行安装命令即可,它会自动依赖nginx.org的基础包,不会产生冲突:
sudo apt install libnginx-mod-http-perl
方案二:手动编译NGINX并集成Perl模块
如果nginx.org仓库未提供该模块,需要手动编译NGINX并加入Perl模块支持:
- 卸载现有NGINX及相关包:
sudo apt remove --purge nginx nginx-core nginx-full nginx-light nginx-extras - 安装编译所需依赖:
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libperl-dev - 下载与之前版本一致的NGINX源码(以1.23.2为例):
wget http://nginx.org/download/nginx-1.23.2.tar.gz - 解压源码并进入目录:
tar -zxvf nginx-1.23.2.tar.gz && cd nginx-1.23.2 - 配置编译选项,关键加入
--with-perl_module开启Perl模块支持(其余选项可根据需求或原包配置调整):./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/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-perl_module - 编译并安装:
make && sudo make install - 配置systemd服务以管理NGINX:
创建服务文件:
写入以下内容:sudo nano /etc/systemd/system/nginx.service
启用并启动服务:[Unit] Description=A high performance web server and a reverse proxy server Documentation=man:nginx(8) After=network.target nss-lookup.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;' ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;' ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /var/run/nginx.pid TimeoutStopSec=5 KillMode=mixed [Install] WantedBy=multi-user.targetsudo systemctl enable --now nginx
方案三:临时切换至官方NGINX版本(不推荐)
此方案会将现有NGINX替换为官方版本,后续更新可能再次出现冲突,仅临时应急使用:
- 备份当前NGINX配置:
sudo cp -r /etc/nginx /etc/nginx.backup - 修改
/etc/apt/preferences.d/99nginx中的优先级为400(低于官方仓库的500):
将sudo nano /etc/apt/preferences.d/99nginxPin-Priority: 900改为Pin-Priority: 400 - 安装模块:
sudo apt install libnginx-mod-http-perl - 若后续需换回nginx.org版本,改回优先级900,重新安装对应版本NGINX并恢复配置:
sudo apt install nginx=1.23.2-1~jammy sudo cp -r /etc/nginx.backup/* /etc/nginx/ sudo systemctl reload nginx
内容的提问来源于stack exchange,提问作者Dr. Gianluigi Zane Zanettini
相关产品推荐
相关产品推荐

