Score:0

Prevent Apache2 from executing CGI in Static Directory

cn flag

I am running an Apache 2 web server running on Ubuntu 20.04 LTS. I have a Python CGI handler enabled for the /var/www/html directory, which is the DocumentRoot. I am wondering how to exclude a certain directory from running CGI for Python files.
Here in my CGI config:

<Directory "/var/www/html">
    Options +ExecCGI
    AddHandler cgi-script .py
        <IfModule mod_rewrite.c>
                RewriteEngine On
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteCond %{REQUEST_FILENAME}\.py -f
                RewriteRule ^(.*)$ $1.py
        </IfModule>
</Directory>

<Directory "/var/www/html/static/cdn">
        DirectoryIndex disabled
        Options +Indexes -ExecCGI
        AllowOverride None
        Require all granted
</Directory>

In the /static/cdn directory, I want .py files to be served just like any other static file, instead of being executed as CGI. Here is a tree of the cdn directory:

.
├── checkForUpdates.exe
├── checkForUpdates.py
└── findLogErrors
    ├── botCriteria.json
    ├── cleanup.json
    ├── findLogErrors.exe
    └── version.json

1 directory, 6 files

I am able to see the indexes of the directory in the web browser, as desired. I am able to view or download any file from this directory except checkForUpdates.py. The server is not trying to execute it as CGI, it's giving a 403. The permissions on checkForUpdates.py are the same as the other files:

nbroyles@webserver:/var/www/html/static/cdn$ ls -altr
total 15548
-rwxrwxr-x 1 www-data web 15901526 Nov 17 11:37 checkForUpdates.exe
drwxrwxr-x 7 www-data web     4096 Nov 19 11:13 ..
drwxrwxr-x 2 www-data web     4096 Dec 23 09:41 findLogErrors
drwxrwxr-x 3 www-data web     4096 Dec 23 09:49 .
-rwxrwxr-x 1 www-data web     2072 Dec 23 09:49 checkForUpdates.py

How can I view the .py file just like any of the .json or .exe files? I'm sure there's something simple I'm missing in my config. Any help is greatly appreciated!

Score:1
jp flag

You need to add SetHandler default-handler in <Directory "/var/www/html/static/cdn">

Noah Broyles avatar
cn flag
That works to display the Python file, but now the indexes are not showing.
jp flag
Then chage it to `SetHandler default-handler .py`
Noah Broyles avatar
cn flag
`apachectl configtest` shows `SetHandler takes one argument, a handler name that overrides any other configured handler`, but I changed it to `AddHandler` and it's all good. Thank you!
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.