准备工作
主机名 | 操作系统 | IP地址 | 版本号 |
---|---|---|---|
Apache | CentOS 7.4 | 192.168.1.1 | httpd-2.2.17.tar.gz |
DaoTu | CentOS 7.4 | 192.168.1.2 | httpd-2.2.17.tar.gz |
[root@Apache ~]# wget http://archive.apache.org/dist/httpd/httpd-2.2.17.tar.gz
[root@Apache ~]# yum -y install zlib-devel
[root@Apache ~]# tar zxf httpd-2.2.17.tar.gz -C /usr/src/
[root@Apache ~]# cd /usr/src/httpd-2.2.17/
[root@Apache httpd-2.2.17]# ./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-cgi \
--enable-deflate \
--enable-expires && make && make install
注解:
--enable-deflate
:启用 deflate
模块(需安装 zlib-devel
依赖包)--enable-expires
:启用网页缓存过期时间模块。[root@Apache httpd-2.2.17]# ln -s /usr/local/httpd/bin/* /usr/local/bin/ #优化执行路径
[root@Apache ~]# httpd -v #查看httpd版本
[root@Apache ~]# sed -i '97s/^#//g' /usr/local/httpd/conf/httpd.conf #将 # 替换为空
[root@Apache ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd #复制启动文件
[root@Apache ~]# /etc/init.d/httpd start #启动httpd服务
[root@Apache ~]# netstat -anpt | grep 80 #查看端口是否开启
将 Apache 主机编译好的,远程复制到 DaoTu 机器中:
[root@Apache ~]# scp -r /usr/local/httpd/ root@192.168.1.2:/usr/local/httpd/
配置项 | 作用 |
---|---|
KeepAlive On OFF |
是否打开保持连接功能 |
MaxKeepAliveRequests | 一次 TCP 连接能够传输的最大 HTTP 请求数量 |
KeepAliveTimeout | 一次 TCP 连接多次 HTTP 请求之间的最大间隔时间 |
[root@Apache ~]# vim /usr/local/httpd/conf/extra/httpd-default.conf
16 KeepAlive On
23 MaxKeepAliveRequests 100
29 KeepAliveTimeout 20
[root@Apache ~]# sed -n '/httpd-default.conf/p' /usr/local/httpd/conf/httpd.conf
[root@Apache ~]# sed -i '/httpd-default.conf/s/^#//' /usr/local/httpd/conf/httpd.conf
[root@Apache ~]# sed -n '/httpd-default.conf/p' /usr/local/httpd/conf/httpd.conf
[root@Apache ~]# /etc/init.d/httpd restart
[root@Apache ~]# netstat -anpt | grep 80
客户端在请求 httpd 服务器数据后,httpd 服务器在返回数据包给客户端时,会先对返回的数据进行压缩,压缩之后再传输。
作用:
优势:
方式:Apache 能实现网页压缩功能的模块有 mod_gzip
模块和 mod_deflate
模块。
配置项 | 作用 |
---|---|
AddOutputFilterByType DEFLATE test/html test/plain test/css test/xml test/javascript | 对什么格式的内容启用压缩 |
DeflateCompressionLevel 9 | 压缩级别为 9,范围是 1-9,数字大压缩率高 |
SetOutputFilter DEFLATE | 启动 deflate 压缩方式 |
[root@Apache ~]# apachectl -D DUMP_MODULES | grep deflate
[root@Apache ~]# cat <<END >> /usr/local/httpd/conf/httpd.conf
AddOutputFilterByType DEFLATE test/html test/plain test/css test/xml test/javascript
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
END
[root@Apache ~]# /etc/init.d/httpd restart
[root@Apache ~]# netstat -anpt | grep 80
mod_expires
模块配置 Apache,使网页能在客户端浏览器缓存一段时间,以避免重复请求,减轻服务端工作压力。mod_expires
模块后,会自动生成页面头部信息中的 Expires 标签和 CacheControl 标签;[root@Apache ~]# apachectl -D DUMP_MODULES | grep expires
[root@Apache ~]# cat <<END >> /usr/local/httpd/conf/httpd.conf
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 60 seconds"
</IfModule>
END
[root@Apache ~]# /etc/init.d/httpd restart
[root@Apache ~]# netstat -anpt | grep 80
[root@Apache ~]# sed -n '/^Server/p' /usr/local/httpd/conf/extra/httpd-default.conf
[root@Apache ~]# sed -i 's/ServerTokens Full/ServerTokens Prod/' /usr/local/httpd/conf/extra/httpd-default.conf
[root@Apache ~]# sed -i 's/ServerSignature On/ServerSignature Off/' /usr/local/httpd/conf/extra/httpd-default.conf
[root@Apache ~]# sed -n '/^Server/p' /usr/local/httpd/conf/extra/httpd-default.conf
[root@Apache ~]# /etc/init.d/httpd restart
[root@Apache ~]# netstat -anpt | grep 80
如果是源代码编译安装,还可以用修改源代码编译的方式:
include
目录,编辑 ap_release.h
这个文件,然后重新编译安装即可。[root@Apache ~]# vim /usr/src/httpd-2.2.17/include/ap_release.h
将:
42 #define AP_SERVER_BASEVENDOR "Apache Software Foundation"
43 #define AP_SERVER_BASEPROJECT "Apache HTTP Server"
44 #define AP_SERVER_BASEPRODUCT "Apache"
46 #define AP_SERVER_MAJORVERSION_NUMBER 2
47 #define AP_SERVER_MINORVERSION_NUMBER 2
48 #define AP_SERVER_PATCHLEVEL_NUMBER 17
修改为:
42 #define AP_SERVER_BASEVENDOR "Coco"
43 #define AP_SERVER_BASEPROJECT "Coco"
44 #define AP_SERVER_BASEPRODUCT "CSDN 愿许浪尽天涯"
46 #define AP_SERVER_MAJORVERSION_NUMBER 8
47 #define AP_SERVER_MINORVERSION_NUMBER 8
48 #define AP_SERVER_PATCHLEVEL_NUMBER 8
[root@Apache ~]# cat /usr/local/httpd/build/config.nice
[root@Apache ~]# cd /usr/src/httpd-2.2.17/
[root@Apache httpd-2.2.17]# ./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-cgi \
--enable-deflate \
--enable-expires && make clean && make && make install
[root@Apache ~]# /etc/init.d/httpd stop
[root@Apache ~]# /etc/init.d/httpd start
[root@Apache ~]# netstat -anpt | grep 80
[root@Apache ~]# httpd -v
rewrite
模块进行优化。[root@Apache ~]# vim /usr/local/httpd/htdocs/index.html
<html>
<body>
<h1>It works!</h1>
<img src="./1.jpg"/>
</body>
</html>
[root@Apache ~]# cd /usr/local/httpd/htdocs/
[root@Apache htdocs]# ls
1.jpg index.html
[root@Apache htdocs]# /etc/init.d/httpd start
[root@Apache htdocs]# netstat -anpt | grep 80
[root@DaoTu ~]# vim /usr/local/httpd/htdocs/index.htm
<html>
<body>
<h1>It works!</h1>
<img src="http://192.168.1.1/1.jpg"/>
</body>
</html>
[root@DaoTu ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@DaoTu ~]# /etc/init.d/httpd start
[root@DaoTu ~]# netstat -anpt | grep 80
配置项 | 作用 |
---|---|
RewriteEngine On | 打开网页重写功能 |
RewriteCond | 设置匹配规则 |
RewriteRule | 设置跳转动作 |
Rewrite 的规则:%{HTTP_REFERER} 浏览 header 中链接字段,存放一个连接的 URL,代表是从哪个链接访问所需的页面。
!^
:不以某个字段开头。.*$
:以任意字符结尾。NC
:不区分大小写。R
:强制跳转。[root@localhost htdocs]# vim /usr/local/httpd/conf/httpd.conf
131 <Directory "/usr/local/httpd/htdocs">
添加如下:
RewriteEngine On
RewriteCond %{
HTTP_REFERER} !^http://192.168.1.1/.*$ [NC]
RewriteCond %{
HTTP_REFERER} !^http://192.168.1.1$ [NC]
RewriteRule .*\(gif|jpg|swf\)$ http://192.168.1.1/2.png [R,NC]
</Directory>
[root@Apache htdocs]# ls
1.jpg 2.png index.html
[root@Apache htdocs]# /etc/init.d/httpd restart
[root@Apache htdocs]# netstat -anpt | grep 80
[root@Apache htdocs]# /etc/init.d/httpd restart
语法:ab -n 请求数总和 -c 并发用户数 网站网址
主要参数 | 作用 |
---|---|
Time taken for tests | 表示所有这些请求被处理完成所花费的总时间。 |
Failed requests | 表示失败的请求数量。 |
Transfer rete | 表示这些请求在单位时间内从服务器获取的数据长度。 |
测试:
[root@Apache ~]# /usr/local/httpd/bin/ab -n 1000 -c 200 http://192.168.1.1/