Apache various optimization and security configuration in detail

Summary:

Apache is running on the hardware environment is the most influential factor on the performance, even if the hardware can not be upgraded, it's best to host a separate Apache in order to avoid interference from other applications. Various hardware indicators, the greatest impact on performance is the memory, for static content (images, JavaScript files, css files, etc.). It determines how much content can be cached Apache, the more it cached content, the opportunity to read the contents on the hard disk will be less, large memory can greatly improve the speed of static sites;

High dynamic load sites, saved some more time each request, Apache's mpm module will be derived for each request the corresponding processes or threads are processed, while the number of memory-consuming processes or threads is approximately proportional to , thus increasing the memory is also extremely beneficial to increase the load and speed dynamic site.

Followed by the hard drive speed, especially static site, Apache constantly read the file and sent to the appropriate request, the hard disk read and write is extremely frequent; dynamic loading web site needs to continue the program (php, etc.), a request to read even more than a dozen files to process, so as to improve the quality and speed of the hard disk to improve the performance of Apache is of positive significance.

And finally, the impact CPU CPU Network is a web program execution speed, size of network traffic impact.

 

A, Apache several operating modes, and to tune

Apache installation process in detail, please click on this link to view: http://blog.csdn.net/kangshuo2471781030/article/details/79171344

Apache HTTP Server is designed to be a powerful and flexible server can work in a variety of different platforms and environments. This modular design is called "multiprocessing module" (Multi-Processing Module, MPM), also known as the operating mode.

1.Prefork (a non-threaded type):

Its main work is: When the Apache server starts, mpm_prefork module pre-created multiple sub-processes (the default is 5), each child process only one thread, after receiving the client's request, mpm_prefork module and then forwarded to the sub-request treatment process, and each sub-process can only be used while processing a single request. If the current number of requests exceeds the number of child processes created in advance, mpm_prefork module creates a new child process to handle additional requests. Request so that the client does not need to wait for the process to produce the child after receiving.

Since mpm_prefork module, each request corresponding to a child process, so the more system resources relative to the other two modules concerned. But the advantage is that it mpm_prefork modules each corresponding to child processes independently process a single request, so that, if the request is a problem which will not affect the other requests. Prefork work is higher than on efficiency, but much larger memory usage, not good at handling high concurrency scenarios.

Apache in prefork affect the performance of the operating mode important parameters: 

The MPM the prefork # 

<IfModule mpm_prefork_module> 

     StartServers. 5     default number of child processes start when a start #Apache 

     the MinSpareServers. 5  # minimum number of idle sub-process 
 MaxSpareServer 10  # largest number of unused sub-process 
 MaxRequestWorkers 250 #MaxRequestWorkers sets the maximum allowed simultaneous access requests number. # Any request exceeds the limit MaxRequestWorkers will enter the waiting queue, in Apache2.3 is called MaxClients .1 MaxRequestWorkers the previous version, the old name is still supported. 500 MaxConnectionsPerChild # set is the number of requests each child process can handle. Each child process automatically destroyed after processing the "MaxConnectionsPerChild" requests. # 0 means unlimited, that the child process never destroyed. Although the default is set to 0 so that each child process can handle more requests, but if set to a nonzero value also has two important advantages: # (1 ) to prevent accidental memory leaks # (2) the decline in server load when it will automatically reduce the number of sub-processes. Thus, the server load can be adjusted according to this value. In Apache2.3 before .9 called MaxRequestsPerChild. </ IfModule>

note:

(1) MaxRequestWorkers these instructions is the most important one, is set Apache request can be processed simultaneously, it is the greatest impact Apache performance parameters. If the total number of requests has reached this value (via ps -ef | grep httpd | wc -l) to confirm, then subsequent requests will queue until a request has been processed. That left a lot of system resources and HTTP access the main reason is very slow. Although in theory the larger the value, the more requests may be processed, it is recommended to set an initial value (in MB physical memory Maximum / 2), and then dynamically adjusted according to the load.

