lost and found ( for me ? )

Ubuntu 12.04 : use CGI (python scripts) on Apache



# tail -1 /etc/lsb-release ;uname -ri
DISTRIB_DESCRIPTION="Ubuntu 12.04.3 LTS"
3.2.0-56-generic x86_64

# python --version
Python 2.7.3

install apache
# apt-get install apache2

edit /etc/apache2/sites-available/default to allow CGI and SSI
# cat /etc/apache2/sites-available/default
<VirtualHost *:80>
       ServerAdmin webmaster@localhost

       DocumentRoot /var/www
       <Directory />
       Options FollowSymLinks
               AllowOverride None
       </Directory>
       <Directory /var/www/>
               Options Indexes FollowSymLinks MultiViews ExecCGI
               AllowOverride None
               Order allow,deny
               allow from all
       </Directory>


edit /etc/apache2/mods-available/mime.conf
AddHandler cgi-script .cgi .py .pl .rb
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml

start apache
# service apache2 restart

you can use cgi scripts under /var/www directory.

make a python script
# cat example1.py
#!/usr/bin/env python
print "Content-type: text/html"
print
print "<html>"
print "<center>Hello!</center>"
print "</html>"

run the script for syntax check.
# chmod +x example1.py

# ./example1.py
Content-type: text/html

<html>
<center>Hello!</center>
</html>

copy the script to /var/www
# cp example1.py /var/www/
# chmod +x /var/www/example1.py

access to http:// IP /example1.py




No comments:

Post a Comment

Note: Only a member of this blog may post a comment.