Compression and caching for Apache web page optimization

Posted by jaiswal on Tue, 12 Nov 2019 01:10:50 +0100

Overview of Apache Web Page Optimization

In the enterprise, when Apache is deployed, only the default configuration parameters are used.
This causes a lot of problems for the website, in other words, the default configuration is for a lower level than before
Server Configuration, Previous Configuration No longer Applies to Today's Internet Age
To meet the needs of the enterprise, you need to consider how to enhance Apache's sexuality
Energy and stability, which is what Apache optimizes

Introduction to gzip

Configure Apache's web compression capabilities by using the gzip compression algorithm
Compress web content before transferring to client browsers
Function of ^
Reduce the number of bytes transmitted over the network to speed up the loading of web pages
Reduce traffic and improve users'browsing experience
** gzip has a better relationship with search engine grabbing tools

Apache web page compression module includes

mod_gzip module
moddeflate module
Apache 1.x
There is no built-in Web page compression technology, but third-party mod gzip modules can be used to perform compression
Apache 2.x
During development, mod deflate was built in to replace mod gzip
mod gzip module and mod deflate module
Both use gzip compression algorithm and work similarly
Compression speed of mod deflate is slightly faster, while compression of mod _gzip is slightly higher than that of mod_gzip, which takes up more CPU on the server
High-traffic servers, using mod_deflate may load faster than mod_gzip
Faster

Configure Web Page Compression

First check if the mod_deflate module is installed
Implement the apachectl-t-D DUMP_MODULES command
If there is no deflate_module (static) in the output, it is not installed at compile time
mod_deflate module
Recompile installation if not installed
●./configure --enable-deflate...
●make && make install

_Configure to turn on gzip functionality in configuring httpd.conf
●AddOutputFilterByType DEFLATE texthtml text/plain text/css
text/xml text/javascript
●DeflateCompressionL evel
●SetOutputFilter DEFL ATE
The first line represents what gzip compression is enabled for
The second row represents the compression level
The third line represents gzip compression of the output of this site by enabling the deflate module

Apache Compression Practice

Install Apache Service

Remote mount package folder to local and view

[root@localhost ~]# Mkdir/abc //Create mount point
[root@localhost ~]# Mount.cifs //192.168.100.7/LAMP-C7/abc///mount
Password for root@//192.168.100.10/LAMP-C7:  
[root@localhost ~]# cd /abc/
[root@localhost abc]# ls
apr-1.6.2.tar.gz       cronolog-1.6.2-14.el7.x86_64.rpm  LAMP-php5.6.txt
apr-util-1.6.0.tar.gz  Discuz_X2.5_SC_UTF8.zip           mysql-5.6.26.tar.gz
awstats-7.6.tar.gz     httpd-2.4.29.tar.bz2              php-5.6.11.tar.bz2

Unzip the source package into the / opt directory and move the httpd package

