Apache web page compression and caching to optimize ---

Apache Web Optimization Overview

    在企业中,部署Apache后只采用默认的配置参数,会引发网站很多问题,换言之默认配置是针对以前较低的服务器配置的,以前的配置已经不适用当今互联网时代
    为了适应企业需求,就需要考虑如何提升Apache的性能与稳定性,这就是Apache优化内容

Content Optimization

Configuration page compression
to select the operating mode parameters to optimize
the configuration security chain
configuration hide the version number
...


Apache compression modules

Apache web page compression to achieve functional modules include
mod_gzip module
mod_deflate module
Apache 1.x
there is no built-in web compression technology, but third-party mod_gzip module can be used to perform compression
Apache 2.x
development at the time, built mod_deflate this module, replace mod_gzip


Enable Web compression step

Here Insert Picture Description



Examples of presentation

Deploy web page compression

The first step: On Linux remote share LAMP Kit

[root@localhost ~]# smbclient -L //192.168.10.37

     Sharename       Type      Comment
                ---------       ----      -------
                LAMP       Disk      

[root@localhost ~]# mount.cifs //192.168.10.37/LAMP /mnt

Step two: compile and install Apache

1. Unzip the source package

[root@localhost ~]# cd /mnt 
[root@localhost mnt ]# tar zvxf apr-1.6.2.tar.gz -C /opt
[root@localhost mnt ]# tar zvxf apr-util-1.6.0.tar.gz -C /opt
[root@localhost mnt ]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt

2. Install the compilation tools

[root@localhost mnt ]# cd /opt
[root@localhost opt ]# mv apr-1.6.2 httpd-2.4.29/srclib/apr
[root@localhost opt ]# mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util

[root@localhost opt ]# yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
zlib-devel \
expat-devel \
pcre \
perl

3. configure configuration

[root@localhost opt ]# cd /opt/httpd-2.4.29/
[root@localhost httpd-2.4.29 ]# ./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-deflate \
--enable-expires \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi

4. Compile and install

[root@localhost httpd-2.4.29 ]# make
......              //省略编译过程
[root@localhost httpd-2.4.29 ]# make install
......              //省略安装过程

5. Open the configuration file httpd master module relevant

[root@localhost httpd-2.4.29 ]# vim /usr/local/httpd/conf/httpd.conf

 51  Listen 192.168.235.151:80      
 //在文件的第51行替换监听地址为本主机

 52 #Listen 80
 //注释第52行内容

199 ServerName www.kgc.com:80
//在第199行设置域名

106 LoadModule deflate_module modules/mod_deflate.so
//取消第106行的注释符号#,来启用压缩模块

113 LoadModule headers_module modules/mod_headers.so
//取消第113行的注释符号#,来启用头部模块

//在末行追加一下内容
510 <IfModule mod_deflate.c>
511   AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml t    ext/jpg text/javascript text/png
512   DeflateCompressionLevel 9
513   SetOutputFilter DEFLATE
514 </IfModule>

[root@localhost httpd-2.4.29 ]# systemctl stop firewalld.service 
[root@localhost httpd-2.4.29 ]# setenforce 0
[root@localhost httpd-2.4.29 ]# cd /usr/local/httpd/bin
[root@localhost bin]# ./apachectl -t        //检查语法
Syntax OK

[root@localhost bin]# ./apachectl start     //启动Apache服务
httpd (pid 71016) already running

6. configure the site home page content

[root@localhost bin]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# vim index.html //编辑首页并放入图片
[root@localhost htdocs]# cat index.html 

<html>
 <body>
  <h1>this is test web</h1>
  <img src="game.jpg"/>
 </body>
</html>
[root@localhost htdocs]# ls
game.jpg  index.html

The third step: Use the Windows 7 client installation fiddler and packet capture tool to view web page

1. Double-click fiddler figure does not love can be installed

2. Use the browser to enter service host IP: 192.168.235.151 can access the page and see the picture
Here Insert Picture Description3. Use fiddler packet capture tool to view
Here Insert Picture Description



Configure the cache time for web pages

    通过mod_expire模块配置Apache,使用网页能在客户端浏览器缓存一段时间,以避免重复请求
    启用mod_expire模块后,会自动生成页面头部信息中的Expires标签和Cache-Control标签,从而降低客户端的访问频率和次数,达到减少不必要的流量和增加访问速度的目的

Enable Web caching feature step

Here Insert Picture Description

Deploy web caching feature (based on previous operations)

The first step: open the main configuration file httpd related modules

[root@localhost htdocs]# vim /usr/local/httpd/conf/httpd.conf

112 LoadModule expires_module modules/mod_expires.so
//取消文件第112行的#注释,开启缓存模块

//在文件末行追加以下内容
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 50 seconds"
</IfModule>

Step two: Check the syntax of the file, and then start the service

[root@localhost htdocs]# cd /usr/local/httpd/bin
[root@localhost bin]# ./apachectl -t
Syntax OK
[root@localhost bin]# ./apachectl stop      //停止服务
[root@localhost bin]# ./apachectl start     //再启动服务
httpd (pid 71016) already running  
[root@localhost bin]# systemctl stop firewalld.service    
[root@localhost bin]# setenforce 0

The third step: Use the Windows 7 client installation fiddler and packet capture tool to view web page
Here Insert Picture Description

That's all web content optimization, Thanks for reading !!!

Guess you like

Origin blog.51cto.com/14449521/2445819