docker 中升级 nginx

10 篇文章 0 订阅

需求:

  • 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/
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker更新nginx可以通过以下步骤进行操作: 1. 首先,停止并删除当前正在运行的nginx容器。可以使用以下命令: ``` docker stop nginx docker rm nginx ``` 2. 然后,拉取最新的nginx镜像。可以使用以下命令: ``` docker pull nginx ``` 3. 创建一个新的nginx容器,并将新的镜像挂载到容器。可以使用以下命令: ``` docker run --name nginx -d -p 80:80 nginx ``` 这将创建一个名为nginx的容器,将容器的80端口映射到主机的80端口,并使用最新的nginx镜像运行容器。 如果你想要自定义nginx的配置文件或者网站内容,你可以使用`-v`参数来挂载本地文件到容器,就像引用\[2\]和引用\[3\]的示例一样。 请注意,这些命令假设你已经安装了Docker并具有适当的权限来执行这些操作。 #### 引用[.reference_title] - *1* [docker 更新升级 nginx](https://blog.csdn.net/Weirdo_zhu/article/details/126120940)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Docker安装Nginx并修改Nginx配置文件](https://blog.csdn.net/weixin_43388691/article/details/127878007)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值