改 Apache 的根目录(DocumentRoot),常见做法如下(以 Linux 为例,Windows 类似):
常见位置:
Ubuntu / Debian
/etc/apache2/sites-enabled/000-default.conf
或
/etc/apache2/apache2.conf
CentOS / RHEL / Rocky / Alma
/etc/httpd/conf/httpd.conf
Windows(XAMPP / WAMP)
C:\xampp\apache\conf\httpd.conf
DocumentRoot找到类似内容:
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
改成你的新目录,例如:
DocumentRoot "/home/user/web"
<Directory "/home/user/web">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
⚠️ 注意:
DocumentRoot 和 <Directory> 要一起改chmod -R 755 /home/user/web
chown -R www-data:www-data /home/user/web
www-data是 Apache 在 Ubuntu/Debian 下的运行用户
CentOS 通常是apache
编辑虚拟主机配置:
sudo nano /etc/apache2/sites-available/my-site.conf
示例:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /home/user/web
<Directory /home/user/web>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
启用站点(Ubuntu):
sudo a2ensite my-site.conf
# Ubuntu / Debian
sudo systemctl restart apache2
# CentOS / RHEL
sudo systemctl restart httpd
<Directory> 未设置 Require all grantedsetenforce 0
或正确设置:
chcon -Rt httpd_sys_content_t /home/user/web
DocumentRoot "/new/path"
<Directory "/new/path">
AllowOverride All
Require all granted
</Directory>
然后:
systemctl restart apache2
如果你告诉我:
我可以给你完全对应的配置示例 ✅