4G memory such as a machine, then the initial value is 4000/2 = 2000.

After (2) prefork control the process of establishing "StartServers" sub-process initially set to meet the needs of MinSpareServers create a process, wait a second, continue to create two, and then wait for a second and continue to create four. . . . So by the exponential increase in the number of processes created, up to reach 32 per second, until the value is set to meet MinSpareServers. This model can not produce a new process at the request arrives, thereby reducing the system overhead in order to increase performance.

MaxSpareServers sets the maximum number of idle processes, if the idle process is greater than this value, Apache will automatically kill off some of the excess process. This value is not set too large, but if you set a value smaller than MinSpareServers, Apache will automatically adjust it to MinSpareServers + 1. If the site load is large, it can be considered at the same time increasing MinSpareServers and MaxSpareServers.

(3) ServerLimit and MaxClients (MaxRequestWorkers) What difference does it make?
Because Apache1 era, only the maximum number of control parameters MaxClients this process, and the maximum value of this parameter is 256, and is written dead, trying to set more than 256 is not valid, because Apache1 era of server hardware limitations. However, due to the age Apache2 server hardware upgrades, hardware is no longer the limit, so ServerServerLimit use this parameter to control the maximum number of processes, ServerLimit value> = MaxClient value is valid. ServerLimit be placed before MaxClients, either a value less than MaxClients.

(4) View Apache module loaded

/Applications/MAMP/Library/bin/apachectl -t -D DUMP_MODULES

/Applications/MAMP/Library/bin/apachectl -M

/Applications/MAMP/Library/bin/apachectl -l

How can I see Apache's work pattern here? You can use httpd -V command to view, additional use can also view the httpd -l

/Applications/MAMP/Library/bin/httpd -V

 

2.Work mode (multi-threaded multi-process)

和prefork模式相比,work使用了多进程多线程的混合模式,work模式也同样会先预派生一些子进程,然后每个子进程创建一些线程,同时包括一个监听线程,每个请求过来会被分配到一个线程来服务。线程比起进程会更轻量,因为线程是通过共享父进程的内存空间,因此,内存的占用会减少一些,在高并发的场景下会比prefork有更多可用的线程,表现会更优秀一些;另外,如果一个线程出现了问题也会导致同一进程下的线程出现问题,如果是多个线程出现问题,也只是影响Apache的一部分,而不是全部。由于用到多进程多线程,需要考虑的线程的安全了,在使用keep-alive长连接的时候,某个线程会一被占用,即使中间没有请求,需要等到超时才会被释放(该问题在prefork模式下也存在)

总的来说,prefork方式速度要稍高于worker,然而它需要的CPU和memory资源也稍多于worker。

Apache在worker工作模式下影响性能的重要参数说明:

<IfModule mpm_worker_module>
    StartServers          2     #apache启动时候默认开始的子进程数
    MaxClients          150     #MaxClients / MaxRequestWorkers  用于客户端请求的最大请求数量(最大子进程数)
    MinSpareThreads      25     #最小空闲数量的工作线程
    MaxSpareThreads      75     #最大空闲数量的工作线程
    ThreadsPerChild      25     #每个子进程产生的线程数量 MaxRequestsPerChild 0 #MaxRequestsPerChild / MaxConnectionsPerChild 每个子进程可处理的请求数 </IfModule>

注意:

(1)work由主控制进程生成“StartServers”个子进程,每个子进程中包含固定的ThreadsPerChild线程数,各个线程独立地处理请求。同样,为了不在请求到来时再生成线程,MinSpareThreads和MaxSpareThreads设置了最少和最多的空闲线程数;而MaxRequestWorkers设置了同时连入的clients最大总数。如果现有子进程中的线程总数不能满足负载,控制进程将派生新的子进程。

