Apache Web page optimization: Web page compression and caching

Apache Web page optimization concept

In an enterprise, only the default configuration parameters are used after deploying Apache, which will cause many problems in the website. In other words, the default configuration is for the previous lower configuration.
Server configuration, the previous configuration is not suitable for today's Internet era. In order to meet the needs of enterprises, we need to consider how to improve the performance of Apache.
Ability and stability, which is what Apache optimizes.

Optimization content:

  • Configure web page compression

  • Configure page cache time

  • Configure anti-theft chain

  • Configure hidden version number

gzip introduction

To configure Apache's Web compression function, gzip compression algorithm is used to compress the web content and then transfer it to the client browser.

Effect

  • Reduce the number of bytes transmitted by the network and speed up the loading of web pages

  • Save traffic and improve users' browsing experience

  • gzip has a better relationship with the grabbing tool of search engine

Apache's compression module

The function modules of Apache to realize web page compression include

Mod? Gzip module
Mod? Deflate module

Apache 1.x

There is no built-in web page compression technology, but compression can be performed using a third-party E mod ﹣ gzip module.

Apache 2.x

At the time of development, we built the mod ﹣ deflate module to replace mod ﹣ gzip.

Mod gzip module and mod deflate module

  • Both of them use gzip compression algorithm, with similar operation principle.

  • Mod ABCD deflate compression speed is slightly faster, while mod ABCD gzip compression ratio is slightly higher

  • Mod ﹣ gzip consumes more CPU of the server

  • For high traffic servers, using mod ﹣ deflate may load faster than mod ﹣ gzip

Experiment of configuring web compression function

(1) share the toolkit we need on the host.

(2) mount the toolkit to Linux system through Samba service.

[root@localhost ~]# smbclient -L //192.168.100.50 / / view share
Enter SAMBA\root's password:      //Anonymous sharing, no password, enter directly
OS=[Windows 10 Enterprise LTSC 2019 17763] Server=[Windows 10 Enterprise LTSC 2019 6.3]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       Long-range 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 / / create the mount directory
[root@localhost ~]# mount.cifs //192.168.100.50/tools /mnt/tools / / Mount
Password for root@//192.168.100.50/tools:  
[root@localhost ~]# cd /mnt/tools / / / enter the mount directory
[root@localhost tools]# ls / / view
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) extract the compressed package of the source code compilation and installation of Apache service to the "/ opt /" directory.

