在 Apache 中自动启用 Gzip 和缓存过期

在 Apache 中自动启用 Gzip 和缓存过期

apache2.conf每次我创建新的服务器环境时,我都不需要复制粘贴以下代码来启用 Gzip 压缩和缓存过期,有没有办法让它们从 Apache 本身使用一些 CLI 命令出现在那里?

## ENABLE GZIP
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>
## ENABLE GZIP ##

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##

更新

有没有其他方法可以自动添加这些/etc/apache2/apache2.conf而无需粘贴或includes?

答案1

为了尽可能轻松地部署您的配置,最好的方法是运行脚本(或运行每个命令)。

您不需要列出的所有更改。Apache2 已经带有与mods-available/deflate.conf您的类似的更改。只需启用它即可获得 GZIP 支持。

该脚本将是(我称之为a2gzip.sh):

#!/bin/bash

cat > /etc/apache2/mods-available/expires.conf << EOF
<IfModule mod_expires.c>
        ExpiresActive On
        ExpiresByType image/jpg "access 1 year"
        ExpiresByType image/jpeg "access 1 year"
        ExpiresByType image/gif "access 1 year"
        ExpiresByType image/png "access 1 year"
        ExpiresByType text/css "access 1 month"
        ExpiresByType application/pdf "access 1 month"
        ExpiresByType application/javascript "access 1 month"
        ExpiresByType application/x-javascript "access 1 month"
        ExpiresByType application/x-shockwave-flash "access 1 month"
        ExpiresByType image/x-icon "access 1 year"
        ExpiresDefault "access 2 days"
</IfModule>
EOF

a2enmod deflate expires
systemctl reload apache2

用于sudo ./a2gzip.sh运行脚本。

相关内容