以下是在CentOS上設置Apache權限的指南:
創建www
用戶和組(若不存在):
sudo groupadd www
sudo useradd -g www -d /var/www -s /usr/sbin/nologin www
將網站目錄所有者設為www
用戶和組,如網站根目錄為/var/www/html
:
sudo chown -R www:www /var/www/html
設置目錄權限為755,文件權限為640:
sudo chmod -R 755 /var/www/html
sudo find /var/www/html -type f -exec chmod 640 {} \;
編輯/etc/httpd/conf/httpd.conf
,設置User
和Group
為www
:
User www
Group www
配置<Directory>
指令,允許訪問網站目錄:
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
臨時關閉SELinux:
sudo setenforce 0
或永久更改策略,允許Apache訪問特定目錄:
sudo yum install -y policycoreutils-python
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/html(/.*)?"
sudo restorecon -Rv /var/www/html
允許HTTP(端口80)和HTTPS(端口443)流量:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
完成設置后,重啟Apache服務:
sudo systemctl restart httpd