[root@localhost abc]# tar zxvf apr-1.6.2.tar.gz -C /opt/
[root@localhost abc]# tar zxvf apr-util-1.6.0.tar.gz -C /opt/
[root@localhost abc]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt/
[root@localhost abc]# cd /opt/
[root@localhost opt]# ls
apr-1.6.2  apr-util-1.6.0  httpd-2.4.29  httpd.txt  rh
[root@lamp opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr        [root@lamp opt]#  mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util

Install packages necessary for the lab environment

yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
zlib-devel \
expat-devel \
pcre \
perl   

Configure installation directory and modules

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

Compile and Install Apache

make && make install

Modify Apache configuration file

[root@localhost httpd-2.4.29]# cd /usr/local/httpd/
[root@localhost httpd]# ls
bin    cgi-bin  error   icons    lib   man     modules
build  conf     htdocs  include  logs  manual
[root@localhost httpd]# cd conf
[root@localhost conf]# ls
extra  httpd.conf  magic  mime.types  original
[root@localhost conf]# vim httpd.conf 
[root@localhost conf]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf
[root@localhost conf]# vim /etc/httpd.conf 
/deflate Search for keywords to find the row of keywords    
LoadModule deflate_module modules/mod_deflate.so        //Remove Comments to Open Service
/headers Search for keywords to find the row of keywords
LoadModule headers_module modules/mod_headers.so        //Remove Comments to Open Service
/filter Search for keywords to find the row of keywords
LoadModule filter_module modules/mod_filter.so          //Remove Comments to Open Service
//Use/Listen to find the location of keywords
//Annotate ipv6, turn on ipv4 and change the listening port to local address
Listen 192.168.131.129:80
#Listen 80
//Use/ServerName to find keywords and make the following modifications
ServerName www.kgc.com:80

//Insert the following at the end of the file
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/ipg text/png
  DeflateCompressionLevel 9
  SetOutputFilter DEFLATE
</IfModule>
[root@localhost conf]# /usr/local/httpd/bin/apachectl -t
Syntax OK
//Verify grammar is correct

[root@localhost bin]# . /apachectl start //start service
[root@localhost bin]# netstat -ntap | grep 80
tcp        0      0 192.168.35.131:80       0.0.0.0:*               LISTEN      35887/httpd
[root@localhost bin]# systemctl stop firewalld.service //close firewall
[root@localhost bin]# setenforce 0 //Turn off enhanced security features
[root@localhost bin]# ./apachectl -t -D DUMP_mooules | grep "expire"
Syntax OK

Use the test machine win10 to test, enter the web address 192.168.131.129 in the browser to access the home page
Finally, open the fiddre software to capture the package

Pictures from shared folders are copied to the web file directory and added to the web page

[root@localhost bin]# cd /abc/
[root@localhost LAMP]# ls
apr-1.6.2.tar.gz                  fangdao.jpg
apr-util-1.6.0.tar.gz             httpd-2.4.29.tar.bz2
awstats-7.6.tar.gz                tupian.png
cronolog-1.6.2-14.el7.x86_64.rpm  mysql-5.6.26.tar.gz
Discuz_X2.5_SC_UTF8.zip           php-5.6.11.tar.bz2
[root@localhost LAMP]# CP lf.jpg/usr/local/httpd/htdocs/ //Copy pictures to web folder
[root@localhost LAMP]# cd /usr/local/httpd/
[root@localhost httpd]# cd htdocs/
[root@localhost htdocs]# ls
index.html  tupian.png
[root@localhost htdocs]# vim index.html 
<html><body><h1>It works!</h1>
<img src="tupian.png"/>
</body></html>

Now we can see the added pictures when we access them again with the test machine. We can see that the capturing information of the pictures includes gzip compression information at the port.

Configure caching time for web pages

_Configure Apache with the mod expire module to enable Web pages on the client side
Browser caches for a period of time to avoid duplicate requests
When the mod expire module is enabled, the page header information is automatically generated
Expires tags and ache-Control tags for reducing customers
End access frequency and times to reduce unnecessary traffic and increase visits
Purpose of asking speed

Modify the httpd.conf configuration file
Enable the mod expires module and set any format of documents under the http protocol to 60
Expires in seconds
<lfModule mod expires.c\>
ExpiresActive On
ExpiresDefault "access plus 60 seconds"
</lfModule\>
Restart httpd service

Apache cache implementation

After configuring Apache Web Page Compression, configure the following

Configure Apache Profile

[root@localhost htdocs]# vim /etc/httpd.conf
 Insert the following at the end of the profile
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 50 seconds"
</IfModule>

Check syntax correctness and restart service

[root@localhost htdocs]# pwd
/usr/local/httpd/htdocs
[root@localhost htdocs]# cd /usr/local/httpd/
[root@localhost httpd]# cd bin/
[root@localhost bin]# . /apachectl-t //Check syntax
Syntax OK
[root@localhost bin]# ./apachectl stop
[root@localhost bin]# ./apachectl start

We're doing a crawl test with the browser in the test machine win10, and we can see that the time we set for caching the web page appears in the header information

Topics: Linux Apache vim xml Javascript