截至目前,Debian 9 (stretch) 安装了 nginx 版本 1.10.3,该版本易受CVE-2017-7529 攻击:
从 0.5.6 到 1.13.2 的 Nginx 版本容易受到 nginx 范围过滤器模块中整数溢出漏洞的影响,从而导致由特制请求触发的潜在敏感信息泄漏。
我担心用户数据的安全,因此我想升级到不再受此问题影响的最新版本。
nginx 1.13.3及以上版本有多种获取方式。可以自己编译,使用stretch-backports仓库,也可以添加nginx自己的apt仓库。在这个答案中,我将引导您完成最后一个,因为它可能是所有三个中最容易做到的。
nginx 的网站有一个关于如何设置存储库的专用页面,但还有更多内容,特别是如果您现在想避免此特定漏洞。该stable
分支将安装 1.12.0,它仍然存在漏洞(它已在 1.12.1+ 和 1.13.3+ 中修补),因此您需要使用mainline
,它将安装 1.13.5。
在最好的情况下,切换 nginx 版本应该像运行一些命令一样简单,您将在 2-3 分钟内完成,并且停机时间最短。为了能够尽快恢复并运行,让我们从准备安装开始。首先,您需要将存储库添加到您的 apt 配置中,添加签名密钥,并更新包列表:
$ sudo echo "deb http://nginx.org/packages/mainline/debian/ stretch nginx
deb-src http://nginx.org/packages/mainline/debian/ stretch nginx" > /etc/apt/sources.list.d/nginx.list
$ wget -qO - https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
$ sudo apt update
Run Code Online (Sandbox Code Playgroud)
接下来,运行以下命令:
$ sudo apt remove nginx-common
Run Code Online (Sandbox Code Playgroud)
这将有效地从系统中卸载 nginx,但会保留您的配置文件,保存易于恢复的 systemd 服务文件。
接下来从新存储库安装 nginx:
$ sudo apt install nginx
Run Code Online (Sandbox Code Playgroud)
请注意,这会询问您是否要替换某些配置文件,如下所示:
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] ?
Run Code Online (Sandbox Code Playgroud)
确保不要输入Y
,每次提示时只需按Enter或输入N
以避免丢失当前配置。
如果您不小心覆盖了您的内容,nginx.conf
您将 - 至少 - 需要将文件中的最后一行从include /etc/nginx/conf.d/*.conf;
更改include /etc/nginx/sites-enabled/*;
为 以恢复以前的包含行为。
您可以验证新安装的版本:
$ nginx -v
nginx version: nginx/1.13.5
Run Code Online (Sandbox Code Playgroud)
最后,您会注意到现在尝试运行service nginx start
失败并显示以下消息:
Failed to start nginx.service: Unit nginx.service is masked.
Run Code Online (Sandbox Code Playgroud)
这是因为之前 systemd 用来管理 nginx 的删除nginx-common
也被擦除了/lib/systemd/system/nginx.service
。要恢复此文件,只需使用以下命令创建它:
$ echo "[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
" > /lib/systemd/system/nginx.service
Run Code Online (Sandbox Code Playgroud)
最后,运行systemctl unmask nginx
后跟systemctl enable nginx
,现在您应该能够像以前一样管理服务,而您以前的所有设置都完好无损。
$ service nginx start
Run Code Online (Sandbox Code Playgroud)