Apache 错误 403 禁止请求 .wsgi 或 .py

Apache Error 403 Forbidden on Request to .wsgi or .py

我已经从源代码编译了最新的 mod_wsgi,安装了,但是脚本没有在浏览器中打开。

当我在浏览器中打开 localhost/python.wsgi 时,出现错误 403 'Forbidden'。不明白为什么。

脚本位于:/var/www/html/python.wsgi

模块 mod_wsgi 已安装并加载。 添加处理程序以进行扩展。 处理程序设置为 FilesMatch 的正则表达式。 该文件夹是可读的。 该文件是可读的。 为什么 'Forbidden' ?

apache2.conf 默认为:

DefaultRuntimeDir ${APACHE_RUN_DIR}
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

AccessFileName .htaccess

<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf

加载的配置列表:

/etc/apache2/conf-enabled$ ls

apache2-doc.conf
localized-error-pages.conf
security.conf
charset.conf
other-vhosts-access-log.conf
serve-cgi-bin.conf

加载的模块:

/etc/apache2/mods-enabled$ ls

access_compat.load
alias.conf
alias.load
auth_basic.load
authn_core.load
authn_file.load
authz_core.load
authz_host.load
authz_user.load
autoindex.conf
autoindex.load
deflate.conf
deflate.load
dir.conf
dir.load
env.load
filter.load
mime.conf
mime.load
mpm_prefork.conf
mpm_prefork.load
negotiation.conf
negotiation.load
php7.0.conf
php7.0.load
reqtimeout.conf
reqtimeout.load
setenvif.conf
setenvif.load
status.conf
status.load
wsgi.conf
wsgi.load

wsgi.load:

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

wsgi.conf:

<IfModule mod_wsgi.c>

    #AddHandler wsgi-script .wsgi # unsafe!
    #AddHandler wsgi-script .py # unsafe!

    <FilesMatch ".+\.wsgi$">
    SetHandler wsgi-script
    Require all granted
    </FilesMatch>

    <FilesMatch ".+\.py$">
    SetHandler wsgi-script
    Require all granted
    </FilesMatch>

</IfModule>

终于找到答案了。 您需要设置 ExecCGI 选项以使脚本可执行。 OS 中的可执行标志无关紧要。

所以,答案是为此类脚本添加特殊目录并修改虚拟主机的配置:

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias "/wsgi" "/var/www/wsgi-scripts"
    <Directory "/var/www/wsgi-scripts">
        Require all granted
        Options +ExecCGI
    </Directory>

</VirtualHost>

现在我可以在 http://127.0.0.1/wsgi/python.wsgi

访问我的 hello-worldish 脚本

P.S.

忘了说在 Apache 中使用 'AddHandler' 构造是服务器中的一个严重安全漏洞,因为它认为 a.wsgi.jpg 仍然是 'wsgi'。最好不要使用 'AddHandler' 并使用 RegExp 代替它们。

:-)