CentOS7 + Apache 2.4配置VirtualHost后,访问报403错误

环境

CentOS 7
Apache 2.4

问题现象:

安装Apache, 配置virtualhost后,访问页面一直报403 Forbidden错误.

You don’t have permission to access / on this server

解决方案:

经过各种baidu,google后发现,这个问题很多人遇到,解决方案也各有不同,我猜想可能跟CentOS或者Apache版本有关。个人总结主要有两方面检查:

  1. 首先检查文件访问权限,确保virtualhost配置的路径有访问权限
  2. 如果访问权限没问题,检查SELinux配置(我遇到的就是这个问题)

关于文件权限,应该都会检查修改。对于SELinux的修改,方法如下:

1. 临时修改: setenforce 0
通过sestatus查看如果Current modePermissive说明修改成功
2. 永久修改:
vi /etc/sysconfig/selinux
配置SELinux=disabled
3. 脚本修改:
sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
对于方法2,3修改需要重启主机才能生效

详情请参考如下链接修改

吐槽

之前一直在用Ubuntu,最新在整CentOS,相比于Ubuntu,CentOS搭建LAMP环境遇到了太多个坑,各种不便。。。

附录 CentOS Apache VirtualHost配置

vi /etc/httpd/conf.d/vhost.conf
添加如下内容,ServerNameDocumentRoot等按照个人实际情况修改


    ServerName  yii2-app.test
    DocumentRoot /vagrant/www/web/
    
        DirectoryIndex index.html index.php
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    
    ErrorLog /var/log/httpd/${HOSTNAME}_error.log
    LogLevel warn
    CustomLog /var/log/httpd/${HOSTNAME}_access.log combined

参考

https://www.tecmint.com/disable-selinux-temporarily-permanently-in-centos-rhel-fedora/
http://sillyadmin.blogspot.co.nz/2013/04/quickly-disable-selinux-in-setup-script.html
https://www.vultr.com/docs/disabling-selinux-on-centos-7

你可能感兴趣的:(CentOS7 + Apache 2.4配置VirtualHost后,访问报403错误)