简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM