PHPS 源文件 - 403 Forbidden You don't have permission to access this resource

PHPS source file - 403 Forbidden You don't have permission to access this resource

您好,我正在尝试查看 Ubuntu 20.04.3 Apache 2.4.41 PHP 7 服务器上的 .PHPS 源文件,但是当我查看 .phps 文件时我收到此消息的浏览器 -

403 Forbidden You don't have permission to access this resource.

如果我将文件类型重命名为 .txt,我可以在浏览器中正常查看文件。

我已将以下内容添加到 .htaccess -

<FilesMatch "\.phps$">
    SetHandler application/x-httpd-php-source
</FilesMatch>
        
RewriteRule (.*\.php)s$  [H=application/x-httpd-php-source]

我尝试将其添加到 /etc/apache2/sites-enabled/demo.testsite.com-le-ssl.conf 但没有成功。

然后我更新了这个文件 /etc/apache2/mods-available/php7.3.conf 并注释掉了 Require all denied 并添加了订单拒绝,允许拒绝所有订单 -

<FilesMatch ".+\.phps$">
    SetHandler application/x-httpd-php-source
    # Deny access to raw php sources by default
    # To re-enable it's recommended to enable access to the files
    # only in specific virtual host or directory
    #Require all denied
    Order Deny,Allow
    Deny from all
</FilesMatch>

然后我重新启动了 apache sudo service apache2 restart 我在查看 PHPS 文件时仍然在浏览器中收到此消息 -

403 Forbidden You don't have permission to access this resource.

有人知道如何修复吗?


更新 2022/05/15

出于某种原因,我再次收到 403 Forbidden。我唯一的猜测是因为我做了一些更新(sudo apt update 和 sudo apt upgrade)。当我再次检查此文件时 /etc/apache2/mods-available/php7.3.conf 设置显示 Require all denied 所以我将其改回 要求全部授予.

我重新启动了两个服务 sudo service apache2 restartsudo service php7.3-fpm restart 并且仍然 403 禁止.

当我在 apache2 文件夹中搜索 .phps 时,这些文件中出现了相同的结果 -

里面都有这个-

<FilesMatch ".+\.ph(ar|p|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch ".+\.phps$">
    SetHandler application/x-httpd-php-source
    Require all granted
</FilesMatch>

# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(ar|p|ps|tml)$">
    Require all denied
</FilesMatch>

# Running PHP scripts in user directories is disabled by default
#
# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
        php_admin_flag engine Off
    </Directory>
</IfModule>

我已经尝试从所有这些文件中删除它,但仍然无法正常工作 -

# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(ar|p|ps|tml)$">
    Require all denied
</FilesMatch>

知道如何解决这个问题吗?

Apache 2.4 已弃用 Allow,Deny 语法。替换

Order Deny,Allow
Deny from all

Require all granted

然后重启 Apache。

编辑添加: 即使您使用 Allow,Deny 语法,Deny from all 之后没有 Allow 语句也会,因为如您所料,拒绝对相关资源的所有访问。

参考资料