Python Forum
Run Python CGI from Apache
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Run Python CGI from Apache
#1
Depending on the Linux distro and even the version the name and path of the apache config file will be different. You did not specify which distro you were running on. 

I can only say regarding ubuntu and LAMP as that is what i have created servers on numerous times. Since you did not specify which distro you were on...from this point on ill assume your on ubuntu. As it is the most used. 

1) You have to install AND enable the cgi_mod
sudo apt-get install libapache2-mod-python
enable it 
sudo a2enmod cgi
2) create and change your permissions for the cgi directory
sudo chmod 755 /usr/lib/cgi-bin
(change path to where ever you want it, but you need to add a script alias if you change it from default /usr/lib/cgi-bin)
2) In your apache config file you have to add a directory to the planned CGI directory. The best way to explain that is to use an example...

This is my /etc/apache2/apache2.conf directory adding for my cgi directory
ScriptAlias "/cgi-bin/" "/var/www/html/cgi-bin" 
<Directory "/var/www/html/cgi-bin">
    AllowOverride None
    Options +ExecCGI +MultiViews +SymLinksIfOwnerMatch +Includes +Multiviews
    Order allow,deny
    Allow from all
    AddHandler cgi-script .py
    AddHandler cgi-script .cgi
    AddHandler wsgi-script .wsgi
</Directory>
This is my directory listing for my cgi directory which is /var/www/html/cgi-bin. The scriptalias line enables this directory path.  The main points are Options +ExecCGI and the addhandler for .py whihc enable you to run cgi scripts. Save this in your apache config file. 

Restart apache. 
sudo service apache2 restart
Close the old browser and start anew after. 


Then on your scripts...
make sure to add a shebang line
#!/usr/bin/python3
As well as cgitb to allow you to view tracebacks through the browser
import cgitb
cgitb.enable()
If you do not do this you might get an internal server error because you typo'd something and now cannot see the traceback to point you to your python script error. If you do not do this you can still view the traceback through the error logs. Create a second terminal to the server (or just use tmux to split the terminal) and execute
tail -f  /var/log/apache2/error.log
before you execute your python script to view the traceback in the error logs

And the first line printed must be 
Content-type: text/html\n
and you might as well make it as organized as possible by using the format method to insert values into your html such as....
html = """Content-type: text/html\n

<html>
<head>
<title>Python Porting Guide</title>
{MENU}
</head>
<body>
<br>
Input all or a portion of the known syntax for either Python2.x or Python3.x <br />
Results are based only on version differences, not every module in existence to python<br />

<form method=POST action='portingguide.py'>
        <p><input type=text name=search placeholder="'viewall' for everything">
        <p><input type=submit value='Search'> <!-- <input type="button" onclick="change.viewall()"        value="viewall"> -->
<br /><br />
{SEARCHED_TEXT}{SEARCHED}<br /><br />
{SEARCH_TEXT} <br /><br />
</body>
</html>
""".format(
        MENU=Menu().get_content(),
        SEARCHED=searched,
        SEARCH_TEXT=search_text,
        SEARCHED_TEXT=searched_text,
        )
print(html)
and make your python script executable via
sudo chmod a+x SCRIPTNAME.py
If you get any errors throughout this process, or changes...I would come back, post the outcome/error/etc.
Recommended Tutorials:
Reply
#2
-
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020