首页主机资讯Apache2在Ubuntu上的配置教程

Apache2在Ubuntu上的配置教程

时间2025-12-01 11:28:04发布访客分类主机资讯浏览807
导读:Ubuntu 上 Apache2 配置教程 一 安装与启动 更新软件包索引并安装 Apache2: 命令:sudo apt update && sudo apt install apache2 -y 启动服务并设置开...

Ubuntu 上 Apache2 配置教程

一 安装与启动

  • 更新软件包索引并安装 Apache2:
    • 命令:sudo apt update & & sudo apt install apache2 -y
  • 启动服务并设置开机自启:
    • 命令:sudo systemctl start apache2sudo systemctl enable apache2
  • 检查运行状态:
    • 命令:sudo systemctl status apache2(看到 Active: active (running) 即正常)
  • 防火墙放行(如使用 UFW):
    • 命令:sudo ufw allow 'Apache Full'(同时放行 80/443

二 目录结构与配置文件

  • 主要目录与文件
    • 配置根目录:/etc/apache2/
    • 主配置文件:/etc/apache2/apache2.conf
    • 可用站点:/etc/apache2/sites-available/
    • 已启用站点:/etc/apache2/sites-enabled/
    • 模块管理:/etc/apache2/mods-available//etc/apache2/mods-enabled/
    • 日志文件:/var/log/apache2/error.log/var/log/apache2/access.log
  • 常用管理命令
    • 启用站点:sudo a2ensite yourdomain.conf
    • 禁用站点:sudo a2dissite yourdomain.conf
    • 启用模块:sudo a2enmod module_name(如 rewritessl
    • 禁用模块:`sudo a2dismod module_name**
    • 检查配置语法:sudo apache2ctl configtest
    • 重新加载配置:sudo systemctl reload apache2(或重启:sudo systemctl restart apache2

三 配置虚拟主机

  • 准备站点目录与权限(示例域名:example.com
    • 创建目录:sudo mkdir -p /var/www/example.com
    • 写入测试页:echo "< h1> Hello example.com< /h1> " | sudo tee /var/www/example.com/index.html
    • 设置权限(推荐由 www-data 运行):sudo chown -R www-data:www-data /var/www/example.com & & sudo chmod -R 755 /var/www/example.com
  • 创建站点配置
    • 新建文件: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}
      /example_error.log
          CustomLog ${
      APACHE_LOG_DIR}
          /example_access.log combined
      <
          /VirtualHost>
          
      
  • 启用站点与可选禁用默认站点
    • 启用:sudo a2ensite example.com.conf
    • 禁用默认:sudo a2dissite 000-default.conf
  • 使配置生效
    • 检查语法:sudo apache2ctl configtest
    • 重载:sudo systemctl reload apache2
  • 访问测试
    • 浏览器访问:http://example.comhttp://服务器IP
  • 提示
    • 若使用 NameVirtualHost,确保域名已解析到服务器 IP(DNS A 记录)

四 启用 HTTPS 与自动续期

  • 安装 Certbot 与 Apache 插件
    • 命令:sudo apt install certbot python3-certbot-apache -y
  • 获取并自动配置证书(同时处理 HTTP→HTTPS 重定向)
    • 命令:sudo certbot --apache -d example.com -d www.example.com
  • 验证与续期
    • 浏览器访问:https://example.com(应显示安全锁)
    • 测试自动续期:sudo certbot renew --dry-run
  • 说明
    • 证书默认位于 /etc/letsencrypt/live/,Certbot 会自动修改虚拟主机并启用 SSL/TLS

五 常见问题与排查

  • 403 Forbidden
    • 检查目录权限与所有权(如:www-data:www-data755
    • 确认 < Directory> 中有 Require all granted
  • 404 Not Found
    • 确认 DocumentRoot 路径正确且存在 index.html
  • 域名访问异常
    • 核对 DNS A 记录是否指向服务器 IP
    • 检查是否禁用了默认站点导致冲突
  • 配置不生效
    • 运行 sudo apache2ctl configtest 检查语法
    • 使用 sudo systemctl reload apache2 重载配置
  • 查看日志定位问题
    • 错误日志:sudo tail -f /var/log/apache2/error.log
    • 访问日志:sudo tail -f /var/log/apache2/access.log
  • 端口被占用
    • 检查占用:sudo ss -tulpen | grep ':80\|:443'
    • 释放端口或调整服务配置后重启 Apache

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Apache2在Ubuntu上的配置教程
本文地址: https://pptw.com/jishu/759920.html
如何在Linux上卸载FTP Server Linux CPUInfo 优化技巧

游客 回复需填写必要信息