1

I'm working on Ubuntu 16.04LTS with apache2. I working on executing a simple CGI python script "hello.py". But instead of executing the file, it is getting downloaded. If I change the extension from ".py" to ".cgi", the code is displaying in my browser.

My CGI script is saved in /var/www/cgi-bin directory and it is written below

import cgitb
cgitb.enable()    
print("Content-Type: text/html;charset=utf-8")

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>'

I have followed CGI tutorial. This tutorial is following ScriptAlias method to configure apache2. As per the tutorial, I have added the below code to the end of the apche2.conf file

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

And I have changed my conf-available/serve-cgi-bin.conf file into below form

<IfModule mod_alias.c>
        <IfModule mod_cgi.c>
                Define ENABLE_USR_LIB_CGI_BIN
        </IfModule>

        <IfModule mod_cgid.c>
                Define ENABLE_USR_LIB_CGI_BIN
        </IfModule>

        <IfDefine ENABLE_USR_LIB_CGI_BIN>
                ScriptAlias /cgi-bin/ /var/www/cgi-bin/
                <Directory "/var/www/cgi-bin">
                        AllowOverride None
                        Options +ExecCGI
                        #Require all granted
                </Directory>
        </IfDefine>
</IfModule>

Still my CGI script is downloading instead of running. How to solve this problem ?

2
  • Welcome, have a look here and see if you've forgotten anything: httpd.apache.org/docs/2.4/howto/cgi.html (such as LoadModule) Commented Oct 23, 2018 at 6:50
  • Thank you for your support. I have fixed it Commented Oct 23, 2018 at 9:36

1 Answer 1

1

I have fixed it. After editing config files, there is one more step to do.In /etc/apache2/mods-available directory you can see the installed modules of apache2. cgi.load is the module for CGI files. It is not enabled by default. Use these commands to enable them.

$ cd /etc/apache2/mods-enabled
$ sudo ln -s ../mods-available/cgi.load

Then reload the server

$ sudo service apache2 reload
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.