1

I have a problem, I can't execute a Python script on my Raspberry Pi 3. I set an Apache server to execute a python script by following a tutorial.

I can execute the following script:

#!/usr/bin/python3  

import RPi.GPIO as GPIO  # bibliothèque pour utiliser les GPIO
import time              # bibliothèque pour gestion du temps

print("Content-type:text/html\r\n\r\n")
print("<html><body>")
print("<h1> It's works!! </h1>")
for i in range(5):
        print("<h2> Hello world!"+str(i)+"</h2>")
print("</body></html>")   

GPIO.setmode(GPIO.BOARD)   # defini qu'on travail avec les numéro de pin et non de Pin ( = GPIO.setmode(GPIO,BOARD)
GPIO.setup(5,GPIO.OUT)  # la GPIO 3 réglée en sortie (output)
var = 1

# Boucle
while var < 5:
        GPIO.output(5,GPIO.HIGH)   # sortie au niveau logique haut (3.3 V)
        time.sleep(2)               # on ne change rien pendant 1 seconde
        print(var)
        var = var + 1
        GPIO.output(5,GPIO.LOW)    # sortie au niveau logique bas (0 V)
        time.sleep(2)               # on ne change rien pendant 1 seconde

GPIO.cleanup()

But when I want execute this script:

#!/usr/bin/python3

import time
import pigpio

LED=3

pi = pigpio.pi()

pi.set_mode(LED,pigpio.OUTPUT)

for i in range(10):
      pi.write(LED,1)
      time.sleep(0.2)
      pi.write(LED,0)
      time.sleep(0.2)

pi.stop()

My browser prints the error message:

Internal Server Error

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

Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Apache/2.4.10 (Raspbian) Server at localhost Port 80

in the server error log there is nothing....

I don't understand why it doesn't work. Any help greatly appreciated.

For information: To solve this problem I put simple script but all my real script works with IDLE or in bash terminal.

Output ls -la (files cligno.py and hello.py):

-rwxr-x--- 1 pi       pi         293 juil.  3 18:58 cligno.py
-rw-r--r-- 1 pi       pi         423 juil.  1 18:17 com_fin.py
-rw-r--r-- 1 pi       pi         427 juil.  1 18:07 com_service.py
-rw-r--r-- 1 pi       pi        2409 juil.  2 17:30 constantes.py    
drwxr-xr-x 2 pi       pi        4096 juin  25 20:32 Db
-rwxr-xr-x 1 pi       pi         916 juin  25 14:36 hello.py
-rw-r--r-- 1 pi       pi        3082 juil.  3 18:34 Init_v2.py
-rwxr-xr-x 1 pi       pi         975 juil.  2 10:00 ledpresence.py
-rw-r--r-- 1 pi       pi         460 juil.  2 10:01 ledtenscligno.py
-rw-r--r-- 1 pi       pi         228 juil.  2 10:48 ledtensfixe.py
-rwxrwxrwx 1 www-data www-data  1087 juin  20 20:42 notation.py 
drwxr-xr-x 2 pi       pi        4096 juil.  2 18:36 Old 
drwxrwxrwx 2 www-data www-data  4096 juil.  3 18:34 __pycache__
-rw-r--r-- 1 pi       pi       10240 juin  25 22:51 recettes.db
-rwxrwxrwx 1 www-data www-data  4828 juil.  2 20:55 searchDb.py
-rwxrwxrwx 1 www-data www-data  5627 juil.  2 19:43 service_v2.py
-rwxrwxrwx 1 www-data www-data  2203 juil.  3 18:35 shutdown.py 
drwxrwxrwx 2 www-data www-data  4096 juin  25 18:48 Test
-rwxr-xr-x 1 www-data www-data   197 juil.  2 18:15 transfert.html
-rwxrwxrwx 1 www-data www-data   370 juil.  2 18:22 transfert.py

and my error file with my differents tests but no error since i tried to execute the script :

