I Apache Web page optimization
In enterprises, only using the default configuration parameters after deploying Apache will cause many problems in the website. In other words, the default configuration is for the previous lower server configuration, which is no longer applicable to today's Internet era
In order to meet the needs of enterprises, we need to consider how to improve the performance and stability of Apache, which is the content of Apache optimization
- Web page compression
- Web cache
1.1 web page compression
① Check if mod is installed_ Deflate module
apachectl -t -D DUMP_MODULES | grep "deflate"
② If mod is not installed_ Deflate module, recompile and install Apache, add mod_deflate module
systemctl stop httpd.service cd /usr/local/httpd/conf mv httpd.conf httpd.conf.bak yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel cd /opt/httpd-2.4.29/ ./configure \ --prefix=/usr/local/httpd \ --enable-so \ --enable-rewrite \ --enable-charset-lite \ --enable-cgi \ --enable-deflate #Add mod_deflate module make && make install
③ Configure mod_deflate module enabled
vim /usr/local/httpd/conf/httpd.conf --52 that 's ok--modify Listen 192.168.80.10:80 --105 that 's ok--note off LoadModule deflate_module modules/mod_deflate.so #Enable mod_deflate module --197 that 's ok--note off,modify ServerName www.kgc.com:80 --Last line add-- <IfModule mod_deflate.c> AddoutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png #Represents what kind of content to enable gzip compression DeflateCompressionLevel 9 #Represents the compression level, ranging from 1 to 9 SetOutputFilter DEFLATE #On behalf of enabling deflate module to gzip compress the output of this site </IfModule>
④ Check the installation and start the service apachectl -t
#Verify that the configuration file is configured correctly apachectl -t -D DUMP_MODULES | grep "deflate" #Check Mod_ Is deflate module installed deflate_module (shared) #Correct results installed systemctl start httpd.service
⑤ Test mod_deflate compression works cd /usr/local/httpd/ htdocs
First game .jpg File transfer/usr/local/httpd/htdocs Under the directory vim index.html <html><body><h1>It works!It works!It works!It works!It works!It works!Itworks!It works!It works!It works!It works!It works!It works!It works!Itworks!It works!It works!It works!It works!It works!It works!It works!Itworks!It works!It works!It works!It works!It works!It works!It works!Itworks!it works!It works!It works!It works!It works!It works!It works!Itworks!</h1> <img src="game.jpg" /></body></html>
1.2 Web Caching
(1) configure the web page compression function of Apache, which uses gzip compression algorithm to compress the web page content and then transmit it to the client browser
(2) function
- It reduces the number of bytes transmitted on the network and speeds up the loading speed of web pages
- Save traffic and improve users' browsing experience
- gzip has a better relationship with the crawling tools of search engines
II Web page compression
1. Check whether mod is installed_ Deflate module
apachectl -t -D DUMP_MODULRS | grep "deflate"
2. If mod is not installed_ Deflate module, recompile and install Apache, add mod_deflate module
systemctl stop httpd.service cd /usr/ local/httpd/ conf mv httpd.conf httpd.conf. bak
yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel cd /opt/httpd-2.4.29/ ./configure \ --prefix=/usr/local/httpd \ --enable-so \ --enable-rewrite \ --enable-charset-lite \ --enable-cgi \ --enable-deflate #Add mod_deflate module make && make install
- Switch to cd /opt/httpd-2.4.29/ add mod_deflate module, and then make & & make install to compile
3. Configure mod_deflate module enabled
vim /usr/local/httpd/conf/httpd.conf --52 that 's ok--modify Listen 192.168.80.10:80 --105 that 's ok--note off LoadModule deflate_module modules/mod_deflate.so #Enable mod_deflate module --197 that 's ok--Uncomment, modify ServerName www. kgc.com:80 --Last line add-- <IfModule mod deflate.c> AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/pag #Represents what kind of content to enable gzip compression DeflateCompressionLevel 9 #Represents the compression level, ranging from 1 to 9 setoutputFilter DEFLATE #On behalf of enabling deflate module to gzip compress the output of this site </IfModule>
4. Check the installation and start the service
apachectl -t #Verify that the configuration file is configured correctly apachectl -t -D DUMP_MODULES | grep "deflate" #Check Mod_ Is deflate module installed deflate_module (shared) #Installed correct results systemctl start httpd.service
5. Test whether mod deflate compression is effective
cd /usr/local/httpd/htdocs First game.jpg File transfer/usr/local/httpd/htdocs Under the directory vim index.html <html><body><h1>It works! It works! It works! It works ! It works!It works! It works! It works! It works! It works! It works! It works ! It works ! It 1 works!</h1> <img src="game.jpg"/> </body></html> Method 1: stay Linux In the system, open Firefox browser and right-click to view elements Select network--->choice HTML,WS,other visit http://192.168.80.10, double-click the 200 response message to see that the response header contains content encoding: gzip Method 2: stay Windows Installed in sequence in the system Microsoft.NET4 and fiddler Software, open fiddler Software choice inspectors ---> choice Headers Browser access http://192.168.80.10, double-click the 200 response message to view the content encoding: gzip
III Web cache
3.1 check whether mod is installed_ Expires module
apachectl -t -D DUMP_MODULES | grep "expires"
3.2 if mod is not installed_ Expires module, recompile and install Apache, add mod_expires module
systemctl stop httpd.service cd /usr/local/httpd/conf mv httpd.conf httpd.conf.bak1 yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel cd /opt/httpd-2.4.29/ ./configure \ --prefix=/usr/local/httpd \ --enable-so \ --enable-rewrite \ --enable-charset-lite \ --enable-cgi \ --enable-deflate \ --enable-expires #Add mod_ expires module make && make install
3.3 configuring mod_expires module enabled
vim /usr/local/httpd/conf/httpd.conf --52 that 's ok--modify Listen 192. 198.80.10:80 --111 that 's ok--note off LoadModule expires_ module modules/mod_expires.so #Enable mod_expires module --199 that 's ok--Uncomment, modify ServerName www.kgc.com:80 --Last line add-- <IfModule mod_expires.c> ExpiresActive On #T urn on the web page caching function ExpiresDefault "access plus 60 seconds" #Set cache for 60 seconds </ IfModule>
3.4 check the installation and start the service
apachectl -t #Verify that the configuration file is configured correctly apachectl -t -D DUMP_MODULES | grep "expires" #Check Mod_ Is deflate module installed deflate_module (shared) #Installed correct results systemctl start httpd.service
3.5 test whether the cache is effective
cat /usr/local/httpd/htdocs/index.html Method 1-: stay Linux In the system, open Firefox browser and right-click to view elements Select network--->choice HTML,WS,other visit http://192.168.80.10, double-click the 200 message to see that the response header contains the Expires item Method 2: stay Windows Installed in sequence in the system Microsoft. NET4 and fiddler Software, open fiddler Software choice inspectors --->choice Headers Browser access http://192.168.80.10, double-click the 200 message to view the Expires item
IV Hide version information
vim /usr/local/httpd/conf/httpd.conf --491 that 's ok--note off Include conf/extra/httpd-default.conf vim /usr/local/httpd/conf/extra/httpd-default.conf --55 that 's ok--modify ServerTokens Prod #Change the original Full to Prod, only display the name, no version #ServerTokens indicates whether the response header field returned by the Server to the client contains information about the Server OS type and the compiled module description. systemctl restart httpd.service Browser access http://192.168.80.10, double-click the 200 message to view the Server item
V Apache anti-theft chain
5.1 check whether mod is installed_ Rewrite Module
apachectl -t -D DUMP_MODULES | grep " rewrite"
5.2 if mod is not installed_ Rewrite module, recompile and install Apache, add mod_rewrite Module
systemctl stop httpd.service cd /usr/local/httpd/conf mv httpd.conf httpd.conf.bak2 yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel cd /opt/httpd-2.4.29/ ./configure \ --prefix=/usr/local/httpd \ --enable-so \ --enable-rewrite \ #Add mod rewrite Module --enable-charset-lite \ --enable-cgi \ --enable-deflate \ --enable-expires make && make install
5.3 configuring mod_rewrite Module enabled
vim /usr/local/httpd/conf/httpd.conf --157 that 's ok--note off LoadModule rewrite_module modules/mod_rewrite.so --224 that 's ok-- <Directory "/usr/local/httpd/htdocs"> Options Indexes FollowSymLinks AllowOverride None Require all granted RewriteEngine On #Open the rewrite function and add mode_ rewrite module content RewriteCond %{HTTP_REFERER} !^http://kgc.com/. *$ [NC] #Set matching rules RewriteCond %{HTTP_REFERER} !^http://kgc. com$ [NC] RewriteCond %{HTTP_REFERER} !^http://www.kgc.com/.*$ [NC] RewriteCond %{HTTP_REFERER} ! ^http://www.kgc.com/$ [NC] RewriteRule .*\.(gif | jpg | swf)$ http://www.kgc.com/error.png #Set Jump Action </Directory>
5.4 web page preparation
web Source host configuration: cd /usr/local/httpd/htdocs take game.jpg,error.png File transfer/usr/local/httpd/htdocs Under the directory vim index.html <html><body> <h1>this is kgc.com!</h1> <img src="game.jpg"/> </body></html> echo "192.168.253.11 www.kgc.com" >> /etc/hosts echo "192.168.253.22 www.benet.com" >> /etc/hosts Chain stealing website host: cd /usr/local/httpd/htdocs #The default path of httpd service installed by yum is / var/www/html/ vim index.html <html><body> <h1>this is benet.com!</h1> <img src="http://www.kgc.com/game.jpg"/> </body></html> echo "192.168.253.11 www.kgc.com" >> /etc/hosts echo "192.168.253.22 www.benet.com" >> /etc/hosts