需求:
- docker 镜像打包出去之后,接收的人发现当前的nginx 版本会有漏洞需要做版本升级
进程:
- 因为老的镜像里已经有了nginx, 所以不能直接使用apt install nginx,而且就算是install 因为啥老版本的ubuntu,可用库里的nginx 也不是最新版的
- 下载nginx 源码,并解压
wget http://nginx.org/download/nginx-1.23.1.tar.gz tar -zxvf nginx-1.23.1.tar.gz
- 查看当前nginx 的版本和配置信息 nginx -V
- 进入到解压目录下进行编译 ./configure 并带上原有的configure arguments
./configure --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=/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-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.20.2/debian/debuild-base/nginx-1.20.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
- 运行的时候如果没有安装过 openssl 的话可能会报错
./configure: error: SSL modules require the OpenSSL library.
- 这就需要安装openssl, 或者下载源码配上openssl 的地址,配上地址使用参数 --with-openssl=***, *** 表示源码在本地的地址例:--with-openssl=/root/openssl
- 成功之后在当前目录执行 make,记住不是make install 这很关键, 如果make 不存在就需要安装一下
apt install make -y
- make 完成之后会生成 objs 目录下面有nginx 可执行文件
- 通过上面的nginx -V 返回的 --sbin-path 找到之前的nginx 备份一下 mv 掉
- 再将 objs 下的nginx 拷贝过去,重新执行 nginx -V 版本替换成功
- 如果空间越小越好,可以将下载和解压的包都删掉
- nginx update 完整 Dockerfile
FROM nginx:old RUN apt install wget -y RUN wget http://nginx.org/download/nginx-1.23.1.tar.gz -P /root/ RUN wget https://www.openssl.org/source/old/3.0/openssl-3.0.4.tar.gz -P /root/ RUN cd /root && tar -zxvf nginx-1.23.1.tar.gz RUN cd /root && tar -zxvf openssl-3.0.4.tar.gz RUN cd /root/nginx-1.23.1 && ./configure --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=/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-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.20.2/debian/debuild-base/nginx-1.20.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --with-openssl=/root/openssl-3.0.4 RUN cd /root/nginx-1.23.1/ && make RUN mv /usr/sbin/nginx /usr/sbin/nginx.1.20.2 RUN cp /root/nginx-1.23.1/objs/nginx /usr/sbin/nginx RUN rm -rf /root/openssl-3.0.4 RUN rm -rf /root/nginx-1.23.1 RUN rm /root/nginx-1.23.1.tar.gz RUN rm /root/openssl-3.0.4.tar.gz
拓展:
- openssl 地址 https://www.openssl.org/source/old/3.0/openssl-3.0.4.tar.gz 下载之后直接解压就行
- 在docker 执行 RUN ./configure 会报错,因为docker 中每一个RUN 都是独立的,所以就算在上面运行了 RUN cd 对于下面的这个RUN 并不会有影响,所以需要 RUN cd /root && ./configure
- nginx 的下载也是同理,在配置 --with-openssl= 的时候需要指定绝对路径的话上面解压或者下载的时候需要指定路径
- wget 指定下载路径 使用 -P 例: wget http://nginx.org/download/nginx-1.23.1.tar.gz -P /root/