How do I run python cgi script on apache2 server on Ubuntu 16.04?
Asked Answered
P

4

6

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.

Pastose answered 2/7, 2017 at 13:6 Comment(0)
A
13

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 :)

Ama answered 17/3, 2021 at 5:32 Comment(0)
O
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.

Orleans answered 3/8, 2017 at 14:23 Comment(0)
O
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!"
Orta answered 2/7, 2017 at 13:49 Comment(0)
K
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")
Kidron answered 3/12, 2022 at 23:31 Comment(1)
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.Verb

© 2022 - 2024 — McMap. All rights reserved.