目录
- 前提条件:准备 CentOS 系统
- 第一步:安装 Apache HTTP 服务器
- 第二步:启动并设置开机自启
- 第三步:配置防火墙
- 第四步:验证基本安装
- 第五步:理解核心配置文件
- 第六步:部署第一个网站(虚拟主机)
- 第七步:优化与安全加固
- 第八步:常用管理命令
- 常见问题与故障排查
前提条件:准备 CentOS 系统
在开始之前,请确保您:

- 已经拥有一台安装好的 CentOS 7 或 CentOS 8/Stream 系统。
- 拥有
root权限或一个具有sudo权限的用户账户。 - 系统已更新到最新的软件包:
sudo dnf update -y(CentOS 8/Stream) 或sudo yum update -y(CentOS 7)。
第一步:安装 Apache HTTP 服务器
Apache 在 CentOS 的官方软件仓库中都有提供,使用包管理器可以轻松安装。
对于 CentOS 8 / CentOS Stream:
# 使用 dnf 包管理器 sudo dnf install httpd -y
对于 CentOS 7:
# 使用 yum 包管理器 sudo yum install httpd -y
安装完成后,Apache 的主程序文件位于 /usr/sbin/httpd,配置文件在 /etc/httpd/ 目录下。

第二步:启动并设置开机自启
安装完成后,需要启动 Apache 服务并设置其在系统重启后自动运行。
启动服务:
# 对于 CentOS 8/Stream 和 CentOS 7 (使用 systemd) sudo systemctl start httpd
设置开机自启:
sudo systemctl enable httpd
检查服务状态:

