You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ubuntu系统的EC2服务器中配置public_html目录?

Ubuntu EC2 配置 Apache 用 public_html 替代默认根目录

首先纠正个关键误区:Ubuntu 上的 Apache 配置文件路径不是你查到的 /etc/httpd/conf/httpd.conf——那是 CentOS/RHEL 系的路径,Ubuntu 的 Apache 配置都在 /etc/apache2/ 目录下。

下面是具体步骤:

  • 编辑默认站点配置文件
    Ubuntu 默认的站点配置存放在 /etc/apache2/sites-available/000-default.conf,用新手友好的 nano 编辑器打开:

    sudo nano /etc/apache2/sites-available/000-default.conf
    
  • 修改根目录和权限规则
    找到 DocumentRoot 行,把默认的 /var/www/html 替换成你的 public_html 实际路径(比如你的 EC2 用户名是 ubuntu,路径就是 /home/ubuntu/public_html):

    DocumentRoot /home/ubuntu/public_html
    

    接着找到对应的 <Directory> 区块(默认是 <Directory /var/www/html/>),把路径同步修改,同时确保权限规则允许访问:

    <Directory /home/ubuntu/public_html/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    
  • 修复父目录权限
    就算你的 public_html 权限设为 705,父目录(比如 /home/ubuntu)必须至少有 711 权限,否则 Apache 进程无法进入该目录:

    chmod 711 /home/ubuntu
    
  • 重启 Apache 生效
    修改完成后重启服务:

    sudo systemctl restart apache2
    
  • PHP 支持(如果需要)
    如果你的项目包含 PHP 文件,先安装 Apache 的 PHP 模块:

    sudo apt install libapache2-mod-php
    sudo systemctl restart apache2
    

内容的提问来源于stack exchange,提问作者knowledge of no knowledge

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 04:45:57