Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 727
Edward Elric Avatar asked Jul 15 '26 06:07

Edward Elric


1 Answers

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

like image 55
anoopknr Avatar answered Jul 18 '26 07:07

anoopknr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!