Cat73 Blog

喵星人占领地球战略指挥部

0%

修改 Ubuntu 自带的 Nginx 的编译参数使用 OpenSSL 1.1

前言

很长一段时间都在用Ubuntu里直接用apt-get install安装的Nginx
因此自己很多想折腾的东西如TLSv1.3就因为缺依赖、依赖过旧等原因被锁了
然而又不想自己从头编译Nginx,要折腾的东西太多了。。。
今天终于搞明白怎么自定义Ubuntu里的Nginx的编译参数惹。。写篇文章记下来 0.0

准备OpenSSL 1.1.0

要换1.1.0当然要先有啦~

  1. 下载并解压最新的OpenSSL源码
    1
    2
    3
    4
    5
    6
    7
    # 个人习惯,扔别的地方也是可以的
    cd /opt
    # 到 https://www.openssl.org/source/ 可以找到最新版的下载地址
    wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz
    # 解压源码
    tar -xf openssl-1.1.0f.tar.gz
    cd openssl-1.1.0f
  2. 安装OpenSSL不要覆盖系统自带的OpenSSL!!!(很重要!划重点!)
    1
    2
    3
    ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
    make
    make install
  3. 为依赖库建立连接,不然1.1OpenSSL会因为找不到库而没法使用
    1
    2
    3
    cd /usr/local/openssl/lib
    ln -s $(pwd)/libssl.so.1.1 /usr/lib/libssl.so.1.1
    ln -s $(pwd)/libcrypto.so.1.1 /usr/lib/libcrypto.so.1.1
  4. 测试安装结果
    1
    2
    3
    4
    openssl version
    # OpenSSL 1.0.2g 1 Mar 2016
    /usr/local/openssl/bin/openssl version
    # OpenSSL 1.1.0f 25 May 2017

准备自定义Nginx的安装包

更换最新版Nginx的源

折腾这种东西嘛,当然要折腾最新的啦,在1.13.5都发布了的时候,Ubuntu里装的Nginx居然还是1.10.3
真是要多不爽就有多不爽,那么第一步当然是换个新的咯 0.0
这个源一般会紧跟官方最新的版本,可能更新也就比官方慢个四五天的样子 0.0

1
2
3
apt-get update
apt-get install software-properties-common
add-apt-repository ppa:chris-lea/nginx-devel

准备Nginx编译环境和源代码

  1. 首先编辑/etc/apt/source.list.d/里包含nginx的那个文件,取消第二行deb-src的注释
  2. 准备Nginx编译环境
    1
    2
    apt-get update
    apt-get build-dep nginx
  3. 下载Nginx源代码
    1
    2
    3
    4
    5
    cd /opt
    mkdir nginx
    cd nginx
    apt source nginx
    cd nginx-1.13.5
  4. 编辑Nginx安装规则,使用自定义的OpenSSL
    1
    vim debian/rules
    添加下面的规则:
    1
    --with-openssl=/opt/openssl-1.1.0f \
    添加后就像这样:
    1
    2
    3
    4
    5
    # configure flags
    common_configure_flags := \
    --with-openssl=/opt/openssl-1.1.0f \
    --with-cc-opt="$(debian_cflags)" \
    --with-ld-opt="$(debian_ldflags)" \

编译并安装Nginx

  1. 编译(打包)Nginx
    1
    2
    3
    cd /opt/nginx/nginx-1.13.5
    # 可能会持续十分钟左右,甚至更长,请耐心等待
    dpkg-buildpackage -b
  2. 安装Nginx
    1
    2
    3
    dpkg -i nginx-light_1.13.5-1chl1~zesty1_amd64.deb
    apt-get install -f
    apt-mark hold nginx-light

最后

1
2
3
4
5
6
7
# 执行下 nginx -V 来看看 OpenSSL 版本对不对咯
# 如果正确就会像下面似的输出 built with OpenSSL 1.1.0f 咯~
nginx -V
# nginx version: nginx/1.13.5
# built with OpenSSL 1.1.0f 25 May 2017
# TLS SNI support enabled
# configure arguments: --with-openssl=/root/nginx/openssl-1.1.0f 后略...
  • 啊科技树终于解锁了真爽!以后会继续在这个基础上折腾一堆新玩意的!

参考内容

Build nginx (Mainline) with OpenSSL 1.1.0 on Ubuntu 16.04
How To Add ngx_pagespeed to Nginx on Ubuntu 14.04