前言
HTTPS 即将在 2017 年获得广泛的普及和支持,这里介绍一下 Apache Httpd 设置 HTTPS 并开启 HTTP/2 (以下会简称 h2)的教程。
不过呢,在目前的条件下,个人还是建议一些强 Apache Httpd 的环境例如 LAMP 可以考虑前面加一层 Nginx,来更灵活的实现 HTTPS + HTTP/2 的新特性,例如目前 Apache Httpd 对 CHACHA20-POLY1305
的算法弄起来就灵活。
准备
- Apache Httpd ≥ 2.4.17 ,Apache Httpd 从 2.4.17 开始支持
mod_http2
,并且编译时加入--enable-http2
- OpenSSL ≥ 1.0.2 浏览器客户端例如 Chrome 51(2016 年 5 月 31 日发布)之后,已经不支持对没有 ALPN 协议网站的 h2 支持,OpenSSL 1.0.2 起开始支持 ALPN 协议。
- nghttp2 ≥ 1.12,其为是基于 C 实现的 HTTP/2 库,为实现 Apache Httpd 支持 h2 所必须
- (编译、Win二进制)Apache Httpd ≥ 2.4.25 ,如果是自己编译或者 Win 采用二进制编译包的 Apache Httpd 并且开启
mod_http2
支持,版本必须大于 2.4.25,因为之前的版本存在 DDOS 漏洞
非编译
Debian
Debian 在 stretch(9)才有 Apache Httpd(apache2) ≥ 2.4.17 (已经编译 mod_http2 模块)和 OpenSSL ≥ 1.0.2 的支持。
apt-get install apache2 openssl
Ubuntu
Ubuntu 截止发文,包括 xenial(16.04)和 yakkety(16.10)中的 Apache Httpd(apache2)2.4.18 并没有编译编译 mod_http2
模块,也没提供 mod_http2
的二进制包。
不过大神,ondrej 则提供了最新的 Apache Httpd(apache2)和 mod_http2
的二进制包还有 nghttp2
的 PPA 源支持。
add-apt-repository ppa:ondrej/apache2
apt-get update
apt-get install apache2 openssl
RHEL、CentOS
截止发文切仍受支持的 RHEL、CentOS 5、6、7 均不提供 Apache Httpd(apache2) ≥ 2.4.17 和 OpenSSL ≥ 1.0.2 的支持,也没有提供支持的第三放软件源支持。 请参考下面的 编译 方法。
Fedora
截止发文,最新的 Fedora 25 版本,已经提供 Apache Httpd(apache2) ≥ 2.4.17 (已经编译 mod_http2 模块)和 OpenSSL ≥ 1.0.2 的支持,可以放心使用。
dnf install httpd openssl
启用 mod_http2 模块
在终端中输入:
a2enmod http2 # 启用 mod_http2 模块
service httpd restart # RHEL、CentOS、Fedora
service apache2 restart # Debian、Ubuntu
编译
核心组件依赖
适合 Ubuntu 16.04 or later 和 Debian 9 or later
apt-get installl nghttp2 libnghttp2-dev openssl #Debian、Ubuntu
未来或许可以用
yum install nghttp2 libnghttp2-dev openssl #RHEL、CentOS
核心组件不依赖
OpenSSL
wget https://www.openssl.org/source/openssl-1.1.0c.tar.gz
tar xzf openssl-1.1.0c.tar.gz
cd openssl-1.1.0c
./config --prefix=/usr/local/openssl
make && make install
nghttp2
wget https://github.com/nghttp2/nghttp2/releases/download/v1.17.0/nghttp2-1.17.0.tar.gz
tar xzf nghttp2-1.17.0.tar.gz
cd nghttp2-1.17.0
./configure --prefix=/usr/local/nghttp2
make && make install
编译 Apache Httpd
在原来的编译参数上加入如下参数:
–enable-http2
让 Apache Httpd 可以实现 HTTP/2 协议
--enable-ssl
开启 mocd_ssl 的支持
–with-nghttp2=/usr/local/nghttp2
指定 nghttp2库 的位置,如果是利用系统以来的就不用这个
--with-ssl=/usr/local/openssl
指定 openssl 库 的位置,如果是利用系统以来的就不用这个
设置
在 httpd.conf
中,或者 /etc/enabled-sites/
目录下添加或新建,具体文档自己参考。
在相关虚拟主机的 443 端口中加入:
ProtocolsHonorOrder On
Protocols h2 h2c http/1.1
即可开启 HTTPS 和 h2 了,完整的参考可以这里:
<VirtualHost *:443>
...
SSLEngine on
SSLCertificateFile /path/to/crt
SSLCertificateKeyFile /path/to/key
SSLCACertificateFile /path/to/ca_certs_for_client_authentication
ProtocolsHonorOrder On
Protocols h2 h2c http/1.1
# HSTS
Header always set Strict-Transport-Security "max-age=15768000"
# modern configuration, tweak to your needs
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM- SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA- AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE- RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256
SSLHonorCipherOrder on
SSLCompression off
SSLSessionTickets off
# OCSP Stapling, only in httpd 2.3.3 and later
SSLUseStapling on
SSLStaplingResponderTimeout 5
SSLStaplingReturnResponderErrors off
SSLStaplingCache shmcb:/var/run/ocsp(128000)
...
</VirtualHost>
HTTPS 才开始普及,就出H2版本啦?看来我消息滞后了
h2去年正式推出的,一般来说有https都会布置h2
学习了
感谢分享