[Sat Jun 24 11:48:57.877987 2017] [cgid:error] [pid 1962:tid 1995513856] (13)Permission denied: AH01241: exec of '/var/www/html/py/transfert.py' failed 
[Sat Jun 24 11:48:57.880756 2017] [cgid:error] [pid 1760:tid 1785721904] [client ::1:54474] End of script output before headers: transfert.py
[Sat Jun 24 11:49:00.699103 2017] [cgid:error] [pid 1963:tid 1995513856] (13)Permission denied: H01241: exec of '/var/www/html/py/transfert.py' failed
[Sat Jun 24 11:49:00.701722 2017] [cgid:error] [pid 1760:tid 1777333296] [client ::1:54476] End of script output before headers: transfert.py /usr/bin/env: python3 : No such file or directory 
[Sat Jun 24 11:51:57.665977 2017] [cgid:error] [pid 1760:tid 1928328240] [client ::1:54482] End of script output before headers: Testled2.py /usr/bin/env: python3 : No such file or directory 
[Sat Jun 24 11:52:00.297699 2017] [cgid:error] [pid 1760:tid 1919939632] [client ::1:54484] End of script output before headers: Testled2.py 
[Sat Jun 24 11:58:12.694075 2017] [mpm_event:notice] [pid 1756:tid 1995513856] AH00491: caught SIGTERM, shutting down 
[Sat Jun 24 11:59:16.551674 2017] [mpm_event:notice] [pid 2461:tid 1995575296] AH00489: Apache/2.4.10 (Raspbian) configured -- resuming normal operations 
[Sat Jun 24 11:59:16.551966 2017] [core:notice] [pid 2461:tid 1995575296] AH00094: Command line: '/usr/sbin/apache2' 
[Sat Jun 24 12:00:44.644903 2017] [mpm_event:notice] [pid 2461:tid 1995575296] AH00491: caught SIGTERM, shutting down 
[Sat Jun 24 12:00:49.113147 2017] [mpm_event:notice] [pid 2636:tid 1995628544] AH00489: Apache/2.4.10 (Raspbian) configured -- resuming normal operations 
[Sat Jun 24 12:00:49.113464 2017] [core:notice] [pid 2636:tid 1995628544] AH00094: Command line: '/usr/sbin/apache2' /usr/bin/env: python3 : No such file or directory 
[Sat Jun 24 12:00:53.928111 2017] [cgid:error] [pid 2640:tid 1945105456] [client ::1:54506] End of script output before headers: Testled2.py /usr/bin/env: python3 : No such file or directory 
[Sat Jun 24 12:00:57.803127 2017] [cgid:error] [pid 2641:tid 1936716848] [client ::1:54508] End of script output before headers: Testled2.py /usr/bin/env: python3 : No such file or directory 
[Sat Jun 24 12:00:59.073735 2017] [cgid:error] [pid 2640:tid 1936716848] [client ::1:54510] End of script output before headers: Testled2.py /usr/bin/env: python3 : No such file or directory 
[Sat Jun 24 12:01:01.466476 2017] [cgid:error] [pid 2640:tid 1928328240] [client ::1:54512] End of script output before headers: Testled2.py /usr/bin/env: python3 : No such file or directory 
[Sat Jun 24 12:01:08.607553 2017] [cgid:error] [pid 2640:tid 1919939632] [client ::1:54514] End of script output before headers: Testled2.py 
[Sat Jun 24 12:01:23.331630 2017] [cgid:error] [pid 2640:tid 1911551024] [client 192.168.0.69:52069] AH01265: attempt to invoke directory as script: /var/www/html/py/ /usr/bin/env: python3 : No such file or directory 
[Sat Jun 24 12:01:38.761712 2017] [cgid:error] [pid 2640:tid 1961882672] [client 192.168.0.69:52080] End of script output before headers: Testled2.py 
[Sat Jun 24 12:02:31.631065 2017] [cgid:error] [pid 2641:tid 1903162416] [client 192.168.0.69:52117] AH01265: attempt to invoke directory as script: /var/www/html/py/ 
[Sat Jun 24 12:04:50.248429 2017] [cgid:error] [pid 2640:tid 1836053552] [client 192.168.0.69:52221] AH01265: attempt to invoke directory as script: /var/www/html/py/ 
[Sat Jun 24 12:05:23.069708 2017] [cgid:error] [pid 2641:tid 1894773808] [client ::1:54516] AH01265: attempt to invoke directory as script: /var/www/html/py/ /usr/bin/env: python3 : No such file or directory 
[Sat Jun 24 12:05:35.963069 2017] [cgid:error] [pid 2641:tid 1886385200] [client ::1:54518] End of script output before headers: Testled2.py 
[Sat Jun 24 12:05:57.048304 2017] [mpm_event:notice] [pid 2636:tid 1995628544] AH00491: caught SIGTERM, shutting down 
[Sat Jun 24 12:06:02.926832 2017] [mpm_event:notice] [pid 709:tid 1995530240] AH00489: Apache/2.4.10 (Raspbian) configured -- resuming normal operations 
[Sat Jun 24 12:06:02.930894 2017] [core:notice] [pid 709:tid 1995530240] AH00094: Command line: '/usr/sbin/apache2' 
[Sat Jun 24 12:45:43.867765 2017] [mpm_event:notice] [pid 709:tid 1995530240] AH00491: caught SIGTERM, shutting down 
[Sat Jun 24 12:45:45.102413 2017] [mpm_event:notice] [pid 1465:tid 1996050432] AH00489: Apache/2.4.10 (Raspbian) configured -- resuming normal operations 
[Sat Jun 24 12:45:45.102554 2017] [core:notice] [pid 1465:tid 1996050432] AH00094: Command line: '/usr/sbin/apache2' 
[Sat Jun 24 12:46:01.644970 2017] [mpm_event:notice] [pid 1465:tid 1996050432] AH00491: caught SIGTERM, shutting down 
[Sat Jun 24 12:46:02.883301 2017] [mpm_event:notice] [pid 1596:tid 1996427264] AH00489: Apache/2.4.10 (Raspbian) configured -- resuming normal operations 
[Sat Jun 24 12:46:02.883443 2017] [core:notice] [pid 1596:tid 1996427264] AH00094: Command line: '/usr/sbin/apache2' 
[Sat Jun 24 12:48:17.494705 2017] [mpm_event:notice] [pid 1596:tid 1996427264] AH00491: caught SIGTERM, shutting down 
[Sat Jun 24 12:48:18.717328 2017] [mpm_event:notice] [pid 1762:tid 1996439552] AH00489: Apache/2.4.10 (Raspbian) configured -- resuming normal operations 
[Sat Jun 24 12:48:18.717625 2017] [core:notice] [pid 1762:tid 1996439552] AH00094: Command line: '/usr/sbin/apache2' 
[Sat Jun 24 12:50:10.892651 2017] [mpm_event:notice] [pid 1762:tid 1996439552] AH00491: caught SIGTERM, shutting down 
[Sat Jun 24 12:50:12.149572 2017] [mpm_event:notice] [pid 1937:tid 1996283904] AH00489: Apache/2.4.10 (Raspbian) configured -- resuming normal operations 
[Sat Jun 24 12:50:12.149920 2017] [core:notice] [pid 1937:tid 1996283904] AH00094: Command line: '/usr/sbin/apache2' 
[Sat Jun 24 12:56:53.985254 2017] [cgid:error] [pid 1940:tid 1946154032] [client 192.168.0.69:53604] AH01264: script not found or unable to stat: /var/www/html/cgi-bin, referer: http://192.168.0.99/pages/Cocktail-CubaLibre.html 
[Sat Jun 24 12:57:01.454264 2017] [cgid:error] [pid 1940:tid 1929376816] [client 192.168.0.69:53604] AH01264: script not found or unable to stat: /var/www/html/cgi-bin, referer: http://192.168.0.99/pages/Cocktail-CubaLibre.html 
[Sat Jun 24 13:00:54.022091 2017] [mpm_event:notice] [pid 1937:tid 1996283904] AH00491: caught SIGTERM, shutting down 
[Sat Jun 24 13:00:55.303861 2017] [mpm_event:notice] [pid 2664:tid 1996374016] AH00489: Apache/2.4.10 (Raspbian) configured -- resuming normal operations 
[Sat Jun 24 13:00:55.304154 2017] [core:notice] [pid 2664:tid 1996374016] AH00094: Command line: '/usr/sbin/apache2' 

