hfr1988
Posts: 2
Joined: Tue Jul 19, 2016 2:00 am
Location: Auburn AL, USA

Run python script as cgi under apache2 server[my experience]

Sun Jul 24, 2016 4:58 pm

I was trying to set up a server with Pi 3, so I ran into this python CGI + Apache server path. Here is what I did and it works:

1. Install apache2 first, after the installation you should be able to see the default page with 'localhost' in your browser.

Code: Select all

sudo apt-get install apache2
2. Enable mods in apache2 for cgi (the codes below showed two ways to do it)

Code: Select all

sudo a2enmod cgid
cd /etc/apache2/mods-enabled
sudo ln -s /etc/apache2/mods-available/cgi.load
3. Modify the config file: /etc/apache2/conf-enabled/serve-cgi-bin.conf to enable python code.

Code: Select all

<Directory "usr/lib/cgi-bin">
             ... ...

             AddHandler cgi-script .py          # add this line (there is a blank between cgi-script and .py)
</Directory>
4. Notice that the default directory for cgi script is /usr/lib/cgi-bin/, so we will create a test file under this directory.

Code: Select all

sudo nano /usr/lib/cgi-bin/pytest.py

Code: Select all

#!/usr/bin/python

import cgi
import cgitb
cgitb.enable()

print 'Content-type: text/html\n\n'
print '<h1>Python Script Test</h1>'

5. Make the python file executable

Code: Select all

sudo chmod +x /usr/lib/cgi-bin/pytest.py
6. Restart the apache2 service

Code: Select all

sudo service apache2 restart
Now you should be able to see your test file output under 'localhost/cgi-bin/pytest.py' in your browser.

Note: I was having trouble restart the apache2 service after enable the cgi mods. So I actually reinstalled the apache2, it works after that. Here is how I reinstalled the apache2:

Code: Select all

sudo apt-get --purge remove apache2
sudo apt-get autoremove
sudo apt-get install apache2
Hope this experience can help someone. Please let me know if there are anything need to be modified.

Bruce Crowthorne
Posts: 89
Joined: Wed Jan 11, 2012 7:19 pm
Location: Sussex, UK

Re: Run python script as cgi under apache2 server[my experie

Tue Jan 10, 2017 7:25 pm

Thanks for your write up, it is very clear and has nearly helped!

My test set up is a fresh build on a RPi1 with Jessie and Apache 2.4.10.
I created the pytest.py file as you suggest. It is in cgi-bin and it is executable.

I have tried to follow your notes as closely as I can and think I have completed each step, but I get an "500 Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request."

Any suggestions of what I can check or try
Thanks
Bruce

Moolectro
Posts: 2
Joined: Mon Apr 03, 2017 6:15 pm

Re: Run python script as cgi under apache2 server[my experie

Tue Apr 04, 2017 3:40 am

Hi
I'm follow your write up. It succeeded. No problem. But the requested URL was not found. Why?
Halp me please. :oops: :oops: :oops:

User avatar
peterv6i
Posts: 12
Joined: Mon Jul 17, 2017 5:35 pm
Location: Slovenija
Contact: Website

Re: Run python script as cgi under apache2 server[my experience]

Wed Sep 20, 2017 7:16 am

I have followed your instructions on raspberrypi zero w and it works well..

Image

ChrisseDD
Posts: 1
Joined: Sun Nov 05, 2017 3:17 pm

Re: Run python script as cgi under apache2 server[my experience]

Sun Nov 05, 2017 3:28 pm

Hi, It do not work for me. :(

I´m not able to restart the apache. The reinstall doesn´t help. Anybody able to help me?
btw: I´m a Pi-Newbe :oops:
Image

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Run python script as cgi under apache2 server[my experience]

Tue Nov 07, 2017 12:45 am

That photo almost tells you what's wrong.

Try systemctl -l status apache2 and your syntax error in line#146 should be more obvious.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

dcristut
Posts: 10
Joined: Sat Jun 24, 2017 3:27 pm

Re: Run python script as cgi under apache2 server[my experience]

Mon Jan 22, 2018 10:39 am

Hello,

Thank you for the helpful tutorial.

I am trying something: I want to put the CGI module (pytest.py) in some other directory : /home/pi/usbdrive/www/cgi-bin
Therefore I have changed the configuration file /etc/apache2/conf-enabled/serve-cgi-bin.conf like this: <Directory "/home/pi/usbdrive/www/cgi-bin"> and put the pytest.py file in that directory.

But I have a problem with user rights: "You don't have permission to access /cgi-bin/pytest.py on this server."

Any idea how should I configure that directory or what configuration parameters are needed?

Thank you

luvnits94
Posts: 1
Joined: Fri Apr 20, 2018 10:07 pm

Re: Run python script as cgi under apache2 server[my experience]

Fri Apr 20, 2018 10:11 pm

I have followed your post closely and it was really helpful .

Now i am able to execute a perl script (eg test.pl )

But when i try to execute a python script (e.g pytest.py ), I get the following error in apache error log

AH01215: (8)Exec format error: exec of '/usr/lib/cgi-bin/pytest.py' failed
End of script output before headers: pytest.py

RichardBlundell
Posts: 1
Joined: Fri Dec 28, 2018 5:23 pm

Re: Run python script as cgi under apache2 server[my experience]

Fri Dec 28, 2018 5:25 pm

I had the same problem (Internal server error), checked /var/log/apache2/error.log and it said "Permission denied - exec of /usr/lib/cgi-bin/pytest.py failed", so I ran a "sudo chmod +x /usr/lib/cgi-bin/pytest.py" to add execute permissions, and the page then worked. Hope this helps someone else!

Schorschi
Posts: 220
Joined: Thu Nov 22, 2012 9:38 pm

Re: Run python script as cgi under apache2 server[my experience]

Wed Feb 13, 2019 9:24 pm

Nice, simple, straight forward. I did not have any issues, worked just fine. Thanks.

martinmmps
Posts: 44
Joined: Fri Apr 29, 2016 5:47 pm

Re: Run python script as cgi under apache2 server[my experience]

Fri May 31, 2019 12:09 pm

What Python Code do I need to get 2 buttons on the Apache Site which run separate functions?

e.g.

Code: Select all

def function1():
	pass
	
def function2():
	pass
Ubuntu users are apt to get this joke

Return to “Troubleshooting”