pythonapachecgi

I get a 404 error while trying to open my python file using apache cgi


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/html\r\n\r\n")
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.


Solution

  • 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