MinSpareThreads 和 MaxSpareThreads的最大缺省值分别是75和250。这两个参数对Apache的性能影响并不大,可以按照实际情况相应调节。

(2)ThreadsPerChild是worker MPM中与性能相关最密切的指令。ThreadsPerChild的最大缺省值是64,如果负载较大,64也是不够的。这时要显示使用ThreadLimit指令,它的最大缺省值是20000。

(3)worker模式下所能同时处理的请求总数是由子进程总数乘以ThreadsPerChild值决定的,应该大于等于MaxRequestWorkers。如果负载很大,现有的子进程数不能满足时,控制进程会派生新的子进程。默认最大的子进程总数是16,加大时也需要显示声明ServerLimit(系统配置的最大进程数量,最大值是20000)。需要注意的是,如歌显示声明了ServerLimit,那么它乘以 ThreadsPerChild的值必须大于等于MaxRequestWorkers,而且MaxRequestWorkers必须是ThreadsPerChild整倍数,否则Apache将会自动调节到相应的一个相应值。

(4)进程与线程的区别

线程是指进程内的一个执行单元,也是进程内的可调度实体。

与进程的区别是:

地址空间:进程内的一个执行单元,进程至少有一个线程,它们共享进程的地址空间,而进程有自己独立的地址空间。

资源拥有:进程是资源分配和拥有的单位,同一个进程内的线程共享进程的资源

线程是处理器调度的基本单位,但进程不是

二者均可并发执行

进程和线程都是由操作系统所体会的程序运行的基本单元,系统利用该基本单元实现系统对应用的并发性。

进程和线程的区别在于:

简而言之,一个程序至少有一个进程,一个进程至少有一个线程

线程的划分尺度小于进程,使得多线程程序的并发性高。

另外,进程在执行过程中拥有独立的内存单元,而多个线程共享内存,从而极大地提高了程序的运行效率。

 

3.Event模式:

这是Apache最新的工作模式,是worker模式的变种,它把服务进程从连接中分离出来,worker模式不同的是在于它解决了keep-alive长连接的时候占用线程资源被浪费的问题,在event工作模式中,会有一些专门的线程用来管理这些keep-alive类型的线程,当有真实请求过来的时候,将请求传递给服务器的线程,执行完毕后,由允许它释放。这增强了在高并发场景下的请求处理。event模式bu不能很好的支持https的访问(HTTP认证相关的问题)。

Apache配置参数详解

(1)keepAlive On/Off
keepAlive 指的是保持连接活跃,换一句话说,如果将KeepAlive设置为On,那么来自同一客户端的请求就不需要再一次连接,避免每次请求都要新建一个连接而加重服务器的负担。一般情况下,图片较多的网站应该把KeepAlive设为On。

(2)KeepAliveTimeOut number
如果第二次请求和第一次请求之间超过KeepAliveTimeOut的时间的话,第一次连接就会中断,再新建第二个连接。它的设置一般考虑图片或者JS等文件两次请求间隔,一般设置为3-5秒。

(3)MaxKeepAliveRequests 100
一次连接可以进行的HTTP请求的最大请求次数。将其值设为0将支持在一次连接内进行无限次的传输请求。
事实上没有客户程序在一次连接中请求太多的页面,通常达不到这个上限就完成连接了。

(4)HostnameLookups on|off|double
如果是使用on,那么只有进行一次反查,如果用double,那么进行反查之后还要进行一次正向解析,只有两次的结果互相符合
才行,而off就是不进行域名验证。
如果为了安全,建议使用double;为了加快访问速度,建议使用off
域名查找开启这个会增加Apache的负担,减慢访问速度建议关闭

(5)timeout 5
推荐5这个是Apache接受请求或者发出相应的时间超过这个时间断开

注意:
以上配置项可在/usr/local/http-2.4.23/conf/extra/httpd-default.conf设置并在httpd.conf文件中通过include选项引用


