凌峰创科服务平台

CentOS httpd服务器如何配置与优化?

目录

  1. 前提条件:准备 CentOS 系统
  2. 第一步:安装 Apache HTTP 服务器
  3. 第二步:启动并设置开机自启
  4. 第三步:配置防火墙
  5. 第四步:验证基本安装
  6. 第五步:理解核心配置文件
  7. 第六步:部署第一个网站(虚拟主机)
  8. 第七步:优化与安全加固
  9. 第八步:常用管理命令
  10. 常见问题与故障排查

前提条件:准备 CentOS 系统

在开始之前,请确保您:

CentOS httpd服务器如何配置与优化?-图1
(图片来源网络,侵删)
  • 已经拥有一台安装好的 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/ 目录下。

CentOS httpd服务器如何配置与优化?-图2
(图片来源网络,侵删)

第二步:启动并设置开机自启

安装完成后,需要启动 Apache 服务并设置其在系统重启后自动运行。

启动服务:

# 对于 CentOS 8/Stream 和 CentOS 7 (使用 systemd)
sudo systemctl start httpd

设置开机自启:

sudo systemctl enable httpd

检查服务状态:

CentOS httpd服务器如何配置与优化?-图3
(图片来源网络,侵删)
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.comtest.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.comhttp://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

常见问题与故障排查

  1. 问题:Permission denied 错误

    • 原因: 文件或目录的权限不正确,或者 SELinux 阻止了 Apache 的访问。
    • 排查:
      1. 使用 ls -l /var/www/ 检查所有者和权限,确保是 apache:apache 且权限为 755
      2. 使用 sudo ls -Z /var/www/ 检查 SELinux 上下文,应该是 unconfined_u:object_r:httpd_sys_content_t:s0
      3. 如果上下文不对,使用 restorecon 命令修复。
  2. 问题:403 Forbidden 错误

    • 原因: 通常是 DocumentRoot 目录权限问题,或者在 <Directory> 指令中设置了 Require all denied
    • 排查:
      1. 确认 DocumentRoot 目录的 ReadExecute 权限已开放给 apache 用户。
      2. 检查虚拟主机配置文件中的 <Directory> 指令,确保 Require all granted 存在。
  3. 问题:404 Not Found 错误

    • 原因: 请求的文件或目录在 DocumentRoot 中不存在。
    • 排查:
      1. 确认 DocumentRoot 路径是否正确。
      2. 确认您访问的 URL 是否与文件路径匹配。
  4. 问题:服务无法启动

    • 原因: 通常是配置文件有语法错误。
    • 排查: 立即运行 sudo apachectl configtest,它会告诉您哪一行配置有误。

希望这份详细的指南能帮助您在 CentOS 上成功搭建和管理 Apache HTTP 服务器!

分享:
扫描分享到社交APP
上一篇
下一篇