在 Ubuntu 下使用 Apache2 时,“目录权限”通常分为 文件系统权限 和 Apache 配置权限 两部分。下面按常见场景说明。
Apache 通常以用户 www-data 运行。
sudo chown -R www-data:www-data /var/www/html
# 目录 755
sudo find /var/www/html -type d -exec chmod 755 {} \;
# 文件 644
sudo find /var/www/html -type f -exec chmod 644 {} \;
如果网站需要写文件(如上传目录):
sudo chown -R www-data:www-data /var/www/html/uploads
sudo chmod -R 775 /var/www/html/uploads
编辑站点配置:
sudo nano /etc/apache2/sites-available/000-default.conf
示例:
<VirtualHost *:80>
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Require all granted:允许所有人访问AllowOverride All:允许 .htaccess 覆盖配置Options -Indexes:禁止目录列表(更安全)Options Indexes:允许目录浏览<Directory /var/www/html>
Options -Indexes
Require all granted
</Directory>
<Directory /var/www/html/admin>
Require ip 192.168.1.100
</Directory>
AllowOverride All
并启用模块:
sudo a2enmod rewrite
sudo systemctl restart apache2
sudo systemctl restart apache2
Require 和文件权限.htaccesssudo tail -f /var/log/apache2/error.log
如果你能具体说明是 哪个目录 / 什么用途(如上传、API、后台),我可以给你更精确的配置。