安装和配置Apache服务器

一、Windows 系统(最简单,推荐新手)

1. 下载安装(用集成包,1 分钟搞定)

推荐:XAMPP(自带 Apache + MySQL + PHP)

下载地址:https://www.apachefriends.org/

安装步骤:
  1. 一路下一步,安装到 D:\xampp 之类的路径
  2. 打开 XAMPP Control Panel
  3. 找到 Apache,点 Start
  4. 出现绿色标记 = 启动成功

2. 测试是否正常

浏览器打开:
plaintext
http://localhost
看到 XAMPP 欢迎页面 = 安装成功!

3. 网站根目录(放网页的地方)

plaintext
D:\xampp\htdocs
把你的 index.html 放这里,就能直接访问。

二、Linux 系统(CentOS / Ubuntu 通用)

1. 安装 Apache

CentOS / RHEL

bash
运行
yum install httpd -y

Ubuntu / Debian

bash
运行
apt update
apt install apache2 -y

2. 启动并设置开机自启

CentOS

bash
运行
systemctl start httpd
systemctl enable httpd

Ubuntu

bash
运行
systemctl start apache2
systemctl enable apache2

3. 开放防火墙 80 端口

CentOS

bash
运行
firewall-cmd --permanent --add-service=http
firewall-cmd --reload

Ubuntu

bash
运行
ufw allow 'Apache'

4. 测试

浏览器输入服务器 IP,例如:
plaintext
http://192.168.1.100
看到 Apache 默认页面 = 成功!

三、最核心配置(必须会)

1. 配置文件位置

Windows(XAMPP)

plaintext
xampp/apache/conf/httpd.conf

Linux

plaintext
/etc/httpd/conf/httpd.conf  # CentOS
/etc/apache2/apache2.conf  # Ubuntu

2. 最重要的 3 个配置

① 网站根目录(你网页放哪)

apache
DocumentRoot "D:/xampp/htdocs"
<Directory "D:/xampp/htdocs">
    AllowOverride All
    Require all granted
</Directory>

② 监听端口(默认 80)

apache
Listen 80

③ 默认首页文件

apache
DirectoryIndex index.html index.php

3. 修改配置后必须重启

Windows

XAMPP 里点 Apache → StopStart

Linux

bash
运行
systemctl restart httpd   # CentOS
systemctl restart apache2 # Ubuntu

四、搭建一个最简单的网站(测试用)

  1. 进入网站根目录
  2. 新建 index.html
html
预览
<h1>我的第一个 Apache 网站</h1>
<p>Apache 安装成功!</p>
  1. 访问 http://localhost 即可看到

五、常见问题(90% 的人都会遇到)

1. Apache 启动失败

原因:80 端口被占用(迅雷、VMware、IIS、nginx)
解决:
  • 关闭占用软件
  • 或修改 Apache 端口为 8080
apache
Listen 8080
访问变成:http://localhost:8080

2. 403 Forbidden 无权限

确保配置里有:
apache
Require all granted

3. 404 Not Found

文件没放在正确的根目录。

 

上一篇 有了 IP 地址,为什么还要用 MAC 地址?
下一篇 浪潮服务器统下重置BMC密码方法Windows