Apache网页优化
一、Apache网页压缩
1、检查是否安装 mod_deflate 模块
apachectl -t -D DUMP_MODULES | grep "deflate"
2、2.如果没有安装mod_deflate 模块,重新编译安装 Apache 添加 mod_deflate 模块
systemctl stop httpd.service
cd /usr/local/httpd/conf
mv httpd.conf httpd.conf.bak
3、
yum -y install gcc gcc-c++ make pcre pcre-devel zlib-devel
cd /opt/httpd-2.4.29/
./configure \
--prefix=/usr/local/httpd \
--enable-deflate \ #加入mod_deflate 模块
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
make -j 2 && make install #编译安装
4、配置 mod_deflate 模块 启用
vim /usr/local/httpd/conf/httpd.conf
-----52行修改------
Listen 192.168.132.50:80
----105行取消注释----
LoadModule deflate_module modules/mod_deflate.so
----197行取消注释,修改------
ServerName www.kgc.com:80
-----末行添加-------
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png #代表什么类型的内容启用gzip压缩
DeflateCompressionLevel 9 #代表压缩级别,范围为1-9
SetOutputFilter DEFLATE #代表启用 deflate 模块对本站点的输出进行gzip压缩
</IfModule>
5、检查安装情况,启动服务
apachectl -t #验证配置文件的配置是否正确
apachectl -t -D DUMP_MODULES | grep "deflate" #检查是否安装 mod_deflate 模块
deflate_module (shared) #出现该段,说明安装模块成功
6、添加网页内容,进行后续的测试
cd /usr/local/httpd/htdocs/
#将自己想要测试的图片上传到该/htdocs目录下
vim /usr/local/httpd/htdocs/index.html
在Linux系统中,打开火狐浏览器,右击点查看元素
选择 网络→重新载入→选择 HTML、WS、其他
访问 http://192.168.132.50 ,查看响应头中包含 Content-Encoding: gzip
进入虚拟机进行验证
二、网页缓存
1.检查是否安装 mod_expires 模块
apachectl -t -D DUMP_MODULES | grep "expires"
2.如果没有安装mod_expires 模块,重新编译安装 Apache 添加 mod_expires模块
systemctl stop httpd.service
cd /usr/local/httpd/conf
mv httpd.conf httpd.conf.bak2
y
um -y install gcc gcc-c++ pcre pcre-devel zlib-devel
cd /opt/httpd-2.4.29/
./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-deflate \
--enable-expires #加入mod_expires 模块
make && make install
3.配置 mod_expires 模块启用
vim /usr/local/httpd/conf/httpd.conf
52行-----修改
Listen 192.168.132.50:80
111行-----取消注释
LoadModule expires_module modules/mod_expires.so #开启mod_expires 模块
199行-----取消注释,修改
ServerName www.zs.com:80
末行添加---
<IfModule mod_expires.c>
ExpiresActive On #打开网页缓存功能
ExpiresDefault "access plus 60 seconds" #设置缓存60秒
</IfModule>
4、检查安装情况,启动服务
apachectl -t #验证配置文件的配置是否正确
apachectl -t -D DUMP_MODULES | grep "expires" #检查 mod_deflate 模块是否已安装
deflate_module (shared) #已安装的正确结果
systemctl start httpd.service
5.测试缓存是否生效
cat /usr/local/httpd/htdocs/index.html
三、隐藏版本信息
vim /usr/local/httpd/conf/httpd.conf
491行-----取消注释
Include conf/extra/httpd-default.conf
vim /usr/local/httpd/conf/extra/httpd-default.conf
systemctl start httpd.service
emctl start httpd.service
浏览器访问 http://192.168.132.50 ,双击200消息查看 Server 项