Apache acceleration module mod_pagespeed installation use detailed introduction

  • 2020-05-09 19:44:45
  • OfStack

1. mod_pagespeed is introduced
mod_pagespeed is a module suitable for apache httpd. It is an open source code released by google, which can automatically complete optimization, including optimizing the cache, reducing communication between client servers, reducing load and so on. Deploy mod_pagespeed without modifying any program, restart apache after installation, and the 1-cut front-end problem is automatically optimized. CDN has deployed mod_pagespeed on its CDN servers to speed up access to client websites, reducing image file sizes by 20-30% with automatic compression, and shortening page load times by up to 50%. GoDaddy has also announced the widespread deployment of mod_pagespeed on its client web servers.
mod_pagespeed project homepage: https: / / developers google. com/speed pagespeed /
2. Function of mod_pagespeed
1.Optimize Caching optimizes caching
Extend Cache extends the cache
Outline CSS
Outline JavaScript
2.Minimize Round Trip Times minimizes round-trip time
Combine CSS merged with CSS
Inline CSS with CSS embedded
Inline JavaScript with JavaScript embedded
3.Minimize Payload Size minimizes the payload size
Collapse Whitespace compress white space
Combine Heads merges header information
Elide Attributes omits properties
Minify Javascript shrinks Javascript
Optimize Images optimized images
Remove Comments delete comments
Remove Quotes removes references
Rewrite CSS rewrite CSS
Move CSS to HEAD loads CSS to head
Add Head
Add Instrumentation
3. Fundamentals of mod_pagespeed
mod_pagespeed improves web latency and bandwidth efficiency by modifying the resources on the requested web page. When Apache HTTP Server serves web site resources, each optimization in mod_pagespeed module is placed in a customizable filter. Some filters modify the HTML content directly, while others adjust the CSS, JavaScript and images referenced on the page to create a more optimized page.
4. Installation of mod_pagespeed
mod_pagespeed currently supports Apache and Nginx. The simple installation method in Apache is as follows. Once the installation is complete, it creates a new installation source in yum and updates it via yum update.
Installation under Centos/Fedora:

#32 position 
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_i386.rpm
#64 position 
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm
yum install at
rpm -U mod-pagespeed-*.rpm

Debina/Ubuntu


#32 position 
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_i386.deb
#64 position 
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.deb
dpkg -i mod-pagespeed-*.deb

Restart httpd:

services httpd restart

See if pagespeed is loaded:
httpd -M|grep pagespeed

5. Configuration of mod_pagespeed
1 in the successful installation, do not need any configuration can be used normally, of course, if you have higher requirements, you can refer to the following content.

1. Create configuration files

vi /usr/local/apache/conf/pagespeed.conf # Create a new configuration file and modify it according to your requirements 
LoadModule pagespeed_module /usr/local/apache/modules/mod_pagespeed_ap24.so
<IfModule pagespeed_module>
    ModPagespeed on
    ModPagespeedInheritVHostConfig on
    AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html
    ModPagespeedFileCachePath            "/var/cache/mod_pagespeed/"
    ModPagespeedFileCacheInodeLimit        500000
    ModPagespeedAvoidRenamingIntrospectiveJavascript on
    ModPagespeedEnableFilters collapse_whitespace
    <Location /mod_pagespeed_beacon>
          SetHandler mod_pagespeed_beacon
    </Location>
    <Location /mod_pagespeed_statistics>
        Order allow,deny
        Allow from localhost
        Allow from 127.0.0.1
        SetHandler mod_pagespeed_statistics
    </Location>
    ModPagespeedMessageBufferSize 100000

    <Location /mod_pagespeed_message>
        Allow from localhost
        Allow from 127.0.0.1
        SetHandler mod_pagespeed_message
    </Location>
</IfModule>

2. Use configuration files

mkdir -p /var/mod_pagespeed/    # Create the cache directory configured in the configuration file 
chown -R www.www /var/mod_pagespeed/ 
echo 'Include conf/pagespeed.conf' >> /usr/local/apache/conf/httpd.conf
/usr/local/apache/bin/apachectl  -t  # Test profile 
Syntax OK
service httpd restart # restart apache

6. Advanced application of mod_pagespeed
mod_pagespeed has the 1 series of default optimizations (filter), which are automatically turned on when the installation is complete, and you only need to restart Apache once to see the effect.

Basic optimizations have been included, such as compressing CSS and Javascript, extending the expiration time of resources, rewriting images, etc. If you want to further optimize, you can consider the following options:
1.remove_comments: delete the comments in the source file. For the most part, comments are invisible to the end user and can be deleted without affecting browsing. Moreover, mod_pagespeed can recognize the conditional comment of IE without touching it.
2.collapse_whitespace: delete the white space characters in the source file.
3.insert_dns_prefetch: direct the browser to DNS parsing ahead of time. If your blog USES images from other sites, such as Google Cloud Storage as a photo bed, add this option to allow the browser to pre-parse Cloud Storage DNS, which will speed up the image download.
Add options need to be modified/etc/httpd/conf d/pagespeed conf file, and add the following code. Note that you still need to restart Apache after the modification.

ModPagespeedDisableFilters remove_comments,collapse_whitespace

In addition, if your website is HTTPS enabled, you will need to make special Settings for HTTPS, because the communication in HTTPS is encrypted and mod_pagespeed will not know what is in it. Setting is only 1 sentence:
ModPagespeedMapOriginDomain http://ofstack.com https://ofstack.com

Once you're done, refresh the page and see what it looks like.
7. Temporarily disable the PageSpeed component
When debugging a website, sometimes you need to disable the caching component so that you can see the latest changes. At this point the Page Speed component may become an obstacle, and it may not be able to detect changes in the original file in time to re-cache it.
Of course, it's easy to disable it, not even SSH. Just add the parameter ModPagespeed=off after the URL to be debugged, like this:
http://ofstack.com/?ModPagespeed=off

If you look closely at the source files, you can see some differences.


Related articles: