Apache2 server not executing CGI script

apache-2.2cgifile-permissions

I have a local Apache2 server (installed with LAMP). The Apache2 directory is /etc/apache2 and my document root is /var/www. Under the doc root, there's a /cgi-bin directory, in which my script cgi.pyis. Executing with ./cgi.py gives the desired output, but when I point my web browser to the file it offers me to download it. Apparently this is often due to an access problem, but after adding a+x permissions to the file the problem remains.

Here's the simple script:

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

#enable debugging
import cgitb
cgitb.enable()

print("Content-Type: text/plain;charset=utf-8");
print()

print("Hello World!");

In the apache2 directory's httpd.conf I've added the follow (it was empty prior to it):

ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory /var/www/cgi-bin/>
    Options ExecCGI
    AddHandler cgi-script .cgi .py
</Directory>

What could be the reason the script won't execute?

EDIT: Rookie mistake, I didn't restart the server after making changes to httpd.conf. After doing that the script executed. Thanks to Jenny D for suggesting it.

Best Answer

The server will only read its configuration file on startup, or when you specifically tell it to reload it. After changing the httpd.conf you need to either restart the server or reload the configuration (either by running apachectl restart, or by running service httpd reload, or something else depending on your operating system/setup).

If you want to have a config that is reread without restarting the server, there is htaccess. However, since that means that apache will re-check the file for every connection, it will slow the server down a bit. Whether that is a problem will of course depend on how great a load the server has to start with.