在 CentOS 上,Apache 通常是 httpd(不是 apache2,apache2 是 Debian/Ubuntu 的叫法)。下面从 目录权限、SELinux、防火墙、虚拟主机 几个方面说明如何“设置权限”。
# 启动/停止/开机自启
systemctl start httpd
systemctl enable httpd
systemctl status httpd
/var/www/html
# 假设你的用户是 www
chown -R apache:apache /var/www/html
chmod -R 755 /var/www/html
apache 是 CentOS 中 Apache 的运行用户755644find /var/www/html -type d -exec chmod 755 {} \;
find /var/www/html -type f -exec chmod 644 {} \;
getenforce
如果是 Enforcing,必须处理。
semanage fcontext -a -t httpd_sys_content_t "/var/www/html(/.*)?"
restorecon -Rv /var/www/html
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/uploads(/.*)?"
restorecon -Rv /var/www/html/uploads
# 允许 Apache 访问网络
setsebool -P httpd_can_network_connect on
# 允许访问用户目录
setsebool -P httpd_enable_homedirs on
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<Files ".env">
Require all denied
</Files>
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html/example
<Directory "/var/www/html/example">
Require all granted
</Directory>
</VirtualHost>
Execute 权限Require all granted 没加/var/log/httpd/error_log
/var/log/httpd/access_log
| Ubuntu | CentOS |
|---|---|
| apache2 | httpd |
| /var/www | /var/www/html |
| www-data | apache |
如果你能告诉我:
我可以给你更精确的配置。