My Apache config to execute script is in "000-default.conf":

> <Directory /var/www/>
> Options ExecCGI Indexes FollowSymlinks Multiviews
> AllowOverride none
> Order allow,deny
> allow from all
> AddHandler cgi-script .py
> </Directory>
  • 1
    What is the output of: ls -la when run in the directory where your script is located? What tutorial specifically are you following? also please add the last few lines of your /var/log/apache2/error.log – Steve Robillard Jul 3 '17 at 20:04
  • What is the name of the two scripts, and which one works? – Steve Robillard Jul 4 '17 at 18:35
  • Hello. py works , the others doesn't work. the one mentioned above is cligno.py – Ririx Jul 4 '17 at 19:26
  • try this switch to the directory where the scripts are. Then run these commands sudo chown www-data:www-data clingo.py and then sudo chmod 755 clingo.py then retry your script if it fails please post the last few lines of the error.log file – Steve Robillard Jul 4 '17 at 19:42
  • I have the same message : Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log. and no line appeared in error.log – Ririx Jul 4 '17 at 20:11
0

here is ls -la:

drwxrwxrwx 6 www-data www-data  4096 juil.  4 18:17 .
drwxrwxrwx 7 root     www-data  4096 juil.  4 22:04 ..
-rwxr-xr-x 1 www-data www-data   293 juil.  5 17:09 cligno.py
-rwxr-xr-x 1 pi       pi         449 juil.  4 20:47 com_fin.py
-rwxr-xr-x 1 pi       pi         427 juil.  1 18:07 com_service.py
-rwxr-xr-x 1 pi       pi        2409 juil.  2 17:30 constantes.py
drwxr-xr-x 2 pi       pi        4096 juin  25 20:32 Db
-rwxr-xr-x 1 pi       pi         916 juin  25 14:36 hello.py
-rwxr-xr-x 1 pi       pi        3082 juil.  3 18:34 Init_v2.py
-rwxr-xr-x 1 pi       pi         975 juil.  2 10:00 ledpresence.py
-rwxr-xr-x 1 pi       pi         460 juil.  2 10:01 ledtenscligno.py
-rwxr-xr-x 1 pi       pi         228 juil.  2 10:48 ledtensfixe.py
-rwxrwxrwx 1 www-data www-data  1087 juin  20 20:42 notation.py
drwxr-xr-x 2 pi       pi        4096 juil.  2 18:36 Old
drwxrwxrwx 2 www-data www-data  4096 juil.  4 20:48 __pycache__
-rw-r--r-- 1 pi       pi       10240 juin  25 22:51 recettes.db
-rwxrwxrwx 1 www-data www-data  4887 juil.  4 20:42 searchDb.py
-rwxrwxrwx 1 www-data www-data  6485 juil.  4 20:32 service_v2.py
-rwxrwxrwx 1 www-data www-data  2203 juil.  3 18:35 shutdown.py
drwxrwxrwx 2 www-data www-data  4096 juil.  4 22:04 Test
-rwxr-xr-x 1 www-data www-data   197 juil.  2 18:15 transfert.html
-rwxrwxrwx 1 www-data www-data   370 juil.  2 18:22 transfert.py

