Apache网页优化
优化概述
压缩功能优化
配置Apache的网页压缩功能,是使用gzip压缩算法对网页内容进行压缩后再传输到客户端浏览器
先把宿主机的共享目录共享到Linux下
[[email protected] ~]# smbclient -L //192.168.100.23/ #访问宿主机的共享文件
Enter SAMBA\root‘s password:
OS=[Windows 10 Enterprise 17763] Server=[Windows 10 Enterprise 6.3]
Sharename Type Comment
--------- ---- -------
ADMIN$ Disk 远程管理
C$ Disk 默认共享
D$ Disk 默认共享
E$ Disk 默认共享
F$ Disk 默认共享
G$ Disk 默认共享
IPC$ IPC 远程 IPC
LAMP Disk
LAMP-C7 Disk
share Disk
Users Disk
Connection to 192.168.100.23 failed (Error NT_STATUS_RESOURCE_NAME_NOT_FOUND)
NetBIOS over TCP disabled -- no workgroup available
[[email protected] ~]# mkdir /abc #创建一个挂载点
[[email protected] ~]# mount.cifs //192.168.100.23/LAMP-C7 /abc/ #挂载
Password for [email protected]//192.168.100.23/LAMP-C7:
[[email protected] ~]# cd /abc
[[email protected] abc]# ls #我们的源码包都在里面
apr-1.6.2.tar.gz Discuz_X2.5_SC_UTF8.zip mysql-5.6.26.tar.gz
apr-util-1.6.0.tar.gz fiddler.exe php-5.6.11.tar.bz2
awstats-7.6.tar.gz httpd-2.4.29.tar.bz2
cronolog-1.6.2-14.el7.x86_64.rpm LAMP-php5.6.txt
解压,移动
cronolog-1.6.2-14.el7.x86_64.rpm LAMP-php5.6.txt
[[email protected] abc]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt #解压到OPT底下
[[email protected] abc]# tar zxvf apr-1.6.2.tar.gz -C /opt/
[[email protected] abc]# tar zxvf apr-util-1.6.0.tar.gz -C /opt/
[[email protected] abc]# cd /opt
[[email protected] opt]# ls
apr-1.6.2 apr-util-1.6.0 httpd-2.4.29 rh
[[email protected] opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr #移动到这个目录底下
[[email protected] opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
[[email protected] opt]# ls
httpd-2.4.29 rh
安装环境包
[[email protected] opt]# cd httpd-2.4.29 /
calhost httpd-2.4.29]#
yum -y install gcc gcc-c++ make pcre-devel zlib-devel expat-devel pcre perl
已安装:
gcc.x86_64 0:4.8.5-39.el7 gcc-c++.x86_64 0:4.8.5-39.el7
pcre-devel.x86_64 0:8.32-17.el7 zlib-devel.x86_64 0:1.2.7-18.el7
作为依赖被安装:
cpp.x86_64 0:4.8.5-39.el7 glibc-devel.x86_64 0:2.17-292.el7
glibc-headers.x86_64 0:2.17-292.el7 kernel-headers.x86_64 0:3.10.0-1062.4.1.el7
libmpc.x86_64 0:1.0.1-3.el7 libstdc++-devel.x86_64 0:4.8.5-39.el7
作为依赖被升级:
glibc.x86_64 0:2.17-292.el7 glibc-common.x86_64 0:2.17-292.el7
libgcc.x86_64 0:4.8.5-39.el7 libgomp.x86_64 0:4.8.5-39.el7
libstdc++.x86_64 0:4.8.5-39.el7 zlib.x86_64 0:1.2.7-18.el7
完毕!
安装Apache底层功能
calhost httpd-2.4.29]#
./configure --prefix=/usr/local/httpd \ #指定路径
--enable-deflate \ #压缩功能
--enable-so \ #核心模块开启
--enable-rewrite \ #开启重写功能,防盗链
--enable-charset-lite \ #支持字符集
--enable-cgi#通用网关接口
编译安装
[[email protected] opt]#make
[[email protected] opt]#make install
mkdir /usr/local/httpd/manual
make[1]: 离开目录“/opt/httpd-2.4.29”
[[email protected] httpd-2.4.29]#
做软链接以后到主配置文件方便了
[[email protected] httpd-2.4.29]# cd /usr/local/httpd/
[[email protected] httpd]# ls
bin cgi-bin error icons lib man modules
build conf htdocs include logs manual
[[email protected] httpd]# cd conf
[[email protected] conf]# ls
extra httpd.conf magic mime.types original
[[email protected] conf]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf
配置主配置文件
Listen 192.168.136.136:80 #开启监听地址
#Listen 80 #关闭IPV6监听地址
ServerName www.kgc.com:80 #开启域名服务器
LoadModule deflate_module modules/mod_deflate.so #前面#去掉开启功能
#文件末尾加上一段话
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain test/css test/xml test/javascript test/jpg test/png
#支持各种类型的文件
DeflateCompressionLevel 9 #9高压缩比
SetOutputFilter DEFLATE #默认过滤器
</IfModule>
#检查语法有没有错误
[[email protected] conf]# /usr/local/httpd/bin/apachectl -t
Syntax OK
开启服务,查看端口有没有被提供出来
[[email protected] conf]# /usr/local/httpd/bin/apachectl start
[[email protected] conf]# netstat -ntap | grep 80
tcp 0 0 192.168.136.136:80 0.0.0.0:* LISTEN 52832/httpd
[[email protected] htdocs]# cd /usr/local/httpd/
[[email protected] httpd]# cd bin/
[[email protected] bin]# systemctl stop firewalld.service
[[email protected] bin]# setenforce 0
查看压缩模块有没有被启动起来
[[email protected] bin]# ./apachectl -t -D DUMP_MODULES | grep "deflate"
deflate_module (shared)
到宿主机目录下放入一种图片去测试
想看到它压缩的功能,我们放一张照片到默认网页中去试一试
[[email protected] abc]# cd /abc/
[[email protected] abc]# ls
apr-1.6.2.tar.gz Discuz_X2.5_SC_UTF8.zip mysql-5.6.26.tar.gz
apr-util-1.6.0.tar.gz fiddler.exe php-5.6.11.tar.bz2
awstats-7.6.tar.gz httpd-2.4.29.tar.bz2 tu9892_14.jpg #这是我们的图片
cronolog-1.6.2-14.el7.x86_64.rpm LAMP-php5.6.txt
[[email protected] abc]# cp tu9892_14.jpg /usr/local/httpd/htdocs/ #把这种照片复制到默认首页中
[[email protected] abc]# cd /usr/local/httpd/htdocs/
[[email protected] htdocs]# ls
index.html tu9892_14.jpg
[[email protected] htdocs]# vim index.html
<html><body><h1>It works!</h1>
<img src="tu9892_14.jpg"/>
</body></html>
去客户机测试一下压缩功能
网页之缓存时间优化
通过mod_expire模块配置Apache,使网页在客户端浏览器缓存一段时间,以免重复请求,降低客户段的访问频率和次数,达到减少不必要的流量和增加访问速度的目的
前面的步骤更压缩一样,我们直接到主配置文件中开启缓存模块
Listen 192.168.136.136:80 #开启监听地址
#Listen 80 #关闭IPV6监听地址
ServerName www.kgc.com:80 #开启域名服务器
LoadModule expires_module modules/mod_expires.so #开启模块
配置文件末尾添加
<IfModule mod_expires.c>
ExpiresActive On #开启功能
ExpiresDefault "access plus 31104000 seconds" #开启缓存时间31104000s
</IfModule>
验证语法格式有没有错误
[[email protected] conf]# cd /usr/local/httpd/
[[email protected] httpd]# cd bin/
[[email protected] bin]# ./apachectl -t
Syntax OK
开启服务查看端口有没有被提供出来
[[email protected] bin]# ./apachectl start
[[email protected] bin]# netstat -ntap | grep 80
tcp 0 0 192.168.136.136:80 0.0.0.0:* LISTEN 52832/httpd
[[email protected] bin]# systemctl stop firewalld.service
[[email protected] bin]# setenforce 0
查看缓存模块有没有被启动
[[email protected] bin]# ./apachectl -t -D DUMP_MODULES | grep "expires"
expires_module (shared)
去客户段去测试一下
原文地址:https://blog.51cto.com/14449524/2445328
时间: 2024-10-05 06:49:52