Ubuntu 16.04默认源里的Apache2没有Http2模块,如果启用Http2模块一般会报错找不到模块.

a2enmod http2  
ERROR: Module http2 does not exist!  

所以如果要启用Http2模块,需要从源码从头编一个Apache2.不过编译好的Http2模块可以直接拷贝出来使用,所以已经安装过Apache2了也不需要担心.

首先将源码源加入源列表之中:

vi /etc/apt/sources.list  

添加以下:

deb-src http://archive.ubuntu.com/ubuntu/ xenial main universe restricted multiverse  
deb-src http://security.ubuntu.com/ubuntu xenial-security main universe restricted multiverse  
deb-src http://archive.ubuntu.com/ubuntu/ xenial-updates main universe restricted multiverse  

然后刷新源,如果有需要更新的包,顺便升下级:

apt update  
apt upgrade  

然后安装依赖:

apt install curl devscripts build-essential fakeroot libnghttp2-dev  

创建一个工作的编译目录:

cd ~  
mkdir apache2  
cd apache2  

从源里下载Apache2的源码然后编译:

apt source apache2  
apt build-dep apache2  
cd apache2-2.4.18  
apt install curl devscripts build-essential fakeroot  
fakeroot debian/rules binary  

将编译的好的Http2模块拷贝到Apache2模块目录中:

cp debian/apache2-bin/usr/lib/apache2/modules/mod_http2.so /usr/lib/apache2/modules/  

然后为Http2模块编写一个配置文件:

vim /etc/apache2/mods-available/http2.load  

在其中写入以下:

LoadModule http2_module /usr/lib/apache2/modules/mod_http2.so

<IfModule http2_module>  
LogLevel http2:info  
</IfModule>  

最后启用Http2模块:

service apache2 restart  
a2enmod http2  
service apache2 restart  

编辑网站配置文件,加入Http2协议:

vim /etc/apache2/sites-enabled/default-ssl.conf  

在默认配置文件的ServerName下添加一行:

Protocols h2 http/1.1  

如果没有默认配置文件,可参考以下配置文件:

<IfModule mod_ssl.c>  
 <VirtualHost _default_:443>
 ServerAdmin webmaster@localhost
 ServerName your-domain.com
 Protocols h2 http/1.1
 DocumentRoot /var/www/html

 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined

 SSLEngine on

 SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
 SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

 <FilesMatch "\.(cgi|shtml|phtml|php)$">
 SSLOptions +StdEnvVars
 </FilesMatch>
 <Directory /usr/lib/cgi-bin>
 SSLOptions +StdEnvVars
 </Directory>

 </VirtualHost>
</IfModule>  

然后还需要将Http2协议加入Apache2配置文件中:

vim /etc/apache2/apache2.conf  

加入以下行:

Protocols h2 http/1.1  

最后重启Apache2

service apache2 restart