Mac系统下Apache环境检查与启动
Mac操作系统中预装了Apache服务环境。
在“终端”应用中执行 sudo apachectl -v 命令可查看Apache版本信息(系统可能要求输入管理员密码)。执行成功后将显示相应的版本详情。

随后运行 sudo apachectl start 即可启动Apache服务。此时在Safari浏览器地址栏输入 http://localhost,若看到显示“It works!”的页面,则说明服务正常。该页面文件位于 /Library/WebServer/Documents/ 目录下,此为Apache默认的根目录路径。
Apache的主配置文件目录位于 /etc/apache2/,其中 /etc 文件夹默认处于隐藏状态。用户可以通过以下三种方式访问该目录:
1. 在Dock栏右键点击Finder图标,选择“前往文件夹”,输入路径“/etc”。 2. 在Finder菜单栏中选择“前往”->“前往文件夹”,然后输入/etc。 3. 直接在Terminal终端中输入 open /etc 命令打开。
Windows环境下,用户可以前往 http://httpd.apache.org/download.cgi 下载预编译包,或通过命令行方式进行安装配置。
首先完成Apache的安装步骤。
sudo port install apache2
启动Apache服务:sudo apachectl start
其次,通过 brew install pcre 安装必要的依赖库。
接着使用 wget http://ftp.yz.yamagata-u.ac.jp/pub/network/apache//httpd/httpd-2.4.4.tar.bz2 下载源码包。
执行以下步骤完成编译安装: (1) 解压压缩包 (2) 运行 ./configure 进行配置 (3) 执行 make && make install 进行编译和安装 (4) 复制ab工具到系统路径:sudo cp support/ab /usr/sbin
执行测试命令:ab -n 1000 -c 10 http://localhost:3000/ 。注意URL末尾的斜杠不可省略。
在Windows系统中运行上述 ab -n 1000 -c 10 http://localhost:3000/ 命令前,需先使用 cd 命令切换至Apache安装目录下的bin文件夹。
以下是ab命令常用参数的详细说明。
-n 参数用于指定需要执行的总请求次数。
-c 参数用于设定并发连接的数量。
-t 参数用于设置等待服务器返回响应的最长时间。
-b 参数用于指定TCP收发缓冲区的大小,单位为字节(byte)。
-p 参数用于指定使用POST方法发送请求(同时必须定义-T参数以指定内容类型)。
-u 参数用于指定使用PUT方法发送请求(同时必须定义-T参数)。
-T 参数用于定义Content-Type,例如 application/x-www-form-urlencoded,默认为 text/plain。
-w 参数用于将测试结果以HTML表格的形式打印输出。
-x 参数用于设置HTML表格中<table>标签的属性。
-y 参数用于设置HTML表格中<tr>行的属性。
-z 参数用于设置HTML表格中<td>列的属性。
-C 参数用于设置Cookie,例如 Apach=1234。
-H 参数用于添加自定义Header行,例如 Accept-Encoding:gzip。
-k 参数用于启用HTTP Keep Alive功能。
接下来展示典型的测试结果输出样例。
This is ApacheBench, Version 2.3 <$Revision: 1663405 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software:
Server Hostname: localhost
Server Port: 3000
Document Path: /
Document Length: 14 bytes
Concurrency Level: 10
Time taken for tests: 0.323 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 115000 bytes
HTML transferred: 14000 bytes
Requests per second: 3097.37 [#/sec] (mean)
Time per request: 3.229 [ms] (mean)
Time per request: 0.323 [ms] (mean, across all concurrent requests)
Transfer rate: 347.85 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 1
Processing: 1 3 2.2 2 16
Waiting: 1 3 2.2 2 16
Total: 1 3 2.2 2 16
Percentage of the requests served within a certain time (ms)
50% 2
66% 3
75% 3
80% 4
90% 5
95% 7
98% 13
99% 15
100% 16 (longest request)
以上内容涵盖了Apache ab工具的基本使用与参数解析,希望能对读者的学习与实践提供帮助,欢迎继续支持本站获取更多技术资源。