sudo systemctl status httpd
如果看到绿色的 active (running) 字样,说明服务已成功启动。
第三步:配置防火墙
默认情况下,CentOS 的防火墙(firewalld)会阻止外部访问 HTTP (80) 和 HTTPS (443) 端口,您需要放行这些端口。
永久放行 HTTP (80) 端口:
sudo firewall-cmd --permanent --add-service=http
永久放行 HTTPS (443) 端口:
sudo firewall-cmd --permanent --add-service=https
重新加载防火墙以应用新规则:
sudo firewall-cmd --reload
防火墙已经允许外部流量访问您的 Apache 服务器了。
第四步:验证基本安装
打开您的 Web 浏览器,访问服务器的 IP 地址或域名。
http://<您的服务器IP地址>
http://192.168.1.100
如果一切正常,您将看到 Apache 的默认测试页面,上面写着 "Testing 123..." 或 "Apache 2 Test Page",这表明您的 Apache 服务器已经成功运行并可以对外提供服务。
第五步:理解核心配置文件
Apache 的配置非常灵活,但理解其文件结构至关重要。
-
主配置文件:
/etc/httpd/conf/httpd.conf这是 Apache 的核心配置文件,您可以在这里修改全局设置,如监听端口、运行用户、服务器根目录等,在大多数情况下,除非您有特殊需求,否则不需要直接修改这个文件。 -
模块配置目录:
/etc/httpd/conf.d/这个目录用于存放各个模块的独立配置文件,任何以.conf结尾的文件都会在主配置文件加载后被自动加载。强烈建议将您所有的自定义配置都放在这个目录下的新文件中,而不是去修改httpd.conf。 -
网站根目录:
/var/www/html/这是 Apache 默认存放网站文件的地方,如果您没有配置虚拟主机,访问服务器 IP 时显示的就是这个目录下的内容。 -
日志目录:
/var/log/httpd/access_log: 记录了所有客户端的访问请求。error_log: 记录了服务器运行时产生的错误和警告信息,排查问题时,这个文件是您的首选。
第六步:部署第一个网站(虚拟主机)
虚拟主机允许您在一台服务器上托管多个网站,我们以部署两个网站 example.com 和 test.com 为例。
为每个网站创建目录
# 创建网站根目录 sudo mkdir -p /var/www/example.com sudo mkdir -p /var/www/test.com # 设置目录所有者为 apache 用户,并赋予正确的权限 sudo chown -R apache:apache /var/www sudo chmod -R 755 /var/www
创建测试页面
为每个网站创建一个 index.html 文件,以便区分。
# example.com 的首页 echo "<h1>Welcome to example.com</h1>" | sudo tee /var/www/example.com/index.html # test.com 的首页 echo "<h1>Welcome to test.com</h1>" | sudo tee /var/www/test.com/index.html
创建虚拟主机配置文件
在 /etc/httpd/conf.d/ 目录下创建一个新的配置文件,my_vhosts.conf。
sudo vim /etc/httpd/conf.d/my_vhosts.conf
粘贴到文件中,这个配置文件定义了两个虚拟主机。
# 虚拟主机配置文件
# 第一个网站: example.com
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/example.com_error.log
CustomLog /var/log/httpd/example.com_access.log combined
</VirtualHost>
# 第二个网站: test.com
<VirtualHost *:80>
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com
<Directory /var/www/test.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/test.com_error.log
CustomLog /var/log/httpd/test.com_access.log combined
</VirtualHost>
配置解释:
<VirtualHost *:80>: 定义一个监听所有网络接口 80 端口的虚拟主机。ServerName: 您的域名。DocumentRoot: 该网站文件存放的根目录。<Directory ...>: 设置目录权限。AllowOverride All允许使用.htaccess文件。ErrorLog/CustomLog: 为每个网站设置独立的日志文件,方便管理。
检查配置并重启 Apache
在重启服务前,务必检查配置文件是否有语法错误。
# 检查配置语法 sudo apachectl configtest
如果输出 Syntax OK,则说明配置正确,然后重启 Apache 使新配置生效。
sudo systemctl restart httpd
配置本地 hosts 文件进行测试 (可选)
如果您还没有为这两个域名购买 DNS 解析,可以在您本机的 hosts 文件(Windows: C:\Windows\System32\drivers\etc\hosts, macOS/Linux: /etc/hosts)中添加以下条目,将域名指向您的服务器 IP。
<您的服务器IP地址> example.com
<您的服务器IP地址> test.com
在浏览器中访问 http://example.com 和 http://test.com,您应该能看到各自不同的欢迎页面了。
第七步:优化与安全加固
一个生产环境的服务器需要进行安全加固。
禁用目录列表
默认情况下,如果一个目录下没有 index.html,Apache 会列出该目录的所有文件,这会暴露服务器结构,带来安全隐患,可以在主配置文件或虚拟主机配置中禁用。
# 在 <Directory> 指令中添加 Options -Indexes
隐藏 Apache 版本信息
编辑主配置文件 /etc/httpd/conf/httpd.conf,找到并修改或取消注释下面这行:
ServerTokens Prod ServerSignature Off
ServerTokens Prod: 让服务器响应头中的 Server 字段只显示Apache,不显示版本号和操作系统信息。ServerSignature Off: 禁用在服务器生成的页面(如错误页、目录列表页)底部显示 Apache 版本信息。
使用 .htaccess 文件进行安全控制
在您的网站根目录下创建 .htaccess 文件,可以实现更细粒度的控制。
示例:禁止访问 .htaccess 文件本身
<Files ".htaccess">
Require all denied
</Files>
示例:禁止访问所有以 .log 结尾的文件
<FilesMatch "\.log$">
Require all denied
</FilesMatch>
启用 SELinux (CentOS 默认启用)
SELinux 是一个强大的安全模块,Apache 无法正常访问文件,很可能是 SELinux 策略的问题,可以使用以下命令为网站目录设置正确的 SELinux 上下文:
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www(/.*)?" sudo restorecon -Rv /var/www
第八步:常用管理命令
- 启动服务:
sudo systemctl start httpd - 停止服务:
sudo systemctl stop httpd - 重启服务 (应用新配置):
sudo systemctl restart httpd - 重新加载配置 (不中断连接):
sudo systemctl reload httpd - 查看服务状态:
sudo systemctl status httpd - 查看配置语法错误:
sudo apachectl configtest - 查看错误日志:
sudo tail -f /var/log/httpd/error_log - 查看访问日志:
sudo tail -f /var/log/httpd/access_log
常见问题与故障排查
-
问题:
Permission denied错误- 原因: 文件或目录的权限不正确,或者 SELinux 阻止了 Apache 的访问。
- 排查:
- 使用
ls -l /var/www/检查所有者和权限,确保是apache:apache且权限为755。 - 使用
sudo ls -Z /var/www/检查 SELinux 上下文,应该是unconfined_u:object_r:httpd_sys_content_t:s0。 - 如果上下文不对,使用
restorecon命令修复。
- 使用
-
问题:
403 Forbidden错误- 原因: 通常是
DocumentRoot目录权限问题,或者在<Directory>指令中设置了Require all denied。 - 排查:
- 确认
DocumentRoot目录的Read和Execute权限已开放给apache用户。 - 检查虚拟主机配置文件中的
<Directory>指令,确保Require all granted存在。
- 确认
- 原因: 通常是
-
问题:
404 Not Found错误- 原因: 请求的文件或目录在
DocumentRoot中不存在。 - 排查:
- 确认
DocumentRoot路径是否正确。 - 确认您访问的 URL 是否与文件路径匹配。
- 确认
- 原因: 请求的文件或目录在
-
问题:服务无法启动
- 原因: 通常是配置文件有语法错误。
- 排查: 立即运行
sudo apachectl configtest,它会告诉您哪一行配置有误。
希望这份详细的指南能帮助您在 CentOS 上成功搭建和管理 Apache HTTP 服务器!
