Apt安装Nginx的源:从发行版源改为Nginx官方源,升级到最新的稳定版本程序。

Ubuntu 24.04的Nginx版本是1.24,而Nignx官方的稳定版本已经是1.28。

起因

Ubuntu官方源的Nginx版本是1.24,在所有conf文件中启用http2后,存在一个问题:

  1. 在conf文件中绑定了域名访问,没有问题;但使用http://ip 或者 http://{非指定域名}时,浏览器不是打开首页,下载了一个名为<下载>的文件,无后缀名,大小只有57字节
  2. 如果在default.conf中的listen里,删除http2,而xxx2.conflisten里,保留http2时,sudo nginx -t && sudo nginx -s reload执行成功后,访问 xxx2.conf 的域名没有问题,是正常的。使用 http://ip 或者 http://{非指定域名} 时,也没有问题,是正常打开首页。但在 sudo nginx -t && sudo nginx -s reload 会提示警告信息:
2025/11/13 22:11:27 [warn] 250001#250001: protocol options redefined for 0.0.0.0:80 in /etc/nginx/sites-enabled/xxx2.conf:2
2025/11/13 22:11:27 [warn] 250001#250001: protocol options redefined for 0.0.0.0:443 in /etc/nginx/sites-enabled/xxx2.conf:8
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
2025/11/13 22:11:27 [notice] 250004#250004: signal process started

官方社区获得帮助,是一个无害的警告,因为您正在重新定义相同的侦听端口。在1.25.1/1.26.0版本里,listen指令的http2参数已弃用 ,取而代之的是该http2指令。升级Nginx版本后,就可以在所有站点里启用http2,且能正常使用了。

更换Apt安装源方法

含Linux其他发行版本,使用nginx官方软件仓库的方法:见文档;以下是Ubuntu的更换方法:

Bash
# 安装依赖
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
# 导入 nginx 官方签名密钥
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
# 检查输出,应包含完整的指纹 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
# 添加稳定 nginx 软件包的 apt 仓库
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
# 优先使用nginx官方的软件包,而非发行版提供的软件包
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | sudo tee /etc/apt/preferences.d/99nginx
# 安装或升级 nginx
#sudo apt update
#sudo apt install [--only-upgrade] nginx

注意:版本升级要提前准备,直接升级容易中断网站访问,导致生产环境故障。

Nginx从1.24升级到1.28

Bash
$ sudo apt install --only-upgrade nginx

配置文件确认步骤

Configuration file '/etc/nginx/nginx.conf'
 ==> Modified (by you or by a script) since installation.
 ==> Package distributor has shipped an updated version.
   What would you like to do about it ?  Your options are:
    Y or I  : install the package maintainer's version
    N or O  : keep your currently-installed version
      D     : show the differences between the versions
      Z     : start a shell to examine the situation
 The default action is to keep your current version.
*** nginx.conf (Y/I/N/O/D/Z) [default=N] ?
配置文件 '/etc/nginx/nginx.conf'  
==> 自安装以来已被修改(由您或脚本修改)。  
==> 软件包分发者已提供了更新版本。  
您希望如何处理?可选的操作有:  
Y 或 I :安装软件包维护者提供的版本  
N 或 O :保留您当前安装的版本  
D :显示两个版本之间的差异  
Z :启动一个 shell 以检查情况  
默认操作是保留您当前的版本。  
*** nginx.conf (Y/I/N/O/D/Z) [默认=N] ?

选择Y,会自动把原有文件重命名为 nginx.conf.dpkg-old,再创建新的nginx.conf

升级完成后,此时原来网站不可访问,需要调整所有conf文件(主配置+站点配置)。

nginx源的1.28,相对于ubunt源的1.24改变

  • 站点配置文件,从1.24的 /etc/nginx/sites-enabled/* 改到1.28的 /etc/nginx/conf.d/*.conf
  • 工作进程的用户名由www-data改为nginx
Bash
nginx version: nginx/1.28.0
built by gcc 13.3.0 (Ubuntu 13.3.0-6ubuntu2~24.04)
built with OpenSSL 3.0.13 30 Jan 2024
TLS SNI support enabled
configure arguments: --prefix=/etc/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=/run/nginx.pid --lock-path=/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-http_v3_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -ffile-prefix-map=/home/builder/debuild/nginx-1.28.0/debian/debuild-base/nginx-1.28.0=. -flto=auto -ffat-lto-objects -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -fcf-protection -fdebug-prefix-map=/home/builder/debuild/nginx-1.28.0/debian/debuild-base/nginx-1.28.0=/usr/src/nginx-1.28.0-1~noble -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

升级后问题1:invalid PID number “” in “/run/nginx.pid”

Bash
$ sudo nginx -t && sudo nginx -s reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx: [error] invalid PID number "" in "/run/nginx.pid"

解决办法:sudo rm -f /run/nginx.pid && sudo systemctl restart nginx

升级后问题2:访问网站提示502

tail -f /var/log/nginx/error.log, 提示:2025/11/14 09:02:09 [crit] 258767#258767: *21 connect() to unix:/run/php/php8.3-fpm.sock failed (13: Permission denied)

是由于工作进程用户名由www-data改为nginx
解决办法:

Bash
# 改php-fpm工作池的用户
sudo vim /etc/php/8.3/fpm/pool.d/www.conf
# 在vim中执行替换再保存退出 `:%s/www-data/nginx/g`
sudo systemctl restart php8.3-fpm
sudo ls -la /run/php/php8.3-fpm.sock

# 改 webRoot 目录的权限
sudo chown -R nginx:adm /data/www

sudo systemctl restart nginx

# 其他需要nginx使用到权限目录与配置,等等。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注