3人参与 • 2026-04-09 • Windows
本文基于 apache 2.4 官方文档,完全适配 windows 系统,从安装、基础配置、监听绑定、虚拟主机、url 重写、ssl、缓存、日志、性能调优到安全加固,形成一份可直接落地的 windows 运维文档。
apache 官方不提供 exe 安装包,windows 版本以压缩包形式分发:
apache 2.4.x win64解压到无空格、无中文路径,例如:
c:\apache24\ ├── bin\ httpd.exe、服务管理工具 ├── conf\ 主配置目录 ├── htdocs\ 默认网站根目录 ├── logs\ 日志目录 └── modules\ 模块文件(*.so)
以管理员身份打开 cmd,进入 bin 目录:
cd c:\apache24\bin
安装服务:
httpd -k install
启动服务:
httpd -k start
停止 / 重启 / 卸载:
httpd -k stop httpd -k restart httpd -k uninstall
验证安装:
打开浏览器,访问 http://localhost/。如果看到 "it works!" 的默认页面,就说明 apache 已经成功安装并运行了。
文件:conf/httpd.conf
# 监听所有网卡 80 端口 listen 0.0.0.0:80 # 监听 ipv6 listen [::]:80 # 自定义端口 listen 8080 # https 监听 listen 443 https
servername localhost:80
define srvroot "c:/apache24"
serverroot "${srvroot}"
documentroot "${srvroot}/htdocs"
<directory "${srvroot}/htdocs">
options indexes followsymlinks
allowoverride none
require all granted
</directory><directory>:目录权限<files>:文件匹配<location>:url 路径匹配<virtualhost>:虚拟主机<ifmodule>:模块加载判断示例:
<files ".env">
require all denied
</files>errorlog "${srvroot}/logs/error.log"
loglevel warn报警级别: debug, info, notice, warn, error, crit,alert, emerg.
logformat "%h %l %u %t \"%r\" %>s %b \"%{referer}i\" \"%{user-agent}i\"" combined
customlog "${srvroot}/logs/access.log" combined日志文件默认在:
c:\apache24\logs\
将 url中的/static 映射到 d 盘某目录:
alias "/static" "d:/web/static"
<directory "d:/web/static">
require all granted
</directory>scriptalias "cgi-bin" "${srvroot}/cgi-bin/"
<directory "${srvroot}/cgi-bin/">
require all granted
</directory>directoryindex index.html index.php index.htm
loadmodule rewrite_module modules/mod_rewrite.so
1)http 强制跳转 https
rewriteengine on
rewritecond %{https} off
rewriterule ^(.*)$ https://%{http_host}$1 [r=301,l]注解:
rewriteengine on
mod_rewrite 重写引擎。rewritecond %{https} offoff(即非 https,也就是 http)。rewriterule ^(.*)$ https://%{http_host}$1 [r=301,l]^(.*)$:这是一个正则表达式,用于匹配用户请求的整个路径(uri)。(.*) 会把匹配到的内容(比如 /index.html 或 /about)保存为变量 $1。rewriterule 规则中,它只负责处理“路径”,不负责处理“域名”。域名(如 www.example.com)是由 apache 的其他部分(虚拟主机配置)处理的,当请求到达 rewriterule 时,域名已经被“剥离”掉了,只剩下路径。https://%{http_host}$1:这是跳转的目标地址。https://:强制使用加密协议。%{http_host}:自动获取当前访问的域名(如 example.com)。$1:填入刚才匹配到的路径部分。[r=301,l]:这是规则的标志(flags)。r=301:表示执行301 永久重定向。这会告诉浏览器和搜索引擎(如 google、百度),该页面已永久移动到新的安全地址,有利于 seo。l:表示这是最后一条(last)规则。一旦匹配成功并执行了跳转,服务器就不再处理后面的其他规则了。补充:
在 http 协议中,一个完整的 url 是这样的:http://www.example.com:80/news/123.html
apache 在内部处理时,会把它拆分成不同的变量:
httpwww.example.com(对应变量 %{http_host})80/news/123.html(这就是 rewriterule 匹配的对象)当你写 rewriterule ^(.*)$ ... 时,正则表达式 ^(.*)$ 匹配的对象仅仅是 uri/路径 部分。
既然 ^(.*)$ 抓不到域名,那为什么我们在跳转目标里又要写 https://%{http_host}$1 呢?
^(.*)$:负责抓取路径(比如 /news/123.html),并存为 $1。%{http_host}:这是一个系统变量。它的作用是告诉 apache:“去查一下刚才那个请求的域名是什么,把它填在这里”。假设你的域名是 example.com:
http://example.com/news/article1rewritecond 检测到是 http (off)。rewriterule 捕获路径 /news/article1 为 $1。https://example.com/news/article1。2)伪静态 /path → index.php?path=
rewritecond %{request_filename} !-f
rewritecond %{request_filename} !-d
rewriterule ^(.*)$ index.php/$1 [l]3)支持 .htaccess
是 apache web 服务器 的一种分布式配置文件。它允许你在不修改服务器主配置文件(如 httpd.conf)的情况下,直接控制该目录及其子目录下的网站行为。它在虚拟主机(共享主机)环境中非常流行,因为普通用户通常没有权限修改服务器的主配置,但可以通过上传 .htaccess 文件来实现各种高级功能。
.htaccess 的功能非常强大,最常见的用途包括:
mod_rewrite 模块,它可以把复杂的动态 url(如 product.php?id=123)转换成简洁、对搜索引擎友好的静态 url(如 product/123),或者实现 http 强制跳转 https。errordocument 404 /errors/404.htmlindex.html),默认情况下 apache 会列出该文件夹下的所有文件。使用 .htaccess 可以禁止这种行为,防止文件结构泄露。options -indexes<directory "${srvroot}/htdocs">
allowoverride all
</directory>#include conf/extra/这个目录里是放什么的conf/extra/ 目录是 apache 用来存放特定功能的扩展配置文件的地方。
简单来说,apache 的主配置文件 httpd.conf 就像一个总指挥部,而 conf/extra/ 目录就像是各个专项部门(如安保部、交通部、后勤部)的独立办公室。为了保持主配置文件的整洁和易读,apache 将一些复杂或特定功能的配置单独拆分出来,放在这个目录下。主配置文件通过 include 指令来调用它们。
涉及conf/extra/ 目录,是 apache 用来存放特定功能的扩展配置文件的地方。为了保持主配置文件的整洁和易读,apache 将一些复杂或特定功能的配置单独拆分出来,放在这个目录下。主配置文件通过 include 指令来调用它们。
在 httpd.conf 中取消注释:
include conf/extra/httpd-vhosts.conf
<virtualhost *:80>
servername www.site1.com
documentroot "c:/web/site1"
errorlog "logs/site1-error.log"
customlog "logs/site1-access.log" combined
</virtualhost>
<virtualhost *:80>
servername www.site2.com
documentroot "c:/web/site2"
errorlog "logs/site2-error.log"
customlog "logs/site2-access.log" combined
</virtualhost>windows 本地测试需修改:
c:\windows\system32\drivers\etc\hosts
添加
127.0.0.1 www.site1.com 127.0.0.1 www.site2.com
loadmodule ssl_module modules/mod_ssl.so loadmodule socache_shmcb_module modules/mod_socache_shmcb.so include conf/extra/httpd-ssl.conf
<virtualhost *:443>
servername site.com
documentroot "c:/web/site"
sslengine on
sslcertificatefile "c:/ssl/site.crt"
sslcertificatekeyfile "c:/ssl/site.key"
sslcertificatechainfile "c:/ssl/chain.crt"
# 安全套件
sslprotocol all -sslv3 -tlsv1 -tlsv1.1
sslciphersuite high:!anull:!md5
</virtualhost>loadmodule cache_module modules/mod_cache.so loadmodule cache_disk_module modules/mod_cache_disk.so cacheenable disk / cacheroot "c:/apache24/cache" cachedirlevels 2 cachedirlength 1 cachedefaultexpire 3600
<filesmatch "\.(jpg|png|css|js|gif|ico)$">
header set cache-control "max-age=86400"
</filesmatch>需加载头模块
loadmodule headers_module modules/mod_headers.so
根据浏览器语言自动返回对应页面:
loadmodule negotiation_module modules/mod_negotiation.so
<directory "${srvroot}/htdocs">
options +multiviews
</directory>可放置:
apache 会自动匹配。
windows 下所有扩展均为 dso 模式,通过 loadmodule 加载:
loadmodule deflate_module modules/mod_deflate.so loadmodule expires_module modules/mod_expires.so loadmodule rewrite_module modules/mod_rewrite.so
无需编译,直接启用即可。
文件:conf/extra/httpd-mpm.conf
<ifmodule mpm_winnt_module>
threadsperchild 150
maxconnectionsperchild 0
</ifmodule>优化建议:
threadsperchild 250~500threadsperchild 80~120解释代码
2. threadsperchild 150
150,意味着你的服务器同一时间最多可以处理 150 个 并发连接。3. maxconnectionsperchild 0
0(当前配置):表示不限制。子进程会一直运行,永远不会因为处理了太多请求而自动重启。10000):表示当一个子进程累计处理了 10000 个请求后,apache 会“杀掉”这个旧进程,并重新生成一个新的子进程来代替它。总结与建议
这段配置的意思是:
“在 windows 系统下,apache 启动一个子进程,该进程同时开启 150 个线程来处理请求。并且,这个进程将永久运行,除非手动重启,不会因为处理了过多请求而自动重置。”
apache的windows版本,它通常只启动一个子进程,与linux/unix系统下的apache有本质的区别。linux下apache启动父进程来管理多个子进程,window的apache的设计机制主进程负责管理服务和绑定端口,单个子进程(主进程启动唯一的子进程。多线程:这个子进程内部创建大量的线程)。
优化建议:
maxconnectionsperchild 设置为一个较大的数字(如 10000 或 20000),以防止潜在的内存泄漏导致服务器变慢。threadsperchild 的值。hostnamelookups off enablesendfile on keepalive on keepalivetimeout 3 timeout 10
启用 gzip 压缩:
addoutputfilterbytype deflate text/html text/plain text/css application/json
servertokens prod serversignature off
<directory "${srvroot}/cgi-bin">
require all granted
</directory>
# 默认拒绝所有
<directory />
require all denied
allowoverride none
</directory>limitrequestbody 10485760
options -indexes
你直接复制粘贴到 httpd.conf 末尾即可:
# ==============================
# 安全加固配置 security tips
# ==============================
# 隐藏版本号
servertokens prod
serversignature off
# 禁止根目录访问,提高安全性
<directory />
require all denied
allowoverride none
options none
</directory>
# 禁止目录浏览
options -indexes
# 限制请求体大小(10mb)
limitrequestbody 10485760
# 禁止访问 .htaccess、.env 等敏感文件
<filesmatch "\.(htaccess|htpasswd|ini|phps|sql|bak|env|log)$">
require all denied
</filesmatch>
# 关闭反向dns查询,提升速度+安全
hostnamelookups off
# 禁用符号链接(如不需要可开启)
# options -followsymlinks管理员 cmd:
httpd -t
检查无误后重启(cmd):
httpd -k restart
虚拟主机里的安全规则写在:
c:\apache24\conf\extra\httpd-vhosts.conf
每个 <virtualhost> 内部都可以单独加权限控制。
设置环境变量供脚本使用:
setenv app_env production setenv path "c:/php"
netstat -ano | findstr ":80"
httpd -k restart到此这篇关于windows系统下apache http server 2.4安装与配置、运维手册的文章就介绍到这了,更多相关windows apache2.4安装配置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论