在 Apache2(通常指 Linux 下的 Apache HTTP Server)里,“设权限”一般涉及 三块内容,你可以根据实际需求来看:
Apache 运行用户通常是:
www-dataapache# 目录
chmod 755 /var/www/html
# 文件
chmod 644 /var/www/html/index.html
chown -R www-data:www-data /var/www/html
(CentOS 用 apache:apache)
在站点配置文件(如 /etc/apache2/sites-enabled/000-default.conf)中:
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Require all granted:允许所有人访问Require all denied:禁止访问Require ip 192.168.1.0/24:只允许某 IP 段AllowOverride All:允许 .htaccess 生效修改后重启:
sudo systemctl restart apache2
在网站目录下新建 .htaccess:
<Files secret.txt>
Require all denied
</Files>
AuthType Basic
AuthName "Restricted"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
生成密码文件:
htpasswd -c /etc/apache2/.htpasswd username
| 需求 | 做法 |
|---|---|
| 网站无法访问 403 | 检查目录权限 + Require |
| 上传目录不可执行 | php_admin_flag engine off |
| 只允许内网 | Require ip 10.0.0.0/8 |
| 禁止列目录 | 去掉 Indexes |
如果你能说清楚:
我可以给你具体配置示例。