Score:1

Why does Apache with mod_wsgi force a download of .py files instad of executing them?

gb flag

Why does Apache with mod_wsgi force a download of .py files instead of executing them?

I'm trying to run Django, but the first issue I have is .py files not executing.

I'm following the docs here https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/modwsgi/

  • I'm running Ubuntu 20.04.3 LTS
  • libapache2-mod-wsgi-py3 installed and enabled
  • apachectl configtest Syntax OK
  • All files chowned to www-data:www-data
  • .py files are chmod +x
  • /var/log/apache2/access.log is empty

/var/log/apache2/error.log contains:

Apache/2.4.41 (Ubuntu) mod_fcgid/2.3.9 OpenSSL/1.1.1k mod_wsgi/4.6.8 Python/3.8 configured -- resuming normal operations

The .htaccess file at root has

RewriteEngine on
ServerSignature Off

Directory of /var/www/html/example.com/public_html is this:

enter image description here

This is what I see; clicking on a .py file forces it to download.

enter image description here

/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
asktyagi avatar
in flag
Do you have any entry with RemoveHandler?
Score:-1
vn flag

WSGIScriptAlias does not provide a way to click and view service for python, you should use your python-defined route (path) to access your application. Open links to .py files may go through the static file serving instead of WSGI.

cuz you mount your WSGI application at the root, try open / instead of /contact to see if it works.

BlueDogRanch avatar
gb flag
I'm already using `WSGIScriptAlias /` instead of `WSGIScriptAlias /contact`
Chino Chang avatar
vn flag
@BlueDogRanch so please try open `/` in your browser instead of opening `/contact`
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.