and last line of erro.log:

[Sat Jun 24 13:00:54.022091 2017] [mpm_event:notice] [pid 1937:tid 1996283904] AH00491: caught SIGTERM, shutting down
[Sat Jun 24 13:00:55.303861 2017] [mpm_event:notice] [pid 2664:tid 1996374016] AH00489: Apache/2.4.10 (Raspbian) configured -- resuming normal operations
[Sat Jun 24 13:00:55.304154 2017] [core:notice] [pid 2664:tid 1996374016] AH00094: Command line: '/usr/sbin/apache2'

Today i tried to modify hello.py:

when i tried this code it's works:

#!/usr/bin/python3

banane =" singe"

import RPi.GPIO as GPIO  # bibliothèque pour utiliser les GPIO
import time              # bibliothèque pour gestion du temps
from tkinter.messagebox import *

print("Content-type:text/html\r\n\r\n")
print("<html><body>")
print("<h1> It's works!! </h1>")
print("<h2>",banane,"</h2>")
for i in range(5):
        print("<h2> Hello world!"+str(i)+"</h2>")
print("</body></html>")

But with this code it's doesn't work though browser but work with IDLE:

#!/usr/bin/python3

banane =" singe"

import RPi.GPIO as GPIO  # bibliothèque pour utiliser les GPIO
import time              # bibliothèque pour gestion du temps
from tkinter.messagebox import *

showerror("Manque verre", "Hey mon ptit gars, faut mettre un verre")
print("Content-type:text/html\r\n\r\n")
print("<html><body>")
print("<h1> It's works!! </h1>")
print("<h2>",banane,"</h2>")
for i in range(5):
        print("<h2> Hello world!"+str(i)+"</h2>")
print("</body></html>")

may be this is a python interpreter or script problem and not server setting...

  • Please edit your question instead of adding further details as an answer or a comment. – Steve Robillard Jul 4 '17 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.