Apache Web optimization (a) -------- web page compression and caching

Apache Web optimization concept

In business, only after the deployment of Apache default configuration parameters, can cause a lot of problems the site, in other words the default configuration is for the previous low
configuration server, the previous configuration no longer applicable in today's Internet era, in order to meet business needs, we need Apache's consider how to enhance sexual
energy and stability, which is the content of Apache optimization.

Content Optimization:

  • Configuration page compression

  • Configure web cache time

  • Configuring security chain

  • Hide the version number of the configuration

gzip Introduction

Configuring Apache Web compression, using gzip compression algorithm to compress and then transmitted to the client browser to web content.

effect

  • Reduce the number of bytes transmitted over the network, speed up page load

  • Save traffic, improve the user's browsing experience

  • gzip search bow | engine crawlers have a better relationship

Apache compression modules

Apache web page compression to achieve functional modules include

mod_gzip module
mod_deflate module

Apache 1.x

No built-in web compression techniques, but can be a third party E mod_gzip perform compression module.

Apache 2.x

During development, we built mod_deflate this module, replace mod_gzip.

mod_gzip module and module mod_deflate

  • Both use gzip compression algorithm, similar to how it works

  • mod_deflate slightly faster compression, and compression ratio is slightly higher mod_gzip

  • mod_gzip occupation of the server CPU to be higher

  • High-traffic server, use mod_deflate may be faster than the speed of loading mod_gzip

Configuration page compression test

(1) in the host kits we need to share out.

Apache Web optimization (a) -------- web page compression and caching

(2) by Samba service kit to mount the Linux system.

[root@localhost ~]# smbclient -L //192.168.100.50/   //查看共享
Enter SAMBA\root's password:      //匿名共享,没有密码,直接回车
OS=[Windows 10 Enterprise LTSC 2019 17763] Server=[Windows 10 Enterprise LTSC 2019 6.3]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       远程 IPC
    share           Disk      
    tools           Disk      
    Users           Disk      
Connection to 192.168.100.50 failed (Error NT_STATUS_RESOURCE_NAME_NOT_FOUND)
NetBIOS over TCP disabled -- no workgroup available
[root@localhost ~]# mkdir /mnt/tools    //创建挂载目录
[root@localhost ~]# mount.cifs //192.168.100.50/tools /mnt/tools/     //挂载
Password for root@//192.168.100.50/tools:  
[root@localhost ~]# cd /mnt/tools/    //进入挂载目录
[root@localhost tools]# ls     //查看
awstats-7.6.tar.gz                extundelete-0.2.4.tar.bz2  forbid.png                 jdk-8u191-windows-x64.zip  LAMP-C7
cronolog-1.6.2-14.el7.x86_64.rpm  fiddler.exe                intellijideahahau2018.rar  john-1.8.0.tar.gz          picture.jpg
[root@localhost tools]# 

(3) compile the source Apache installation services to extract the archive directory "/ opt /".

[root@localhost tools]# cd LAMP-C7/       //切换目录
[root@localhost LAMP-C7]# ls
apr-1.6.2.tar.gz       Discuz_X2.5_SC_UTF8.zip  LAMP-php5.6.txt      php-5.6.11.tar.bz2
apr-util-1.6.0.tar.gz  httpd-2.4.29.tar.bz2     mysql-5.6.26.tar.gz
[root@localhost LAMP-C7]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt/     //解压
[root@localhost LAMP-C7]# tar zxvf apr-1.6.2.tar.gz -C /opt/           //解压
[root@localhost LAMP-C7]# tar zxvf apr-util-1.6.0.tar.gz -C /opt/     //解压

(4) to enter "/ opt /" directory, move the package to the two apr "httpd-2.4.29 / srclib /" directory, rename.

