调试为什么我在 Apache 2.4 中收到“您没有访问权限”

Yah*_*din 6 apache hosts hosts-file vhosts apache2.4

我正在尝试在 Linux/Ubuntu 中创建本地环境。

我已经安装了 Apache 2.4.7(使用 apt-get)。

我已将 /etc/hosts 更改为:

127.0.0.1   example.dev
127.0.0.1   localhost
...
Run Code Online (Sandbox Code Playgroud)

我还在“/etc/apache2/sites-available”中添加了一个文件“example.dev.conf”,如下所示:

<VirtualHost *:80>
    ServerName example.dev
    DocumentRoot "/home/yahya/path/to/projec"
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory "/home/yahya/path/to/project">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Run Code Online (Sandbox Code Playgroud)

但是当我转到 example.dev 时,我收到以下消息:

第403章 禁止!您没有权限访问此服务器上的 /。

我还根据<Directory />此链接的建议编辑了 apache.conf 部分:禁止您无权访问此服务器上的 /错误消息“禁止您无权访问此服务器上的 /”

从:

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>
Run Code Online (Sandbox Code Playgroud)

<Directory />
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order deny,allow
    Require all granted
</Directory>
Run Code Online (Sandbox Code Playgroud)

我用过a2ensite。但还是不行。

Nek*_*Nek 3

即使解决方案可能不是这样,您也应该首先检查 apache 是否可以访问您的目录。这意味着您的文件夹应该具有“其他”的读取权限,或者了解如何在 Linux 上配置 acls。

$ ls -la /home/nek
# ...
drwxrwxr-x   6 nek  nek    4096 nov.  19 21:07 MyFolderOfDev
Run Code Online (Sandbox Code Playgroud)

重要的部分是权限的最后一部分:“rx”。如果您没有类似的东西,请使用以下命令:

$ chmod -R 755 MyFolderOfDev
Run Code Online (Sandbox Code Playgroud)

你原来的配置对我来说看起来不错。检查您是否与其他虚拟主机没有冲突。但这是我的配置,它运行得很好:

# Defining virtualhost
<VirtualHost *:80>
# This is not needed but seriously recommended
        ServerAdmin contact@moi.com
# This line is your host name (example.dev if you prefere)
        ServerName nekland
# You can add another server name using ServerAlias
    ServerAlias nekland.dev
# Path to your folder
    DocumentRoot /home/nek/Apache/SymfonyProject/web/
# Here are options neeeded to authorizations and I added some classical options
        <Directory /home/nek/Apache/SymfonyProject/web/>
                AllowOverride All
                Options -Indexes +FollowSymLinks +MultiViews
                Require all granted
        </Directory>
# For the error file, exactly like you did
        ErrorLog /var/log/apache2/nekland.err
        LogLevel warn
        CustomLog /var/log/apache2/nekland.log combined
# A little bonus that remove the server signature from http requests
        ServerSignature Off
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

当然不要忘记每次修改后重新加载apache。

$ sudo service apache2 reload
Run Code Online (Sandbox Code Playgroud)

如果没有任何效果...检查你的日志!这是找出您的请求为何有效的更好方法。