Xantium
Xantium

Reputation: 11603

How do I get a Python cgi script running in Apache2

I have a very simple test.py located at /var/www/html/master.com/ that I'm trying to run in Apache2, on Ubuntu 18.1

My Python code:

#!/usr/bin/env python

print("Content-type: text/html\n\n")
print("<h1>Hello</h1>")

What I have run so far:

Run a2enmod cgi to enable cgi:

ISimon@simon-EasyNote-TK85:~$ a2enmod cgi
Your MPM seems to be threaded. Selecting cgid instead of cgi.
Module cgid already enabled

Created the file cgi-enabled.conf at /etc/apache2/conf-available/, which contained the following:

# create new
# process .cgi and .py as CGI scripts

<Directory "/var/www/html/master.com">
  Options + ExecCGI
  AddHandler cgi-script .cgi .py
</Directory>

Restarted Apache2 with systemctl restart apache2

I then went to http://localhost/test.py and it offered to download the file. It should display as plain html.

How do I get my server configured properly?

Upvotes: 1

Views: 1832

Answers (1)

furas
furas

Reputation: 143216

You maybe have to set script as executable

chmod a+x test.py

And it should have shebang (#!) in first line to inform system what program should run this code - ie.

#!/usr/bin/env python

Upvotes: 1

Related Questions