在搭建 Apache 业务没上线之前,我们需要不断去测试性能以达到甚至远远超过预期,因此我们需要对 Apache 进行深度优化,本次我们将介绍 Apache 的优化及工作模式详解。 Apache 优化在 Linux 中搭建的 Apache ,要进行优化必须是在编译安装环境下进行,因此我们需要对本机自带的 httpd 进行卸载。并重新下载并编译安装。要注意,在./配置的时候,要添加以下模块。 ./configure \--prefix=/usr/local/httpd \ ##安装目录--enable-deflate \ ##压缩模块--enable-expires \ ##缓存模块--enable-so \--enable-rewrite \ ##重定向模块--enable-charset-lite \--enable-cgi 一、配置压缩模块打开主配置文件,打开压缩模块 vim /usr/local/httpd/conf/httpd.confLoadModule headers_module modules/mod_headers.soLoadModule deflate_module modules/mod_deflate.so //开启 去掉前面#LoadModule filter_module modules/mod_filter.so##在末尾添加下面的行<IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript ##支持压缩的格式 DeflateCompressionLevel 6 ##压缩比 SetOutputFilter DEFLATE</IfModule> 至此已经配置好了,我们可以使用脚本验证下语法是否存在错误,验证脚本在 /usr/local/httpd/bin/ 目录中。 [root@CentOS7-2 ~]# cd /usr/local/httpd/bin/[root@CentOS7-2 bin]# ./apachectl -t -D DUMP_MODULES | grep "deflate" deflate_module (shared) 二、网页缓存模块主配置文件中打开模块,并在末尾添加语块。 vim /usr/local/httpd/conf/httpd.confLoadModule expires_module modules/mod_expires.so ##开启 ##在末尾添加下面的行 <IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 50 seconds" ##访问最大缓存时间50s</IfModule> 查看模块是否正常开启 [root@CentOS7-2 ~]# cd /usr/local/httpd/bin/[root@CentOS7-2 bin]# ./apachectl -t -D DUMP_MODULES | grep "expires"expires_module (shared) 三、安全性优化此项主要是对网络日益严重的盗链现象,防止本站点网址图片被别人盗链使用。 主配置文件修改 vim /usr/local/httpd/conf/httpd.conf LoadModule rewrite_module modules/mod_rewrite.so ##开启
查看模块是否正常开启 [root@CentOS7-2 ~]# cd /usr/local/httpd/bin/[root@CentOS7-2 bin]# ./apachectl -t -D DUMP_MODULES | grep "rewrite" rewrite_module (shared) 四、隐藏版本信息打开主配置文件,开启default配置文件 vim /usr/local/httpd/conf/httpd.confInclude conf/extra/httpd-default.conf ##开启 vim /usr/local/httpd/conf/extra/httpd-default.conf ServerTokens Prod //只显示名称,没有版本ServerSignature Off Apache 工作模式三种工作模式详解我们可以通过脚本查看当前工作模式 cd /usr/local/httpd/bin/ ./httpd -l
如何在三种模式中切换在编译安装的配置中,指定--with-mpm=NAME 选项指定MPM,NAME就是你想使用的MPM的名称。不指定模式的话,默认为Prefork MPM。 配置模式参数开启mpm配置文件模块 vim /etc/httpd.conf Include conf/extra/httpd-mpm.conf 编辑配置模式参数 vim /usr/local/httpd/conf/extra/httpd-mpm.conf <IfModule mpm_prefork_module> StartServers 10 # 启动时进程数 MinSpareServers 10 # 最小空闲进程数 MaxSpareServers 50 # 最大空闲进程数 MaxRequestWorkers 150 #最大并发进程数 MaxConnectionsPerChild 0 # 最大连接数限制</IfModule> 三种配置模式的配置都是一样的,我们只需要对部分参数根据模式进行修改就可以了。 ©著作权归作者所有:来自51CTO博客作者你讲多次的原创作品,如需转载,请注明出处,否则将追究法律责任 it学习社区 http://www./ |
|