MPM这个比较关键,是影响并发效率的主要因素:

(1)StartServers  10
设置服务器启动时建立的子进程数量。因为子进程数量动态的取决于负载的轻重,所以一般没有必要调整这个参数。

(2)MinSpareServers    10
设置空闲子进程的最小数量。所谓空闲子进程是指没有正在处理的请求的子进程。如果当前空闲子进程数少于MinSpareServers,那么Apache将以最大每秒一个的速度产生新的子进程。只有在非常繁忙机器上才需要调整这个参数。将此参数设的太大通常是一个坏主意。

(3)MaxSpareThreads    75
设置空闲子进程的最大数量。如果当前有超过MaxSpareServers数量的空闲子进程,那么父进程将杀死多余的子进程。只有在非常繁忙机器上才需要调整这个参数。将此参数设的太大通常是一个坏主意。如果你将该指令的值设置为比MinSpareServers小,Apache将会自动将其修改成”MinSpareServers+1″。

(4)ServerLimit      2000
服务器允许配置的进程数上限。只有在你需要将MaxClients设置成高于默认值256的时候才需要使用。要将此指令的值保持和MaxClients一样。修改此指令的值必须完全停止服务后再启动才能生效,以restart方式重新启动将不会生效。

(5)MaxClients/MaxRequestWorkers        256
用于客户端请求的最大请求数量(最大子进程数),任何超过MaxClients限制的请求都将进入等候队列。默认值是256,如果要提高这个值必须同时提高ServerLimit的值。建议将初始值设为(以MB为单位的最大物理内存/2),然后根据负载情况进行动态调整。

比如一台4G内存的机器,那么初始值就是4000/2=2000。

(6)MaxRequestsPerChild / MaxConnectionsPerChild 0
设置的是每个子进程可处理的请求数,每个子进程在处理了“MaxRequestsPerChild”个请求后将自动销毁。0意味着无限,即子进程永不销毁。内存较大的服务器可以设置为0或较大的数字。内存较小的服务器不妨设置成30、50、100。所以一般情况下,如果你发现服务器的内存直线上升,建议修改该参数试试。

注意:
以上配置项可在/usr/local/http-2.4.23/conf/extra/httpd-mpm.conf设置并在httpd.conf文件中通过include选项引用。

4.开启Apache的Gzip(deflate)功能:
gzip可以极大的加速网站,有时压缩比率高到80%,最少都有40%以上,还是相当不错的。
在Apache2之后的版本,模块名不叫gzip,而叫mod_deflate

未使用Gzip

开启使用Gzip

 

(1)如果要开启deflate的话,一定要打开下面两个模块

LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so

设置压缩比率,取值范围在1(最低)到9(最高)之间,不建议设置太高,虽然有很高的压缩率,但是占用更多的CPU资源

注意:

 如果没有安装:

A:编译时安装方法:

编译的时候跟上--enable-deflate即可实现安装

B:DSO方式安装:

cd /root/httpd-2.4.23/modules/filters/ #切到apache源码包mod_deflate所在的目录下
# /usr/local/http-2.4.23/bin/apxs -c -i -a mod_deflate.c #以dso的方式编译安装到apache中
# /usr/local/http2.2/bin/apxs -c -i -a /root/httpd-2.2.17/modules/metadata/mod_headers.c #以dso的方式编译安装到apache中

[root@www filters]# ll /usr/local/http-2.4.23/modules/mod_deflate.so #检查mod_deflate是否安装,成功安装这里会显示出该文件
-rwxr-xr-x. 1 root root 98144 Oct 22 23:14 /usr/local/http-2.4.23/modules/mod_deflate.so

apxs命令参数说明:

-i :此选项表示需要执行安装操作,以安装一个或多个动态共享对象到服务器的modules目录中。

-a:此选项自动增加一个LoadModule行到httpd.conf文件中,以启用此模块,或者,如果此行已经存在,则启用之。

