7

I am a newbie so I saw some tutorials. I have a python script as first.py

#!/usr/bin/python3 print "Content-type: text/html\n" print "Hello, world!"

I have multiple versions of python on my computer. I couldn't figure out my cgi enabled directory so I pasted this code at three places

  1. /usr/lib/cgi-bin/first.py

  2. /usr/lib/cups/cgi-bin/first.py

  3. /var/www/html/first.py

Now when I run this code in terminal it works fine but when I type

curl http://localhost/first.py

it spit out just simple text and does not execute.

I have given all the permissions to first.py

I have enabled and started the server by commands

a2enmod cgi systemctl restart apache2

Please tell how do I execute and what is going on here? Thanks in advance.

asked Jul 2, 2017 at 13:06

4 Answers 4

14

To python cgi script on apache2 server on Ubuntu(Tested on Ubuntu 20.04) from scratch, Follow these steps(Expects python is installed and works perfectly).

  1. Install apache2 sever.

    sudo apt install apache2
    
  2. Enable CGI module.

    sudo a2enmod cgi
    

Here we set /var/www/cgi-bin/ as cgi-bin directory. If you want a different directory, change files appropriately.

  1. Open Apache server configuration file [/etc/apache2/apache2.conf] by,

    sudo gedit /etc/apache2/apache2.conf
    

    And following lines to the end of the file.

    ######### Adding capaility to run CGI-scripts #################
    ServerName localhost
    ScriptAlias /cgi-bin/ /var/www/cgi-bin/
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl .py
    
  2. Open file /etc/apache2/conf-available/serve-cgi-bin.conf by,

    sudo gedit /etc/apache2/conf-available/serve-cgi-bin.conf
    

    Change lines :

    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>
    
  3. Now restart apache2 server by,

    sudo service apache2 restart
    
  4. Now create python script say, first.py inside directory /var/www/cgi-bin/ by,

    sudo gedit /var/www/cgi-bin/first.py
    

    and add following sample code :

    #!/usr/bin/env python
    import cgitb
    cgitb.enable()
    print("Content-Type: text/html;charset=utf-8")
    print ("Content-type:text/html\r\n")
    print("<H1> Hello, From python server :) </H1>")
    

    And give executable permission to first.py by,

    sudo chmod +x /var/www/cgi-bin/first.py
    

Now curl will work.

:~$ curl http://localhost/cgi-bin/first.py
<H1> Hello, From python server :) </H1>

Or open browser and browse http://localhost/cgi-bin/first.py. This should work and will display webpage showing "Hello, From python server :)".

Hope it helps... Happy coding :)

answered Mar 17, 2021 at 5:32
Sign up to request clarification or add additional context in comments.

Comments

4

These lines need to be in your apache2 site configuration file (for example /etc/apache2/sites-available/default-ssl.conf if your website uses HTTPS):

<Directory /var/www/yoursite/yourdir/>
 Options +ExecCGI
 PassEnv LANG
 AddHandler cgi-script .py
</Directory>

Add them for the directory you want and restart apache2. You might also need to add

AddHandler python-program .py

to your httpd.conf if not present already.

A more detailed discussion concerning python scripts and apache2 is found here: Executing a Python script in Apache2.

answered Aug 3, 2017 at 14:23

Comments

0

Probably the problem you're running in Python 3 and your code is written in Python 2 Change the title to python2:

#!/usr/bin/python
print "Content-type: text/html\n"
print "Hello, world!"
answered Jul 2, 2017 at 13:49

Comments

0

For just proof of concept, use this python script ; No input nor output needed.

#!/usr/bin/env python3
import cgitb
cgitb.enable()
outputfile = open("javascriptPython.out", "a")
outputfile.write("Hello from Python\n\n")
answered Dec 3, 2022 at 23:31

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.