skip to Main Content

I’m new to python backend development, I was trying to set up an apache to use cgi-bin and a simple python file.

This is my apache2.conf (I"ve added the following):

<Directory "/var/www/cgi-bin">
   AllowOverride None
   Options ExecCGI
   Order allow,deny
   Allow from all
   AddHandler cgi-script .py
</Directory>

<Directory "/var/www/cgi-bin">
Options All
</Directory>

And this is my python file, it is called hello.py and it is inside /var/www/cgi-bin:

#!/usr/bin/python3

print ("Content-type:text/htmlrnrn")
print ('<html>')
print ('<head>')
print ('<title>Hello Word - First CGI Program</title>')
print ('</head>')
print ('<body>')
print ('<h2>Hello Word! This is my first CGI program</h2>')
print ('</body>')
print ('</html>')

My apache is up and running, but when I go to: http://localhost/cgi-bin/hello.py, I get the following error:

Not Found
The requested URL was not found on this server.

Apache/2.4.54 (Ubuntu) Server at localhost Port 80

I’ve bounced apache already, stopped it and started it from scratch, no error at all when coming back up.

2

Answers


  1. Chosen as BEST ANSWER

    Thank you, this is how I've fixed it: /etc/apache2/apache2.conf

    #########     Adding capaility to run CGI-scripts #################
    ServerName localhost
    ScriptAlias /cgi-bin/ /var/www/cgi-bin/
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl .py
    

    /etc/apache2/conf-available/serve-cgi-bin.conf Changed from:

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Require all granted
    </Directory>   
    

    to

    ScriptAlias /cgi-bin/ /var/www/cgi-bin/
    <Directory "/var/www/cgi-bin/">
        AllowOverride None
        Options +ExecCGI
    </Directory>
    

  2. Your Apache server seems to be configured incorrectly, therefor not allowing the execution of CGI scripts. Maske sure to check your error logs, file permissions and check your directory path exists. In addition, check that the configuration for your virtual host permits the use of CGI scripts if you are using virtual hosts

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search