-c:此选项表示需要执行编译操作


注意:
如果重启的时候出现错误:
引用:

Cannot load /usr/local/apache/modules/mod_deflate.so into server: /usr/local/apache/modules/mod_deflate.so: undefined symbol: inflateEnd

需要在LoadModuledeflate_module  modules/mod_deflate.so 的前面加载zlib.so

这里需要注意的是:LoadModuledeflate_module需要放在LoadModulephp5_module之后
引用:

LoadFile/usr/lib/libz.so(x64系统中该库文件位于/usr/lib64目录下,可以软链接到/usr/lib下LoadModule deflate_module modules/mod_deflate.so)

(3)重新启动httpd

sudo /Applications/MAMP/Library/bin/apachectl  graceful

(4)修改Apache配置文件开启gzip压缩传输参数详解:

修改Apache主配置httpd.conf文件,增加配置参数:

LoadModule deflate_module     modules/mod_deflate.so
LoadModule headers_module     modules/mod_headers.so

打开httpd.conf后,先将上面两行配置前面的#号去掉,这样Apache就会启用这个两个模块,

其中mod_deflate是压缩模块,就是对要传输到客户端的代码进行gzip压缩;

mod_headers那么浏览器就会对gzip压缩过的页面进行下载,而无法正常显示。
在httpd.conf中加入以下代码,可以加到任何空白地方,不了解Apache的话,如果担心加错地方,就放到http.conf文件的最后一行
注意:

在添加代码前最好先查一查要添加的代码是否存在

<IfModule mod_deflate.c>
DeflateCompressionLevel 9  #压缩程度的等级,预设可以采用6这个数值,以维持耗用处理器效能与网页压缩质量的平衡。
SetOutputFilter DEFLATE    #设置输出过滤器,对输出启用压缩,必须的,就像一个开关一样,告诉Apache对传输到浏览器的内容进行压缩
#AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-javascript application/x-httpd-php  #设置对文件的内容进行压缩,例如text/html text/css text/plain等
#AddOutputFilterByType DEFLATE image/*    注意:图片不需要进行压缩,否则越压缩越大
AddOutputFilterByType DEFLATE text/*
AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript application/javascript application/x-javascript #设置对JavaScript文件进行压缩
AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp  #设置对PHP类型的文件进行压缩
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary                   #设置不对后缀gif,jpg,jpeg,png的图片文件进行压缩。
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary        #设置不对exe,tgz,gz等的文件进行压缩
SetEnvIfNoCase Request_URI .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary          #设置不对pdf,avi,mp3等的文件进行压缩
</IfModule>

 

(5)设置日志输出

DeflateFilterNote Input input_info     #声明输入流的byte数量
DeflateFilterNote Output output_info   #声明输出流的byte数量
DeflateFilterNote Ratio ratio_info     #声明压缩的百分比
LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate   #声明日志格式
CustomLog logs/deflate_log.log deflate

(6)修改后保存退出并重启httpd服务

sudo /Applications/MAMP/Library/bin/apachectl  graceful

查看日志:

注意:

 图片是不需要启用Gzip压缩的,接下来不妨做个测试,针对图片做压缩后的效果验证。

(1)首先上传一张图片到Apache根目录

(2)编辑index.html首页把图片加上

(3)重启httpd服务

(4)访问测试页面,查看日志图片压缩前的大小

  查看日志:

(5)那么,接下来开启gzip图片的压缩功能

(6)保存退出,重启服务

(7)再次查看日志以及验证:

 

从Gzip检测结果来看,压缩后的图片体积竟然大过原体积!这就解释了为什么图片不用启用GZip压缩的原因了!
可以检测了几个门户网站的图片,还有Google、baidu的图片,统统都没有启用图片GZip压缩,只是启用了html、css、js等文件的GZip压缩,这就更加说明了GZip压缩不适用于图片上。另外,除了图片之外,flash的swf文件也是不用启用GZip压缩的。

5.配置mod_expires模块

  这个是非常有用的优化,mod_expires可以减少20-30%左右的重复请求,让重复的用户对指定的页面请求结果都cache在本地,根本不向服务器发出请求。但要注意更新快的文件不要这么做。
这个模块控制服务器应答时的Expires头内容和Cache-Control头的max-age指令。有效期(expirationdate)可以设置为相对于源文件的最后修改时刻或者客户端的访问时刻。

 (1)未启用Expires的效果:

curl -I w.tt.com
HTTP/1.1 200 OK
Date: Sat, 29 Jun 2019 08:46:58 GMT
Server: Apache
X-Powered-By: PHP/7.3.1
Content-Type: text/html; charset=UTF-8

(2)启用Expires缓存:

LoadModule expires_module modules/mod_expires.so

(3)然后添加Expires配置规则:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "now plus 1 month"
ExpiresByType application/x-javascript "now plus 5 day"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/bmp "access plus 1 month" ExpiresByType image/x-icon "access plus 1 month" ExpiresByType image/png "access plus 1 minutes" ExpiresByType application/x-shockwave-flash "access plus 1 month" ExpiresDefault "now plus 0 minutes" </IfModule> 

(4)重启服务,并验证

ExpiresDefault 和ExpiresByType指令同样能够用易懂的语法格式进行定义:

ExpiresDefault "<base> [plus] {<num><type>}"
ExpiresByType type/encoding "<base> [plus] {<num><type>}"

其中<base>是下列之一:
• access
• now(等价于'access ')
• modification                             
   plus关键字是可选的。<num>必须是整数,<type>是下列之一:

• years //年 
• months//月 
• weeks//星期 
• days/日 
• hours/时 
• minutes/分 
• seconds/秒
例如,下列3个指令都表示文档默认的有效期是一个月:

ExpiresDefault "access plus 1 month"
ExpiresDefault "access plus 4 weeks"
ExpiresDefault "access plus 30 days"

有效期可以通过增加"<num><type>"子句进一步调整:

ExpiresByType text/html "access plus 1 month 15 days 2 hours"
ExpiresByType image/gif "modification plus 5 hours 3 minutes"

注意:

如果你使用基于最后修改日期的设置,"Expires:"头将不会 被添加到那些并非来自于磁盘文件的内容。这是因为这些内容并不存在"最后修改时间"的属性。

 #GIF有效期为1个月(秒数)

ExpiresByType image/gif A2592000 
ExpiresByType image/jpeg A2592000 
ExpiresByType image/png A2592000 
ExpiresByType image/x-icon A2592000 
ExpiresByType application/x-javascript A604800 
ExpiresByType text/plain A604800

 

HTML文档的有效期是最后修改时刻后的一星期 

ExpiresByType text/html M604800 

注意:

以上"M"表示源文件的最后修改时刻,"A"表示客户端对源文件的访问时刻。后面的时间则以秒计算。

 

6.Apache禁止目录遍历(安全)

将Options Indexes FollowSymLinks中的Indexes 去掉,就可以禁止 Apache 显示该目录结构。

注意:

Indexes 的作用就是当该网站目录下没有 index.html文件时,就会显示目录结构。

 

7.Apache隐藏版本信息(安全)

 (1)修改如下配置文件:

vim /usr/local/http-2.4.23/conf/extra/httpd-default.conf

找到:

ServerTokens Full
ServerSignature On
改成:
ServerTokens Prod
ServerSignature off

(2)重启Apache并测试:

注意:

如果你需要彻底将版本之类的信息进行改头换面,你就需要在编译之前做准备或者进行从新编译了。在重新编译时,修改源码包下include目录下的ap_release.h文件

#define AP_SERVER_BASEVENDOR "Apache Software Foundation" #服务的供应商名称
#define AP_SERVER_BASEPROJECT "Apache HTTP Server"  #服务的项目名称
#define AP_SERVER_BASEPRODUCT "Apache"        #服务的产品名
#define AP_SERVER_MAJORVERSION_NUMBER 2  #主要版本号
#define AP_SERVER_MINORVERSION_NUMBER 4  #小版本号
#define AP_SERVER_PATCHLEVEL_NUMBER 23  #补丁级别
#define AP_SERVER_DEVBUILD_BOOLEAN  0  #

上述列出的行,已经给出了注释,大家可以修改成自己想要的,然后编译安装之后,对方就彻底不知道你的版本号了。

 

8、Apache日志分割(安全)

简介:
为什么要日志分割?

随着网站的访问越来越大,WebServer产生的日志文件也会越来越大,如果不对日志进行分割,那么只能一次将大的日志(如Apache的日志)整个删除,

这样也丢失了很多对网站比较宝贵的信息,因为这些日志可以用来进行访问分析、网络安全监察、网络运行状况监控等,

因此管理好这些海量的日志对网站的意义是很大的。

方法1:使用rotatelogs(apache自带的工具)每隔一天记录一个日志

(1)编辑Apache的主配置文件,更改内容如下:
注释掉如下两行:

ErrorLog logs/error_log
CustomLog logs/access_log common

然后添加如下两行:

ErrorLog "|/usr/local/http-2.4.23/bin/rotatelogs -l logs/error_%Y%m%d.log 86400"
CustomLog "|/usr/local/http-2.4.23/bin/rotatelogs -l logs/access_%Y%m%d.log 86400" combined

注意:其中86400为轮转的时间单位为秒

(2.)重启Apache验证:查看logs目录下的日志文件

注意:

由于apache自带的日志轮询工具rotatelogs,据说在进行日志切割时容易丢日志,因此我们通常使用cronolog进行日志轮询。

方法2:使用 cronolog 为每一天建立一个新的日志

(1)下载安装cronolog程序

官网下载地址:http://cronolog.org/download/cronolog-1.6.2.tar.gz

tar zxf cronolog-1.6.2.tar.gz 
cd cronolog-1.6.2/
./configure && make && make install

(2)安装完成后,进到Apache主配置文件添加如下两行:

ErrorLog "|/usr/local/sbin/cronolog logs/error-%Y%m%d.log"
CustomLog "|/usr/local/sbin/cronolog logs/access-%Y%m%d.log" combined

 

注意:

如果Apache中有多个虚拟主机,最好每个虚拟主机中放置一个这样的代码,并将日志文件名改成不同的名字。

(3)重启Apache验证,查看logs目录下的日志文件

注意:

这两个管道日志文件程序还有一点不同之处是,使用cronolog 时如果日志是放在某个不存在的路径则会自动创建目录,而使用 rotatelogs 时不能自动创建,这一点要特别注意

 

扩展:

#这个保证了每天一个文件夹文,件夹下每个小时产生一个log

CustomLog "|/usr/local/sbin/cronolog logs /%Y%m%d/access_log.%H" combined  

按天轮询(生产环境常见用法,推荐使用):

CustomLog "|/usr/local/sbin/cronolog logs/access_www_%Y%m%d.log" combined

按小时轮询(生产环境较常见用法):

CustomLog "|/usr/local/sbin/cronolog logs /access_www_ %Y%m%d%H.log" combined

总结:
(1.)Apache常见的三种工作模式:prefork、work、event(概念)
(2.)Apache针对prefork模式下的调优、针对work模式下的调优(调优)
(3.)Apache开启Gzip压缩功能的调优(调优)
(4.)Apache开启Expires缓存功能的调优(调优)
(5.)实现Apache禁止目录遍历的功能(安全)
(6.)实现Apache隐藏版本号的功能(安全)
(7.)实现Apache日志切割的功能(安全)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/mzhaox/p/11230945.html