温馨提示×

LAMP架构中Apache如何配置

小樊
36
2025-12-31 16:56:47
栏目: 编程语言

LAMP中Apache配置全流程

一 安装与基础准备

  • 在基于 Debian/Ubuntu 的系统上安装组件:
    • 更新索引并安装:sudo apt update && sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql
  • 在基于 CentOS/RHEL 的系统上安装组件:
    • 安装:sudo yum install httpd mariadb-server php php-mysqlnd
  • 启动并开机自启服务(Debian/Ubuntu):sudo systemctl start apache2 && sudo systemctl enable apache2
  • 启动并开机自启服务(CentOS/RHEL):sudo systemctl start httpd && sudo systemctl enable httpd
  • 安装完成后,浏览器访问服务器 IP 应看到 Apache 默认页面,表示 Web 服务已就绪。

二 核心配置步骤

  • 主配置文件路径
    • Debian/Ubuntu/etc/apache2/apache2.conf
    • CentOS/RHEL/etc/httpd/conf/httpd.conf
  • 常用指令与作用
    • ServerName:站点域名或 IP
    • DocumentRoot:网站文件根目录(如 /var/www/html
    • Listen:监听端口(默认 80
    • Directory:目录访问策略与权限控制
  • 启用 PHP 处理
    • Debian/Ubuntu:启用对应模块(示例按版本):sudo a2enmod php7.4sudo a2enmod php8.1;随后 sudo systemctl restart apache2
    • CentOS/RHEL:PHP 模块通常随安装就绪,必要时重启 httpd
  • 目录默认首页与字符集(示例)
    • 在站点或全局配置中加入:
      • DirectoryIndex index.php index.html
      • AddDefaultCharset UTF-8
  • 修改后检查语法并应用
    • 语法检查:sudo apache2ctl configtest(或 RHEL 上 httpd -t
    • 热重载:sudo systemctl reload apache2(或重启)。

三 虚拟主机配置

  • 创建站点配置(Debian/Ubuntu 推荐方式)
    • 新建:sudo nano /etc/apache2/sites-available/example.com.conf
    • 示例内容(按需替换域名与路径):
      <VirtualHost *:80>
          ServerAdmin webmaster@example.com
          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 ${APACHE_LOG_DIR}/error.log
          CustomLog ${APACHE_LOG_DIR}/access.log combined
      </VirtualHost>
      
    • 启用站点:sudo a2ensite example.com.conf;如存在默认站点可 sudo a2dissite 000-default.conf
    • 重载:sudo systemctl reload apache2
  • RHEL/CentOS 常用做法
    • /etc/httpd/conf/httpd.conf/etc/httpd/conf.d/*.conf 中直接放置与上述类似的 配置,然后 sudo systemctl reload httpd
  • 测试
    • 本地 hosts 或 DNS 将域名指向服务器后,访问 http://example.com 验证站点是否生效。

四 安全与优化要点

  • 隐藏版本信息:在主配置设置 ServerSignature OffServerTokens Prod,减少信息泄露
  • 禁用目录列表:在不需要的目录使用 Options -Indexes
  • 运行身份与目录权限
    • 以非特权用户运行(如 www-data/apache),并确保 ServerRoot、conf、logs 等目录仅 root 可写
    • 站点目录常用权限:chown -R www-data:www-data /var/www/example.comchmod -R 755 /var/www/example.com
  • 模块管理
    • 禁用不需要的模块(如 mod_info、mod_userdir 等),降低攻击面
  • 请求与防护
    • 限制请求体大小:LimitRequestBody 512000(示例 500KB)
    • 可结合 mod_security、mod_evasive 做 WAF/抗 DoS 加固
  • 日志与监控
    • 确保访问与错误日志正常输出并定期审计:/var/log/apache2/error.log/var/log/apache2/access.log(Debian/Ubuntu),或 /var/log/httpd/error_log/var/log/httpd/access_log(RHEL/CentOS)
  • 持续更新
    • 及时更新 Apache 与操作系统补丁,关注安全公告与修复版本。

五 常见问题排查

  • 配置语法错误:执行 apache2ctl configtesthttpd -t,按提示修复
  • 403 Forbidden:检查 Require all granted、目录属主与权限(如 www-data:www-data755
  • 端口冲突:确认 80/443 未被其他进程占用(如 netstat -tulpen | grep ‘:80|:443’
  • 站点未生效:确认已启用站点(Debian/Ubuntu:a2ensite),并 reload;检查 ServerNameDNS/hosts 是否匹配
  • PHP 不解析:确认已启用对应 PHP 模块 并重启 Apache;必要时检查 libapache2-mod-php 是否安装。

0