分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

Apache优化

发布时间:2023-09-06 02:06责任编辑:蔡小小关键词:暂无标签
优化:根据用户访问量(UV、PV)及其它综合考虑,调整系统及软件的参数,将服务器性能最大化利用

并发:同一时刻网站的访问量

用户浏览量UV(Unique Visitor):既独立访客,访问您的网站的上网电脑数量(以cookie为依据)。(单用户刷新算一次)

页面浏览量PV(PageView):当天网站的访问量(单用户刷新算多次)。此指标衡量网站访问量情况。

一、Apache调优


(1)调研(如apache需调研用户访问量、需要调整的内容、防盗链等)

(2)实施优化(优化未上线服务器,提升服务器性能和并发量)

(3)测试性能

ab <-nN总连接数> <-cN并发数> <网站(如:www.baidu.com/index.html)> ???//测试服务器访问性能

选项:
-k:使用HTTP连接保持
-c:请求的进程数
-n:请求的线程数
(4)上线

准备域名、公网IP→服务器→部署服务(Apache) → 网页文件

(5)DNAT(服务器内网IP:80映射公网IP:80)

www.hiahia.com:80 --> 服务器公网IP:80(23.12.45.11:80) --> 服务器内网IP:80(192.168.1.10:80)

二、Apache工作模式


1.perfork


(1)没有线程概念,是多进程模型,一个进程处理一个连接,稳定、快;当连接数大时消耗大量内存

(2)优化

vim /usr/local/httpd/conf/httpd.conf

<ifModule mpm_prefork_module> ???ServerLimit 40 ?????????//最大开启的进程数 ???StartServers 2 ?????????//服务器启动时建立的子进程数 ???Maxclients 1000 ????????//允许同时接入最大请求数量(最大线程数量) ???MinSpareThreads 5 ??????????//空闲进程最小数量 ???ThreadsPerChild 150 ????//进程建立的子进程常驻数 ???MaxRequestPerChild 1000 ????//每个子进程在存活期间允许的最大请求数量</ifModule>

2.worker


(1)多进程、多线程,一个进程多个线程,一个线程处理一个连接;与perfork更省内存,与php等有兼容问题
(2)优化
vim /usr/local/httpd/conf/httpd.conf

<ifModule mpm_worker_module> ???ServerLimit 40 ?????????//最大开启的进程数 ???ThreadLimit 200 ????????//每个进程最大开启的线程数 ???StartServers 2 ?????????//服务器启动时建立的子进程数 ???Maxclients 1000 ????????//允许同时接入最大请求数量(最大线程数量) ???MaxSpareThreads 100 ????//空闲线程最大数量 ???MinSpareThreads 5 ??????????//空闲线程最小数量 ???ThreadsPerChild 150 ????//每个子进程建立的常驻线程数 ???MaxRequestPerChild 1000 ????//每个子进程在存活期间允许的最大请求数量 ??</ifModule>

3.event


worker增强版,提供更好的并发负载能力,不能很好与https支持

vim /usr/local/httpd/conf/httpd.conf

<IfModule mpm_event_module> ???StartServers ????????????3 ???MinSpareThreads ????????75 ???MaxSpareThreads ???????250 ???ThreadsPerChild ????????25 ???MaxRequestWorkers ?????400 ???MaxConnectionsPerChild ??0</IfModule>

4.查看当前工作模式:httpd -V

三、Apache其余优化方式


1.网页压缩


a.查询是否安装mod_deflate模块
b.修改配置文件启动模块
c.测试

(1)判断是否支持mod_deflate模块

apachectl -t -D DUMP_MODULES ???????//查看是否deflate_module

//也可以使用apachectl -l查看安装的所有模块

(2)重新编译安装支持模块

a.httpd-2.2.x

cd httpd-2.2.17/
yum -y install zlib-devel
./configure --prefix=/usr/local/httpd/ --enable-so --enable-charset-lite --enable-rewrite --enable-cgi --enable-deflate
make && make install

b.httpd-2.4.x

cd httpd-2.4.34/ ???//进入解压目录
yum -y install zlib-devel ???//yum安装zlib-devel包
./configure --prefix=/usr/local/httpd/ --enable-so --enable-charset-lite --enable-rewrite --enable-cgi --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre **--enable-deflate** ???//配置安装压缩模块
make && make install ???//编译并安装

(3)修改配置文件启动模块支持

a.httpd-2.2.x

