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
/usr/lib/cgi-bin/first.py
/usr/lib/cups/cgi-bin/first.py
/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.
4 Answers 4
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).
Install apache2 sever.
sudo apt install apache2Enable 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.
Open Apache server configuration file [
/etc/apache2/apache2.conf] by,sudo gedit /etc/apache2/apache2.confAnd 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 .pyOpen file
/etc/apache2/conf-available/serve-cgi-bin.confby,sudo gedit /etc/apache2/conf-available/serve-cgi-bin.confChange 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>Now restart apache2 server by,
sudo service apache2 restartNow create python script say, first.py inside directory
/var/www/cgi-bin/by,sudo gedit /var/www/cgi-bin/first.pyand 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 :)
Comments
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.
Comments
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!"
Comments
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")
1 Comment
Explore related questions
See similar questions with these tags.