Linux云计算架构-生产中常用的Apache调优策略

Linux云计算架构-Apache调优

1. 编译安装前修改Apache版本信息

每个apache版本都会存在一些漏洞,而这些漏洞往往会被黑客所利用。所以要学会隐藏apache的版本信息。

# 查看apache的版本信息
# 这个是之前的apache版本信息
[root@master ~]# curl -I 192.168.8.145
HTTP/1.1 200 OK
Date: Sat, 11 Jul 2020 04:55:50 GMT
Server: Apache/2.4.37 (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html

# 百度的
[root@master ~]# curl -I www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Fri, 10 Jul 2020 14:21:17 GMT
Etag: "575e1f60-115"
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
# 淘宝的
[root@master ~]# curl -I www.taobao.com
HTTP/1.1 301 Moved Permanently
Server: Tengine
Date: Fri, 10 Jul 2020 14:21:41 GMT
Content-Type: text/html
Content-Length: 278
Connection: keep-alive
Location: https://www.taobao.com/
Via: cache4.cn1480[,0]
Timing-Allow-Origin: *
EagleId: 6f3f389815943909010925552e

# Server: Apache/2.4.37 (Unix)
# 上传apache源码包
[root@master ~]# cd /usr/local/src/
[root@master src]# ll
-rw-r--r--. 1 root root  9177278 7月  11 10:42 httpd-2.4.37.tar.gz
# 解压apache源码包
[root@master src]# tar xzvf httpd-2.4.37.tar.gz
# 查看版本信息配置文件
[root@master src]# cd /usr/local/src/httpd-2.4.37/
[root@master httpd-2.4.37]# ll ./include/ap_release.h 
-rw-r--r--. 1 root dip 3144 10月 18 2018 ./include/ap_release.h
# 修改版本信息文件内容
# Server: Apache/2.4.37 (Unix) PHP/7.1.24   这个是安装完的版本信息
# 40、41、42主要介绍用了什么web服务器    44、45、46、47主要介绍对应web服务器的版本号
# 不要去掉#
40 #define AP_SERVER_BASEVENDOR "Apache Software Foundation"   # 服务的供应商名称
41 #define AP_SERVER_BASEPROJECT "Apache HTTP Server"          # 服务的项目名称
42 #define AP_SERVER_BASEPRODUCT "Apache" 43                   # 服务的产品号

44 #define AP_SERVER_MAJORVERSION_NUMBER 2                     # 主要版本号
45 #define AP_SERVER_MINORVERSION_NUMBER 4                     # 小版本号
46 #define AP_SERVER_PATCHLEVEL_NUMBER   37                    # 补丁级别
47 #define AP_SERVER_DEVBUILD_BOOLEAN    0                     # 

在这里插入图片描述

# yum解决包依赖问题
[root@master ~]# yum install apr-util apr-util-devel apr apr-devel pcre pcre-devel zlib zlib-devel openssl openssl-devel gcc -y
# 编译前准备,添加一些模块
[root@master ~]# cd /usr/local/src/httpd-2.4.37/
[root@master httpd-2.4.37]# ./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --enable-ssl --enable-deflate --enable-expires --enable-mpms-shared=all
# --prefix 指定安装目录
# --enable-so  支持动态加载模块,除非所有的模块都静态加载了。该模块被静态加载进去。
# --enable-rewrite   支持网站地址重写,动态加载
# --enable-ssl  支持ssl加密,动态加载
# --enable-deflate  支持页面传输前进行压缩,动态加载
# --enable-expires  支持设置页面缓存时间,动态加载
# --enable-mpms-shared=all    动态编译三种多进程处理模块(prefork、worker、event)
# 编译及编译安装
[root@master httpd-2.4.37]# make -j 4 && make install

# 当有/usr/local/apache/这个目录则说明已经安装成功了。
[root@master ~]# ll -d /usr/local/apache/
drwxr-xr-x. 14 root root 164 7月  11 11:48 /usr/local/apache/
# 启动apache
[root@master ~]# cp /usr/local/apache/bin/apachectl /etc/init.d/apache
[root@master ~]# /etc/init.d/apache start     
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::a0e4:d97e:42bb:abc. Set the 'ServerName' directive globally to suppress this message
# 报错,说是ServerName有问题,查看下apache的配置文件,修改ServerName如下:
[root@master ~]# vim /usr/local/apache/
bin/     cgi-bin/ error/   icons/   logs/    manual/  
build/   conf/    htdocs/  include/ man/     modules/ 
[root@master ~]# vim /usr/local/apache/conf/httpd.conf 
199 ServerName localhost:80
[root@master ~]# /etc/init.d/apache start
httpd (pid 120308) already running
[root@master ~]# netstat -antup | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      120308/httpd 
# 防火墙开放80端口
[root@master ~]# firewall-cmd --permanent --zone=public --add-port=80/tcp
success
[root@master ~]# firewall-cmd --reload 
success
# 输入IP,可以看到apache的默认首页已经可以正常打开了。

在这里插入图片描述

# 查看apache版本信息
[root@master ~]# curl -I 192.168.8.145
HTTP/1.1 200 OK
Date: Sat, 11 Jul 2020 04:44:35 GMT
Server: abong/1.0.0 (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html
# Server: Apache/2.4.37 (Unix)
# 可以看到,web服务器名和版本号都已经修改了。
# 现在存在一个问题,如果是已经存在apache了,能修改apache服务器的名字和版本号嘛?答案是:可以的。
# 删除apache的安装包,重新解压编译
[root@master ~]# cd /usr/local/src/
[root@master src]# rm -rf httpd-2.4.37
[root@master src]# tar xzvf httpd-2.4.37.tar.gz 
# 修改版本信息,但是不能为空,为空会报错
[root@master src]# vim httpd-2.4.37/include/ap_release.h 
[root@master src]# cd httpd-2.4.37/
[root@master httpd-2.4.37]# ./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --enable-ssl --enable-deflate --enable-expires --enable-mpms-shared=all
[root@master httpd-2.4.37]# make -j 4 && make install
# 重新启动下apache,再次获取apache版本信息
# 可以看到apache版本信息已经被修改了
[root@master ~]# curl -I 192.168.8.145
HTTP/1.1 200 OK
Date: Sat, 11 Jul 2020 05:21:27 GMT
Server: haha/3.2.1-dev (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html

2. 安装完apache后隐藏操作系统信息和apache的版本号信息

# Server: haha/3.2.1-dev (Unix)
# 还是有版本信息和操作系统信息,这个可以隐藏吗?答案是:可以的
# 开启版本默认信息文件
[root@master ~]# vim /usr/local/apache/conf/httpd.conf 
491 Include conf/extra/httpd-default.conf
# 修改版本默认信息文件
[root@master ~]# vin /usr/local/apache/conf/extra/httpd-default.conf 
55 ServerTokens Prod   #不显示服务器操作系统类型
65 ServerSignature off  #不显示web服务器版本号和模块版本。
# 重新加载配置文件,类似restart
[root@master ~]# /etc/init.d/apache graceful
[root@master ~]# curl -I 192.168.8.145
HTTP/1.1 200 OK
Date: Sat, 11 Jul 2020 05:29:31 GMT
Server: haha
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html
# 可以看到,apache的版本信息和操作系统信息都已经隐藏了。

3. 设置apache自启动

# 编辑apache的启动文件
[root@master ~]# vim /etc/init.d/apache 
#!/bin/sh
# chkconfig:2345 64 36
# description:apache_abong
[root@master ~]# chkconfig --add apache
[root@master ~]# chkconfig --list apache
apache         	0:关	1:关	2:开	3:开	4:开	5:开	6:关
[root@master ~]# reboot
# 可以看到,apache依旧开启。

在这里插入图片描述

4. 修改运行apache的默认用户

# 查看当前运行apache的是哪个用户,可以看到是daemon,也是apache的默认运行用户
[root@master ~]# ps aux | grep httpd
root       6872  0.0  0.1  76992  2300 ?        Ss   13:46   0:00 /usr/local/apache/bin/httpd -k start
daemon     6875  0.0  0.2 433576  4896 ?        Sl   13:46   0:00 /usr/local/apache/bin/httpd -k start
daemon     6878  0.0  0.2 433576  4876 ?        Sl   13:46   0:00 /usr/local/apache/bin/httpd -k start
daemon     6880  0.0  0.2 433576  4252 ?        Sl   13:46   0:00 /usr/local/apache/bin/httpd -k start
daemon     8933  0.0  0.2 433576  4256 ?        Sl   13:46   0:00 /usr/local/apache/bin/httpd -k start
root       9037  0.0  0.0 112728   988 pts/0    S+   13:49   0:00 grep --color=auto httpd

# 创建一个用户,名为apache
[root@master ~]# useradd -M -s /sbin/nologin apache
[root@master ~]# id apache
uid=1001(apache) gid=1001(apache)=1001(apache)

# 修改apache的配置文件
[root@master ~]# vim /usr/local/apache/conf/httpd.conf 
168 User apache
169 Group apache

# 重新加载配置文件,可以看到运行用户已经变为apache
[root@master ~]# /etc/init.d/apache graceful
[root@master ~]# ps aux | grep httpd
root       6872  0.0  0.1  76992  2928 ?        Ss   13:46   0:00 /usr/local/apache/bin/httpd -k start
apache     9169  0.0  0.2 433576  4220 ?        Sl   13:53   0:00 /usr/local/apache/bin/httpd -k start
apache     9170  0.0  0.2 433576  4204 ?        Sl   13:53   0:00 /usr/local/apache/bin/httpd -k start
apache     9171  0.0  0.2 433576  4236 ?        Sl   13:53   0:00 /usr/local/apache/bin/httpd -k start
root       9260  0.0  0.0 112728   988 pts/0    S+   13:53   0:00 grep --color=auto httpd

# 在这里默认使用daemon用户也是安全的。

5. apache网站数据目录及文件权限调整

# 网站数据目录
# 源码包安装:/usr/local/apache/htdocs/
# rpm包安装:/var/www/html/
# 修改所有者和所属组为apache
# 修改文件权限为755
[root@master ~]# chown -R apache:apache /usr/local/apache/htdocs/
[root@master ~]# chmod -R 755 /usr/local/apache/htdocs/

6. apache日志权限调整

# apache的主进程操作记录apache日志,而apache的主进程是由root用户启动的。
# 设置apache的日志的所有者和所属组为root
[root@master ~]# chown -R root:root /usr/local/apache/logs/
[root@master ~]# ll /usr/local/apache/logs/
总用量 12
-rw-r--r--. 1 root root  720 7月  11 13:46 access_log
-rw-r--r--. 1 root root 2815 7月  11 13:53 error_log
-rw-r--r--. 1 root root    5 7月  11 13:53 httpd.pid

7. apache日志分割

apache服务器的日志默认是不分割的,就只有一整个文件。cronolog是一款web服务器日志切割工具,该工具的作用就是按照当前的日期和时间切割日志文件,将对应时间的日志写入对应文件名的文件中。如每月一个文件、每日一个文件、每小时一个文件。

# 下载并安装cronolog
yum install epel-release -y
yum install cronolog -y
# 修改apache配置文件
# 用cronolog切割日志
# 格式:%Y%m%d   %Y%m%d%H
[root@master ~]# vim /usr/local/apache/conf/httpd.conf
275 ErrorLog "|/usr/sbin/cronolog /usr/local/apache/logs/access_%Y%m%d.log"
304 CustomLog "|/usr/sbin/cronolog /usr/local/apache/logs/error_%Y%m%d.log" combined
[root@master ~]# /etc/init.d/apache graceful
[root@master ~]# cd /usr/local/apache/
[root@master apache]# cd logs/
[root@master logs]# ll
总用量 16
-rw-r--r--. 1 root root  286 7月  11 15:55 access_20200711.log
-rw-r--r--. 1 root root  720 7月  11 13:46 access_log
-rw-r--r--. 1 root root 2951 7月  11 15:55 error_log
-rw-r--r--. 1 root root    5 7月  11 15:55 httpd.pid

8. apache错误页面设置

输入一个不存在的html,会报404错误。如果直接跳转到正常页面,可以查看下IE浏览器设置。

在这里插入图片描述
在这里插入图片描述

# 查看下错误页面
[root@master ~]# vim /usr/local/apache/conf/httpd.conf
/404
428 # Some examples:
429 #ErrorDocument 500 "The server made a boo boo."
430 #ErrorDocument 404 /missing.html
431 #ErrorDocument 404 "/cgi-bin/missing_handler.pl"
432 #ErrorDocument 402 http://www.example.com/subscription_info.html

# 在网站数据目录下添加错误页面设置
DocumentRoot "/usr/local/apache/htdocs"
224 <Directory "/usr/local/apache/htdocs">
225 ErrorDocument 404 /404.html

# 编辑错误页面404.html
[root@master ~]# cd /usr/local/apache/htdocs/
[root@master htdocs]# vim 404.html
[root@master htdocs]# cat 404.html 
this is 404 false.

# 重启apache
[root@master ~]# /etc/init.d/apache restart

再次查看不存在的页面,显示如下:

在这里插入图片描述

9. 启动apache压缩模块,对大文件进行压缩再发送,解压缩再查看

网站随着用户访问量的增加和内容量的增加,网站的带宽会不断的增加,随之就是网站成本的增加。并且当内容量增大的时候,客户端如果带宽小,就会影响用户的体验。因此从这两方面考虑,网站的某些内容必须经过压缩之后再传给用户,然后在用户客户端进行解压,来实现双方共赢的效果。(压缩再发,解压再看)

Apache 的压缩要用到mod_deflate模块,该模块提供了 DEFLATE输出过滤器,允许服务器在将输出内容发送到客户端以前进行压缩,以节约带宽。它的核心思想就是把文件先在服务器进行压缩,然后再进行传输,这样可以显著减少文件传输的大小。当传输完毕后,客户端浏览器会重新对压缩过的内容进行解压缩。如果没特殊情况的话,所有的文本内容都应该能被 gzip 压缩,包括html(php),js,css,xml,txt 等。【如果需要每次都加载的文本就不会经过gzip压缩】

# 检查是否安装mod_deflate
# 在编译前准备的时候,加上--enable-deflate,即安装mod_deflate模块。【博主亲测:好像不会生成静态模块。。。最后还是动态共享模块】
# 查看mod_deflate.so文件是否存在
[root@master ~]# ll /usr/local/apache/modules/mod_deflate.so 
-rwxr-xr-x. 1 root root 53168 7月  11 20:07 /usr/local/apache/modules/mod_deflate.so
# 查看下deflate模块是否加载到apache,如果是注释需取消注释。
[root@master ~]# cat -n /usr/local/apache/conf/httpd.conf | grep deflate
   108	LoadModule deflate_module     modules/mod_deflate.so
# 重新加载apache配置文件
[root@master ~]# /etc/init.d/apache graceful
# 可以看到该模块已经安装成功
[root@master ~]# /usr/local/apache/bin/apachectl -M | grep deflate
 deflate_module (shared)
# 如果不存在/usr/local/apache/modules/mod_deflate.so模块文件,可以用以下方式动态编译进去
# 以DSO方式安装-shared
# DSO:Dynamic shared object 动态共享对象 。DSO模块可以在编译服务器之后编译,也可以用Apache扩展工具(apxs)编译并增加
# /usr/local/apache/bin/apxs -c -i -a
# -c:需要执行编译操作。
# -i:需要执行安装操作,以安装一个或多个动态共享对象到服务器的 modules 目录。
# -a:自动增加一个 LoadModule行到httpd.conf文件中,以激活此模块,如果此行已经存在,启用。
# 模块所在目录:/usr/local/src/httpd-2.4.37/modules/filters/
[root@master ~]# /usr/local/apache/bin/apxs -c -i -a /usr/local/src/httpd-2.4.37/modules/filters/mod_deflate.c
[root@master ~]# ll /usr/local/apache/modules/mod_deflate.so
-rwxr-xr-x. 1 root root 53168 7月  11 18:32 /usr/local/apache/modules/mod_deflate.so

# 有一个报错
[root@master ~]# /usr/local/apache/bin/apachectl -M | grep deflate
httpd: Syntax error on line 107 of /usr/local/apache/conf/httpd.conf: Cannot load modules/mod_deflate.so into server: /usr/local/apache/modules/mod_deflate.so: undefined symbol: inflate
# 修改apache配置文件的关于模块的内容
[root@master ~]# vim /usr/local/apache/conf/httpd.conf
107 LoadFile /usr/lib64/libz.so   # 加载库文件
108 LoadModule deflate_module     modules/mod_deflate.so
[root@master ~]# /usr/local/apache/bin/apachectl -M | grep deflate
 deflate_module (shared)
# 可以看到,已经将deflate_module模块添加进apache了。
# 配置deflate压缩参数
# 编辑apache配置文件,添加压缩模块参数
# DeflateCompressionLevel 9   压缩等级1-9,等级越高效率越高,消耗cpu也越高
# SetOutputFilter DEFLATE 启用压缩
# DeflateFilterNote Input instream 声明输入流的byte量
# DeflateFilterNote Output outstream 声明输出流的byte量
# DeflateFilterNote Ratio ratio   压缩百分比
# AddOutputFilterByType   压缩文件类型
[root@master ~]# vim /usr/local/apache/conf/httpd.conf
173 <Ifmodule mod_deflate.c>
174  DeflateCompressionLevel 9
175  SetOutputFilter DEFLATE
176  DeflateFilterNote Input instream
177  DeflateFilterNote Output outstream
178  DeflateFilterNote Ratio ratio
179  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript  # 在这里设置要压缩的文件类型
180 </Ifmodule>

# 重新加载配置文件
# graceful重新启动Apache服务器,但不会中断原有的连接。用于修改了配置文件后进行重新读取配置文件。【平滑重启】
# 可通过-h查看apachectl帮助
[root@master ~]# /usr/local/apache/bin/apachectl -h
[root@master ~]# /etc/init.d/apache graceful

# text/plain text/html text/php text/xml text/css text/javascript
# application/xhtml+xml application/xml application/rss+xml application/atom_xml 
# application/x-javascript application/x-httpd-php 
# image/svg+xml image/gif image/png image/jpe image/swf image/jpeg image/bmp

验证效果:

①下载360安全浏览器,添加yslow扩展。https://ext.se.360.cn/webstore/search/yslow

在这里插入图片描述

网上说谷歌和火狐浏览器都有这个插件,但是安装比较麻烦。

②搞一个大点的网页,然后打开这个网页。

[root@master ~]#  cp /etc/passwd /usr/local/apache/htdocs/a.html

在这里插入图片描述

可以看到,大小2.3k的html文件,被压缩成了0.9k。

10. apache设置缓存时间

当客户访问网站时,会产生多次同样的html请求,可以通过mod_expires缓存模块来设置ExpiresHeader来缓存这些文件。

Expires 通过 header 报文来指定特定类型的文件在浏览器中的缓存时间。对一些不经常修改的文件,可以做缓存,当网站第一次从服务器中下载这些文件后,就不需要再从服务器中下载,而是直接从浏览器中读取。可以加快客户访问页面的速度,也可以减轻企业的带宽压力。【一次下载,终生受用】

# 有mod_expires.so这个模块
[root@master ~]# ll /usr/local/apache/modules/mod_expires*
-rwxr-xr-x. 1 root root 18360 7月  11 12:41 /usr/local/apache/modules/mod_expires.so

# 启用该模块
[root@master ~]# vim /usr/local/apache/conf/httpd.conf
115 LoadModule expires_module modules/mod_expires.so

# 可以看到,该模块已经被动态加载进去了
[root@master ~]# /usr/local/apache/bin/apachectl -M | grep expires
 expires_module (shared)
# 设置缓存【全局、目录、虚拟主机】
# 全局设置缓存
[root@master ~]# vim /usr/local/apache/conf/httpd.conf
<IfModule mod_expires.c> 
 ExpiresActive on   # 开启缓存功能
 ExpiresDefault "access plus 12 month"    # 文档默认有效期12个月
 ExpiresByType text/html "access plus 12 months"  # html类型
 ExpiresByType text/css "access plus 12 months"
 ExpiresByType text/pdf "access plus 12 months"
 ExpiresByType image/gif "access plus 12 months"
 ExpiresByType image/jpeg "access plus 12 months"
 ExpiresByType image/jpg "access plus 12 months"
 ExpiresByType image/png "access plus 12 months"
 EXpiresByType application/x-shockwave-flash "access plus 12 months"
 EXpiresByType application/x-javascript "access plus 12 months"
 ExpiresByType video/x-flv "access plus 12 months"
</IfModule>


# 对目录设置缓存
<Directory "/usr/local/apache/htdocs"> 
<IfModule mod_expires.c> 
 ExpiresActive on
 ExpiresDefault "access plus 12 month"
 ExpiresByType text/html "access plus 12 months"
 ExpiresByType text/css "access plus 12 months"
 ExpiresByType text/pdf "access plus 12 months"
 ExpiresByType image/gif "access plus 12 months"
 ExpiresByType image/jpeg "access plus 12 months"
 ExpiresByType image/jpg "access plus 12 months"
 ExpiresByType image/png "access plus 12 months"
 EXpiresByType application/x-shockwave-flash "access plus 12 months"
 EXpiresByType application/x-javascript "access plus 12 months"
 ExpiresByType video/x-flv "access plus 12 months"
</IfModule>
</Directory>


# 对虚拟主机设置缓存
<VirtualHost *:80>
<IfModule mod_expires.c> 
 ExpiresActive on
 ExpiresDefault "access plus 12 month"
 ExpiresByType text/html "access plus 12 months"
 ExpiresByType text/css "access plus 12 months"
 ExpiresByType text/pdf "access plus 12 months"
 ExpiresByType image/gif "access plus 12 months"
 ExpiresByType image/jpeg "access plus 12 months"
 ExpiresByType image/jpg "access plus 12 months"
 ExpiresByType image/png "access plus 12 months"
 EXpiresByType application/x-shockwave-flash "access plus 12 months"
 EXpiresByType application/x-javascript "access plus 12 months"
 ExpiresByType video/x-flv "access plus 12 months"
</IfModule>
</VirtualHost>

# 配置完缓存要重启apache
[root@master ~]# /etc/init.d/apache graceful
# 使用yslow测试下缓存结果

在这里插入图片描述

# 设置文档有效期
# ExpiresDefault "<base> [plus] <num> <type>"
# 设置缓存文件的类型和缓存时间
# EXpiresByType type/encoding "<base> [plus] <num><type>}"

# 各参数介绍:
<base>  【access、now、modification】
# access:客户端第一次访问开始算起,对几乎不更新的图片较好。
# modification:最后修改时间开始算起,对定期更新的URL较好。
[plus]
<num>   【整数】
<type>  【years、months、weeks、days、hours、minutes、seconds】【注意单双数】

缓存的优缺点:

优点:缩短服务的响应时间、减轻服务器负担、减少网络带宽使用量,降低企业成本。

缺点:若被缓存的内容更新了,客户端无法及时看到最新的内容。

解决方法:缩短缓存时间、更改缓存对象的名字、客户端可通过清理缓存看到最新的内容。

11. 开启长连接功能

长连接功能: 用户的每一次请求,都会通过三次握手建立连接,四次挥手断开连接,对于同一个客户端频繁的请求,会不断的建立和断开连接,这样大量占用cpu资源。而keepalive,即长连接,可以保持连接,减少建立请求和断开请求的次数,减少占用cpu的时间,但这会占用较大的内存资源。可以分析网站的访问量和请求次数是否频繁发生以决定是否开启长连接功能。

# 加载httpd-default.conf文件进去
[root@master ~]# vim /usr/local/apache/conf/httpd.conf
521 Include conf/extra/httpd-default.conf

# 修改httpd-default.conf默认配置文件
[root@master ~]# vim /usr/local/apache/conf/extra/httpd-default.conf
 10 Timeout 60  # 接收一个GET请求耗费的总时间、POST或PUT请求时,接收两个TCP包之间的时间、应答时TCP包传输中两个ACK包之间的时间。
 16 KeepAlive On # 开启长连接功能
 23 MaxKeepAliveRequests 100  # 长连接期间最大请求数,即5s内最多100个请求,超过5s或者超过100个都会断开连接,需重新建立连接才可继续访问。
 29 KeepAliveTimeout 5 # 长连接持续时间为5s,即5s后断开。
# 案例分析 
假设KeepAlive的超时时间为10秒钟,服务器每秒处理50个独立用户访问,那么系统中Apache的总进程数就是10*50=500个,如果一个进程占用4M内存,那么总共会消耗 2G 内存,所以可以看出,在这种配置中,相当消耗内存,但好处是系统只处理了 50 次 TCP 的握手和关闭操作。
如果关闭 KeepAlive,如果还是每秒50个用户访问,如果用户每秒的并发请求数为3个,那么Apache的总进程数就是 50*3=150个,如果还是每个进程占用4M内存,那么总的内存消耗为600M,这种配置能节省大量内存,但是,系统处理了 150 次 TCP 的握手和关闭的操作,因此又会多消耗一些 CPU 资源。

# 两点建议:
①内存和cpu足够,开不开对性能影响不大。
②若同一个客户端对服务器经常访问,建议开启。

12. MYSQL数据库经常使用cpu达到200%以上,怀疑不正常,如何排查?

①登录IDC机房查看流量情况
②重启mysql数据库
③登录mysql数据库,执行show processlist;查看正在执行的sql语句,看有没有处于“sending data”状态的sql语句,即执行时间过长。
④询问开发人员相关数据表有没有建立索引,让开发看下
⑤修改my.cnf文件,添加tmp_table_size = 300M和query_cache_size = 1024M,看看增加临时表和查询缓存大小能否解决问题。
⑥登录mysql,执行show index from tablename; 查询处于“sending data”状态的表是否有索引,一般都是没有的了
⑦添加索引
ALTER TABLE table_name ADD INDEX index_name (column_list);   -- 增加普通索引
ALTER TABLE table_name ADD UNIQUE (column_list);             -- 增加唯一性索引
ALTER TABLE table_name ADD PRIMARY KEY (column_list);        -- 增加主键索引
-- 主键索引和唯一索引的区别
-- 主键索引所在列不能为空,且唯一,只能有一个
-- 唯一索引所在列可以为空,也唯一,可以有多个
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值