为什么带有 mod_wsgi 的 Apache 强制下载 .py 文件而不是执行它们?

为什么带有 mod_wsgi 的 Apache 强制下载 .py 文件而不是执行它们?

为什么带有 mod_wsgi 的 Apache 强制下载 .py 文件而不是执行它们?

我正在尝试运行 Django,但遇到的第一个问题是 .py 文件无法执行。

我正在关注这里的文档https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/modwsgi/

  • 我正在运行 Ubuntu 20.04.3 LTS
  • libapache2-mod-wsgi-py3 已安装并启用
  • apachectl configtest语法正确
  • 所有文件均归www-data:www-data
  • .py 文件是chmod +x
  • /var/log/apache2/access.log是空的

/var/log/apache2/error.log包含:

Apache/2.4.41 (Ubuntu) mod_fcgid/2.3.9 OpenSSL/1.1.1k mod_wsgi/4.6.8 Python/3.8 已配置 - 恢复正常操作

根目录中的 .htaccess 文件有

RewriteEngine on
ServerSignature Off

/var/www/html/example.com/public_html 的目录如下:

在此处输入图片描述

这是我所看到的;单击 .py 文件会强制下载它。

在此处输入图片描述

/etc/apache2/sites-available/default-ssl.conf:

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SSLEngine on

        SSLCertificateFile  /etc/ssl/certs/ssl-cert-snakeoil.pem
        SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

        <FilesMatch "\.(cgi|shtml|phtml|php)$">
                SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
                SSLOptions +StdEnvVars
        </Directory>


Alias /static /var/www/html/example.com/public_html/contact/static
Alias /media /var/www/html/example.com/public_html/contact/media

<Directory /var/www/html/example.com/public_html/contact/static>
    Require all granted
</Directory>

<Directory /var/www/html/example.com/public_html/contact/media>
    Require all granted
</Directory>

WSGIScriptAlias /  /var/www/html/example.com/public_html/contact/contact/wsgi.py

WSGIDaemonProcess example.com python-home=/var/www/html/example.com/public_html/contact

WSGIProcessGroup example.com

WSGISocketPrefix run/wsgi

 <Directory /var/www/html/example.com/public_html/contact/contact>
    <Files wsgi.py>
        Require all grantd
    </Files>
</Directory>

<Directory /var/www/html/example.com/public_html>
AllowOverride None
    Order allow,deny
    Allow from all
    Options +ExecCGI
    AddHandler cgi-script .py
</Directory>

    </VirtualHost>
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

   # Allow .htaccess files
    <Directory /var/www/html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>


Alias /static /var/www/html/example.com/public_html/contact/static
Alias /media /var/www/html/example.com/public_html/contact/media

<Directory /var/www/html/example.com/public_html/contact/static>
    Require all granted
</Directory>

<Directory /var/www/html/example.com/public_html/contact/media>
    Require all granted
</Directory>

WSGIScriptAlias /  /var/www/html/example.com/public_html/contact/contact/wsgi.py

WSGIDaemonProcess example.com python-home=/var/www/html/example.com/public_html/contact

WSGIProcessGroup example.com

WSGISocketPrefix run/wsgi

 <Directory /var/www/html/example.com/public_html/contact/contact>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>


<Directory /var/www/html/example.com/public_html>
AllowOverride None
    Order allow,deny
    Allow from all
    Options +ExecCGI
    AddHandler cgi-script .py
</Directory>

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

答案1

WSGIScriptAlias没有提供用于单击和查看 Python 服务的方法,您应该使用 Python 定义的路由(路径)来访问您的应用程序。打开.py文件链接可能会通过静态文件服务而不是 WSGI。

因为你将 WSGI 应用程序挂载在根目录,尝试打开 /而不是/contact看看它是否有效。

相关内容