[root@localhost tools]# cd LAMP-C7 / / / switch directories
[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 / / / unzip
[root@localhost LAMP-C7]# tar zxvf apr-1.6.2.tar.gz -C /opt / / / unzip
[root@localhost LAMP-C7]# tar zxvf apr-util-1.6.0.tar.gz -C /opt / / / unzip

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

[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 the "httpd-2.4.29 /" directory, and then install the required environment package for compilation.

[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
......//Omit installation process

(6) configure the Apache server.

[root@localhost httpd-2.4.29]# ./configure \
> --prefix=/usr/local/httpd \    //Installation path
> --enable-deflate \     //Enable compression module support
> --enable-expires \   //Enable cache module support
> --enable-so \     //Enable dynamic load module support
> --enable-rewrite \     //Enable Web address rewriting
> --enable-charset-lite \    //Enable character set support
> --enable-cgi    //Enable CGI scripting support

(7) compile and install the Apache service.

[root@localhost httpd-2.4.29]# make && make install
......//Omit compilation and installation
[root@localhost httpd-2.4.29]#

(8) modify the Apache service configuration file.

[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf / / create a soft link for easy use.
[root@localhost httpd-2.4.29]# 
[root@localhost httpd-2.4.29]# vim /etc/httpd.conf

Listen 192.168.52.133:80    //Enable IPv4 monitoring
#Listen 80 / / Note IPv6 listening

ServerName www.abc.com:80    //Set domain name

LoadModule headers_module modules/mod_headers.so    //Request header, on by default

LoadModule deflate_module modules/mod_deflate.so     //Turn on compression module

LoadModule filter_module modules/mod_filter.so     //Filter, on by default

<IfModule mod_deflate.c>      //Add compression module information in the last line
  AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png   //Compressed content enabled
  DeflateCompressionLevel 9     //Compression level, level 9
  SetOutputFilter DEFLATE      //Enable deflate module to gzip the output of this site
</IfModule>

(9) check the format of the configuration file. The format is correct.

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

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

[root@localhost httpd-2.4.29]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd / / copy
[root@localhost httpd-2.4.29]# vim /etc/init.d/httpd / / add two lines of declaration to the configuration file

# chkconfig: 35 85 21 / / service identification parameter, start in Level 3 and level 5: the order of start and stop is 85 and 21 respectively.
# description: Apache is a World Wide Web server / / service description

[root@localhost httpd-2.4.29]# chkconfig --add httpd / / add httpd service as system service
[root@localhost httpd-2.4.29]# 
[root@localhost httpd-2.4.29]# service httpd start / / start the Apache service
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/bin/* /usr/local/bin / / / set up a soft link to the command file of the Apache service to a directory easy to identify by the system
[root@localhost httpd-2.4.29]# 

(11) check whether the module is installed successfully.

[root@localhost httpd-2.4.29]# Apachectl - t - D dump | modules | grep "deflate" / / validate modules
deflate_module (shared)
[root@localhost httpd-2.4.29]# 
[root@localhost httpd-2.4.29]# systemctl stop firewalld.service / / turn off the firewall
[root@localhost httpd-2.4.29]# setenforce 0 / / turn off enhanced security
[root@localhost httpd-2.4.29]# 

(12) copy the "picture.jpg" picture of the mount directory to the Apache service site directory "/ usr/local/httpd/htdocs /", and then add the picture to the homepage file.

[root@localhost httpd-2.4.29]# cd /mnt/tools / / switch directories
[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 / / copy picture
[root@localhost tools]# cd /usr/local/httpd/htdocs / / switch directories
[root@localhost htdocs]# ls / / view
index.html  picture.jpg
[root@localhost htdocs]# vim index.html / / edit the homepage file

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

(13) install the capture tool fiddler on the win10 host.

(14) visit Apache website on win10 host.

(15) check the contents of the packet capture. You can see that the pictures in the Headers have been gzip compressed.

Apache's caching module

Apache is configured through the mod ﹣ expire module, so that the web page can be cached in the client browser for a period of time, so as to avoid repeated requests. After the mod ﹣ expire module is enabled, the Expires tag and cache control tag in the page header information will be automatically generated, so as to reduce the client's access frequency and times, and achieve the purpose of reducing unnecessary traffic and increasing access speed.

Configure Web cache time experiment

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

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

LoadModule expires_module modules/mod_expires.so    //Open cache module

<IfModule mod_expires.c>     //Add cache module information in last line
  ExpiresActive On       //Cache opening
  ExpiresDefault "access plus 50 seconds"     //Cache time 50 seconds
</IfModule>

(2) check the format of Apache configuration file. The format is correct.

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

(3) restart the service and view the listening port

[root@localhost htdocs]# service httpd stop / / close the service
[root@localhost htdocs]# service httpd start / / start the service
[root@localhost htdocs]# 
[root@localhost htdocs]# netstat -ntap | grep 80 / / check the listening port
tcp        0      0 192.168.52.133:80       0.0.0.0:*               LISTEN      83296/httpd         
[root@localhost htdocs]# 

(4) check whether the cache module is installed successfully.

[root@localhost htdocs]# Apachectl - t - D dump | modules | grep "expires" / / verify modules
 expires_module (shared)
[root@localhost htdocs]#

(5) use win10 host to visit Apache site again.

(6) check the packet capture. You can see that the cache time in Headers is 50 seconds.

(7) we change the cache time in the Apache service configuration file 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"     //Change 50 to 30
</IfModule>

[root@localhost htdocs]# service httpd stop / / close the service
[root@localhost htdocs]# service httpd start / / start the service
[root@localhost htdocs]#

(8) use win10 host again to visit Apache service site.

(9) check the packet capture. You can see that the cache time in Headers has changed from 50 seconds to 30 seconds.

Keywords: Linux Apache vim Windows RPM

Added by bobbfwed on Tue, 29 Oct 2019 10:13:44 +0200