[root@localhost LAMP-C7]# cd /opt/    
[root@localhost opt]# ls
apr-1.6.2  apr-util-1.6.0  httpd-2.4.29  rh
[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

(5) Enter "httpd-2.4.29 /" directory, and then install the build environment package.

[root@localhost opt]# ls
httpd-2.4.29  rh
[root@localhost opt]# cd httpd-2.4.29/
[root@localhost httpd-2.4.29]# ls
ABOUT_APACHE     ap.d          CHANGES         docs         httpd.spec      libhttpd.dep  Makefile.win   README            srclib
acinclude.m4     build         CMakeLists.txt  emacs-style  include         libhttpd.dsp  modules        README.cmake      support
Apache-apr2.dsw  BuildAll.dsp  config.layout   httpd.dep    INSTALL         libhttpd.mak  NOTICE         README.platforms  test
Apache.dsw       BuildBin.dsp  configure       httpd.dsp    InstallBin.dsp  LICENSE       NWGNUmakefile  ROADMAP           VERSIONING
apache_probes.d  buildconf     configure.in    httpd.mak    LAYOUT          Makefile.in   os             server
[root@localhost httpd-2.4.29]# 
[root@localhost httpd-2.4.29]# yum -y install \
> gcc \
> gcc-c++ \
> make \
> pcre \
> pcre-devel \
> expat-devel \
> zlib-devel \
> perl
......//省略安装过程

(6) configure the Apache server.

[root@localhost httpd-2.4.29]# ./configure \
> --prefix=/usr/local/httpd \    //安装路径
> --enable-deflate \     //启用压缩模块支持
> --enable-expires \   //启用缓存模块支持
> --enable-so \     //启用动态加载模块支持
> --enable-rewrite \     //启用网页地址重写功能
> --enable-charset-lite \    //启用字符集支持
> --enable-cgi    //启用CGI脚本程序支持

(7) compile and install Apache service.

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

(8) for Apache service configuration file is modified.

[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf     //创建软链接,方便使用
[root@localhost httpd-2.4.29]# 
[root@localhost httpd-2.4.29]# vim /etc/httpd.conf

Listen 192.168.52.133:80    //开启IPv4监听
#Listen 80     //注释IPv6监听

ServerName www.abc.com:80    //设置域名

LoadModule headers_module modules/mod_headers.so    //请求头部,默认开启

LoadModule deflate_module modules/mod_deflate.so     //开启压缩模块

LoadModule filter_module modules/mod_filter.so     //过滤器,默认开启

<IfModule mod_deflate.c>      //尾行添加压缩模块信息
  AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png   //启用压缩的内容
  DeflateCompressionLevel 9     //压缩级别,9级
  SetOutputFilter DEFLATE      //启用deflate模块对本站点的输出进行gzip压缩
</IfModule>

(9) Check the configuration file format, in the correct format.

[root@localhost httpd-2.4.29]# /usr/local/httpd/bin/apachectl -t
Syntax OK
[root@localhost httpd-2.4.29]# 

(10) to move the "apachectl" file under "/ usr / local / httpd / bin /" directory to the "/etc/init.d/" directory and adds identification chkconfig configuration file at the beginning, and then add them as standard Linux system services

[root@localhost httpd-2.4.29]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd     //复制
[root@localhost httpd-2.4.29]# vim /etc/init.d/httpd     //在配置文件添加两行声明

# chkconfig: 35 85 21    //服务识别参数,在级别3、5中启动:启动和关闭的顺序分别为85、21
# description: Apache is a World Wide Web server      //服务描述信息

[root@localhost httpd-2.4.29]# chkconfig --add httpd     //将httpd服务添加为系统服务
[root@localhost httpd-2.4.29]# 
[root@localhost httpd-2.4.29]# service httpd start     //启动Apache服务
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/bin/* /usr/local/bin/    //将Apache服务的命令文件,建立软链接到易于系统识别的目录
[root@localhost httpd-2.4.29]# 

(11) Check the module installation was successful.

[root@localhost httpd-2.4.29]# apachectl -t -D DUMP_MODULES | grep "deflate"    //验证模块
deflate_module (shared)
[root@localhost httpd-2.4.29]# 
[root@localhost httpd-2.4.29]# systemctl stop firewalld.service     //关闭防火墙
[root@localhost httpd-2.4.29]# setenforce 0    //关闭增强性安全功能
[root@localhost httpd-2.4.29]# 

Copy "picture.jpg" Pictures (12) to mount the directory service site to the Apache directory "/ usr / local / httpd / htdocs /" down, and then add pictures to the page file.

[root@localhost httpd-2.4.29]# cd /mnt/tools/     //切换目录
[root@localhost tools]# ls
awstats-7.6.tar.gz                extundelete-0.2.4.tar.bz2  forbid.png                 jdk-8u191-windows-x64.zip  LAMP-C7
cronolog-1.6.2-14.el7.x86_64.rpm  fiddler.exe                intellijideahahau2018.rar  john-1.8.0.tar.gz          picture.jpg
[root@localhost tools]# cp picture.jpg /usr/local/httpd/htdocs/     //复制图片
[root@localhost tools]# cd /usr/local/httpd/htdocs/    //切换目录
[root@localhost htdocs]# ls     //查看
index.html  picture.jpg
[root@localhost htdocs]# vim index.html    //编辑首页文件

<html>
 <body>
  <h1>It works!</h1>
  <img src="picture.jpg">
 </body>
</html>

(13) is mounted in the capture tool fiddler win10 host.

Apache Web optimization (a) -------- web page compression and caching

(14) Visit the Apache site in win10 host.

Apache Web optimization (a) -------- web page compression and caching

(15) to view the contents of packet capture, you can see Headers, the picture has been gzip compression.

Apache Web optimization (a) -------- web page compression and caching

Apache caching module

Configuring Apache by mod_expire module, so that the page can be in the client browser cache a period of time, to avoid repeated requests enabled mod_expire module, automatically generates a page header information Expires tag and Cache-Control labels, thereby reducing the frequency of access client and the number of times, to reduce unnecessary traffic and increase access speed purposes.

Configure web cache time experiment

(1) Following the above experiment, when configuring cache module Apache service added together. So directly to the Apache configuration file is modified.

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

LoadModule expires_module modules/mod_expires.so    //开启缓存模块

<IfModule mod_expires.c>     //尾行添加缓存模块信息
  ExpiresActive On       //缓存开启
  ExpiresDefault "access plus 50 seconds"     //缓存时间50秒
</IfModule>

(2) Check the Apache configuration file format, in the correct format.

[root@localhost htdocs]# apachectl -t
Syntax OK
[root@localhost htdocs]# 

(3) to restart the service, view the listening port

[root@localhost htdocs]# service httpd stop    //关闭服务
[root@localhost htdocs]# service httpd start    //开启服务
[root@localhost htdocs]# 
[root@localhost htdocs]# netstat -ntap | grep 80     //检查监听端口
tcp        0      0 192.168.52.133:80       0.0.0.0:*               LISTEN      83296/httpd         
[root@localhost htdocs]# 

(4) checks the cache module is installed successfully.

[root@localhost htdocs]# apachectl -t -D DUMP_MODULES | grep "expires"   //验证模块
 expires_module (shared)
[root@localhost htdocs]#

(5) with a win10 host access Apache site again.

Apache Web optimization (a) -------- web page compression and caching

(6) View capture, Headers you can see the cache time is 50 seconds.

Apache Web optimization (a) -------- web page compression and caching

(7) We will Apache configuration file caching service time to 30 seconds, and then restart the service.

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

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 30 seconds"     //将50改为30
</IfModule>

[root@localhost htdocs]# service httpd stop     //关闭服务
[root@localhost htdocs]# service httpd start     //启动服务
[root@localhost htdocs]#

(8) Apache service again visit the site with a win10 host.

Apache Web optimization (a) -------- web page compression and caching

(9) View capture, Headers you can see from the cache time has been changed to 30 seconds 50 seconds.

Apache Web optimization (a) -------- web page compression and caching

Guess you like

Origin blog.51cto.com/14449541/2446257