Run Python script as CGI program with Apache2 in Linux / Wamp in Windows

If you’ve Apache & Python installed, you can execute your python scripts as CGI programs, which your visitors can access if you host the scripts in a server. You can write your scripts in python (file-name will end with .py) or in perl (extention .pl) or anything else which is compatible.

Step 1 – Write a “Hello, World! ” script in Python

In a text editor, type following code. Give the file name “first.py

#!/usr/bin/env python
print "Content-type: text/html\n"

print "Hello, world!"

The first line is important – it tells where in our computer Python is available. If you’re using windows, use appropriate location, for example, change the first line to something like:

#!C:/Python27/python.exe

Step 2 – Put the script in a suitable directory

Important: If you are in Linux, make your file executable! Type:

chmod +x /path/to/your/first.py

Pretty simple!

  1. Let’s rename the file to first.py and make it executable, if we’re in Linux.
  2. Put it in a directory named “cgi-bin” under our document root. Then, the file may be accessed in the web-browser by simply typing: http://localhost/cgi-bin/first.py

Step 3 – Configure Apache2

Next step is to configure Apache so that it treats our file as a CGI program. Go to the directory where your Apache configuration file exists. In Linux, it resides under /etc/apache2/ and the configuration file is httpd.conf

NOTE: If virtual hosts are enabled (normally the are enabled), changing the httpd.conf will have no effect. You may need to edit particular configuration file for the site which is enabled. Go to the sites-enabled  directory and open the file which is currently enabled (If you have only the file “default” in your /etc/apache2/sites-enabled directory, you should open this file with your text editor)

NOTE: If you are in windows, and using Wamp, you can simply open the httpd.conf file and make following changes.

Edit the configuration file:

Search for the line: ScriptAlias /cgi-bin/ /whatever-path/ – when you find it, comment out the line: that is add a # in front of the line:

#ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

Then,  paste following lines of code:

<Directory "/location/to/your/cgi-bin/">
AddHandler cgi-script .cgi .py
AllowOverride All
Options +Indexes FollowSymLinks +ExecCGI
Order allow,deny
Allow from all
</Directory>

If your first.py is in /var/www/cgi-bin/ folder, then replace the first line by <Directory “/var/www/cgi-bin”>

You’re done! Now restart Apache to activate all changes.

If you’re in Linux, open a terminal and enter sudo /etc/init.d/apache2 reload to restart Apache. If you’re using Wamp in Windows, you can restart Apache clicking appropriate icons!

 

Test…

 

Fire up your web browser, type http://localhost/cgi-bin/first.py and hit Enter…

 

If you find only the text:

Hello, world!

in the page, then congratulations!

If you find any other text than Hello, world!, something went wrong. Check for your mistakes. Ensure that “cgi_module” Apache module is enabled.

 

You may find this link helpful.

Loving Ubuntu 11.04 with customized Theme! (with Murrina-LemonGraphite, Equinox Evolution, JiniOrange)

I’ve downloaded Ubuntu 11.04 and already loving it. 😀 I installed my favorite themes, & once again, satisfied the way it looks.

Here is the detailed description of my customized theme:

Controls: Murrina-LemonGraphite

Window-Border: Equinox Evolution Rounded

Icons: JiniOrange

How To Install?

  1. Right click on your desktop, select “change desktop background”
  2. In the Themes Tab, Click “Install”
  3. Browse the downloaded file

Thus you can install these downloaded theme components. Once installed all the files, you can click “customized” to separately select Controls, Window Borders, Icons etc. 🙂

PostgreSQL with PHP in Ubuntu: Step-by-Step How To!

This document is for latest version of PostgreSQL: 8.4, but it should work in any newer versions, as long as you change the commands appropriately, replacing the version mentioned with correct version 🙂

Installation

Install PostgreSQL:

sudo apt-get install postgresql

Install GUI Administration application:

sudo apt-get install pgadmin3

Install PHP based Web Administration site (like phpMyAdmin for MySQL database):

sudo apt-get install phppgadmin

I found this helpful, because it installed all dependant packages required to access postGRE database from PHP 😉

Configuration

Configure so that you can access via localhost:

gksudo gedit /etc/postgresql/8.4/main/postgresql.conf 

It witll open the file for editing, Add following line at the end of the file:

listen_addresses = 'localhost'

Save and close the file. Open another file for editing:

gksuso gedit /etc/postgresql/8.4/main/pg_hba.conf

Replace “local all all ident sameuser” with:

local   all         all                               md5

Change Password for root user

In PostGRE, root user is “postgres” which by default, does not have any password. Enter following line in terminal to set a password for it:

sudo -u postgres psql template1
ALTER USER postgres with encrypted password 'your_password';
\q

Create a new User & a new Database

sudo -u postgres createuser -d -R -P new_username
sudo -u postgres createdb -O new_username new_database_name

This will create a new user, with username “new_username” and create a new database “new_database_name” and set “new_username” it’s owner.

Configure phpPgAdmin

I assume you already installed phpPgAdmin by:

sudo apt-get install phppgadmin

Then, configure Apache:

gksudo gedit /etc/apache2/apache2.conf

Add following line at the end of the file:

Include /etc/phppgadmin/apache.conf

All done! Restart to reflect changes…

sudo /etc/init.d/apache2 restart
sudo /etc/init.d/postgresql-8.4 restart

Access phpPgAdmin

type http://localhost/phppgadmin in your browser & log in by the username you just created (new_username)

Use GUI Administration application

Run following command in terminal:

pgadmin3

Access from Terminal:

psql

References

https://help.ubuntu.com/8.04/serverguide/C/postgresql.html

http://solyaris.wordpress.com/2008/08/09/setup-postgres-in-ubuntu/

https://help.ubuntu.com/community/phpPgAdmin

Download & Install Sun Java Jdk in Ubuntu 11.04

I had to install Netbeans & JDK in some PCs with no internet connectivity. That’s why, I needed to know how to install Java JDK properly and configure it. Though I mentioned Ubuntu 11.04 version, it should work with any version of newer Ubuntu.

Download Sun  JDK

Download the .bin file from official site.

Create Installation + Configuration Scripts

  1. Copy this code to a file name install.sh and put it in the directory where you put the downloaded jdk. When I prepared this script, jdk version was 1.6.0_25, if you install an upgraded version edit line #4 accordingly in this script.
  2. Now Copy code from this link & save it in a file name jdk_config.sh, put it in the same directory where you downloaded jdk.

[Note: I copied the original script from the link provided at the end of this article]

Install “java-common”

I’ll find an offline installer of this package and add later. Till then, you can install it by :

sudo apt-get install java-common

Ready to Install!

Right click all the three files (the JDK, install.sh & jdk_config.sh) and select properties. In the Permissions tab, put check mark on “allow executing file as program”

Then open a terminal, go to this directory where all these files exist (to avoid trouble you can put all these files in your home directory) and type:

./install.sh

That’ll do 🙂

Reference:

http://ju-n.net/properly-setting-up-sun-java-5-jdk-on-ubuntu-karmic-lucid