启用并加载 SSL and HTTP/2 模块:

sudo a2enmod ssl
sudo a2enmod http2

激活这些新模块:

sudo systemctl restart apache2

要在Apache Web服务器上启用HTTP / 2,请将Protocols h2 http/1.1添加到全局Apache配置中或特定虚拟主机内部。

下面是可用于在某些虚拟主机中启用HTTP / 2的最小虚拟服务器配置:

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/public_html/example.com
    SSLEngine on
    SSLCertificateKeyFile /path/to/private.key
    SSLCertificateFile /path/to/cert.pem
    SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
    Protocols h2 http/1.1
</VirtualHost>
启用后的效果
启用后的效果

故障排除

AH10034: The mpm module (prefork.c) is not supported by mod_http2.

Starting from Apache 2.4.27, the Apache MPM (Multi-Processing Module) prefork no longer supports HTTP/2. This will be indicated in your Apache error log as follows: AH10034: The mpm module (prefork.c) is not supported by mod_http2. The mpm determines how things are processed in your server. HTTP/2 has more demands in this regard and the currently selected mpm will just not do. This is an advisory warning. Your server will continue to work, but the HTTP/2 protocol will be inactive.

要解决此问题,请选择其他MPM:event or worker

If you are using PHP, it is likely that PHP is integrated to Apache via the mod_php module, which requires the prefork MPM. If you switch out from preform MPM, you will need to use PHP as FastCGI. To switch to php-fpm, you can do as folllwing. Please note that this assumes you have PHP installed from ondrej/php repository on Ubuntu. The PHP package names could be different in other repositories. Change package name and apt-get commands to match your PHP vendor and package manager.

apachectl stop
apt-get install php7.1-fpm # Install the php-fpm from your PHP repository. This package name depends on the vendor.
a2enmod proxy_fcgi setenvif
a2enconf php7.1-fpm # Again, this depends on your PHP vendor.
a2dismod php7.1 # This disables mod_php.
a2dismod mpm_prefork # This disables the prefork MPM. Only one MPM can run at a time.
a2enmod mpm_event # Enable event MPM. You could also enable mpm_worker.
apachectl start

Why http2 doesn’t work on my Apache/2.4.29?