vim /usr/local/httpd/conf/httpd.conf

AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript ???????//指定需要压缩的文件格式DeflateCompressionLevel 9 ??????????????????????????????//指定压缩的等级SetOutputFilter DEFLATE ????????????????????????????//对所有输出启用压缩

b.httpd-2.4.x

vim /usr/local/httpd/conf/httpd.conf

105 LoadModule deflate_module modules/mod_deflate.so ???????????????????//加载deflate模块<IfModule mod_deflate.c>AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript ???????//指定需要压缩的文件格式DeflateCompressionLevel 9 ??????????????????????????????//指定压缩的等级SetOutputFilter DEFLATE ????????????????????????????//对所有输出启用压缩</IfModule>

(4)验证

/etc/init.d/httpd restart ???//重启httpd服务


注:httpd1.x和2.x都使用gzip压缩算法

2.网页缓存


a.查询是否安装mod_expire模块
b.修改配置文件启动模块
c.测试

(1)判断是否支持mod_expire模块

apachectl -t -D DUMP_MODULES ???????//查看是否expire_module

(2)重新编译安装支持模块

a.httpd-2.2.x

cd httpd-2.2.17/
yum -y install zlib-devel
./configure --prefix=/usr/local/httpd/ --enable-so --enable-charset-lite --enable-rewrite --enable-cgi --enable-deflate --enable-expires 
make && make install

b.httpd-2.4.x

cd httpd-2.4.34/
yum -y install zlib-devel
./configure --prefix=/usr/local/httpd/ --enable-so --enable-charset-lite --enable-rewrite --enable-cgi --enable-deflate --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre **--enable-expires** ???//配置添加缓存模块
make && make install ???//编译并安装

(3)修改配置文件启动模块支持

a.httpd-2.2.x

vim /usr/local/httpd/conf/httpd.conf

<ifModule mod_expires.c> ?ExpiresActive On ?????????????????//开启expires模块 ?ExpiresDefault "access plus 60 seconds" ??????//缓存60秒</ifModule>

b.httpd-2.4.x

vim /usr/local/httpd/conf/httpd.conf

111 LoadModule expires_module modules/mod_expires.so<ifModule mod_expires.c> ?ExpiresActive On ?????????????????//开启expires模块 ?ExpiresDefault "access plus 60 seconds" ??????//缓存60秒</ifModule>

(4)验证

/etc/init.d/httpd restart ??????????????//Expires:Sun, 04 Mar 2018 02:58:59 GMT

3.防盗链


a.查询是否安装mod_rewrite模块
b.修改配置文件启动模块
c.测试

(1)判断是否支持mod_rewrite模块

apachectl -t -D DUMP_MODULES ???????//查看是否rewrite_module

(2)修改配置文件启动模块支持

注:httpd2.2.x这里与httpd2.4.x写法一样

vim /usr/local/httpd/conf/httpd.conf

httpd-2.4159 LoadModule rewrite_module modules/mod_rewrite.so226 <Directory "/usr/local/httpd/htdocs">下252 RewriteEngine On ???????????????????????//启用rewrite地址重写功能253 RewriteCond %{HTTP_HOST} !^www.hiahia.com [NC] ?????????//定义信任的站点254 RewriteRule .*\.(.gif|jpg|swf|mp4|mp3)$ http://aa.com/a.html[R,NC] ?//将未信任站台,以gif、jpg等结尾访问站点的全部重定向到http://aa.com/a.html
/etc/init.d/httpd restart ???//重启服务

(3)测试

①编辑Windows的hosts文件(C:\Windows\System32\drivers\etc\hosts\)写入下面两条,便于客户端访问

192.168.10.200 www.hiahia.com ???//Apache服务器的IP与对应域名
192.168.10.100 www.hehe.com ???//要盗链的服务器

②将要浏览的文件放到Apache服务器的/usr/local/httpd/htdocs/下

③在要盗链的服务器,新建网页文件,盗取地址

④客户端访问:
www.hiahia.com/文件 ??????//允许访问
www.hehe.com/hehe.html ?????//不允许访问,自动跳转到aa.com/a.html

4.隐藏版本信息


vim /usr/local/httpd/conf/httpd.confServerTokens Prod ??????????????????//只显示Web服务器软件ServerSignature Off ????????????//关闭管理员信息
/etc/init.d/httpd restart ???//重启服务

Apache优化

原文地址:http://blog.51cto.com/13770206/2152112

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved