1

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?

7
  • 1
    usually CGI scripts are executed only in folder cgi-bin. Maybe you should put code in subfolder cgi-bin and run localhost/cgi-bin/test.py Commented Dec 14, 2019 at 21:03
  • 1
    maybe script have to be set as executable - chmod a+x test.py. And it should have shebang (#!) in first line to inform what program should run this code - ie, #!/usr/bin/env python. Commented Dec 14, 2019 at 21:05
  • @furas I has the shebang line, and I tried the command chmod a+x test.py sadly it did exactly the same Commented Dec 14, 2019 at 21:11
  • 1
    please, always add code, data and error message in question, not in comment - they will be more readable and all people will see it. Commented Dec 14, 2019 at 21:16
  • 1
    I checked apache doc and it shows your configuration as method to run it in main folder. Frankly, I don't use CGI but web frameworks like Flask, Django because it is easier to write code - they have many elements which helps. And code is better organized. Commented Dec 14, 2019 at 21:22

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.