Deploying Django in Apache using mod_wsgi

June 27th, 2009

Deploying Django in Apache using mod_wsgi is the most recommended one when compared to using mod_python. Deploying Django in apache is not very difficult, its simple with quite simple steps. Here i have created a helloworld project and explained how to deploy it in apache. You must install mod_wsgi apache module before trying this.

Step 1: Create an directory named apache in your project directory. And add two files one for conf and another a wsgi application in it.

$mkdir apache

$cd apache

$touch apache_django_wsgi.conf

$touch dj_hello.wsgi

Step 2: Open the apache_django_wsgi.conf file with a text editor(gedit)and add the following contents

WSGIScriptAlias /dj “/path/to/us/project/folder/helloworld/apache/dj_hello.wsgi”

<Directory “/path/to/ur/project/folder/helloworld/apache”>

Allow from all

</Directory>

Here, helloworld is the name of the project

Step 3: Open the dj_hello.wsgi with a text editor(gedit) and add the following contents

import os, sys

#Calculate the path based on the location of the WSGI script.

apache_configuration= os.path.dirname(__file__)

project = os.path.dirname(apache_configuration)

workspace = os.path.dirname(project)

sys.path.append(workspace)

#Add the path to 3rd party django application and to django itself.

sys.path.append(‘/usr/lib/python2.5/site-packages/django/’)

os.environ['DJANGO_SETTINGS_MODULE'] = ‘helloworld.settings’

# project settings file

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()


Step 4: Put an entry in your apache configuration fine (httpd.conf or apache2.conf depending upon the destro you are using)

Include “/path/to/ur/project/folder/helloworld/apache/apache_django_wsgi.conf”

helloworld, is the name of the django project that i am deploying in django. After doing all the steps don’t forget to restart the apache service for the changes to take effect.

django, Linux, Python , , , ,

IP Bonding or Teaming in Linux – RHEL 5

June 24th, 2009

IP Bonding or Teaming is a method of combining all the network interfaces togather into one with one of the types like Network Fault Tolerance, Round Robin, Back up, Loadbalancing etc. So it looks virtually as a single interface to the outside world providing high availability, based on the type you have set. Means, in a Network fault tolerant type one will take over if other fails and in a Load Balancing type trafic is shared when one busy to the other.

I got a chance of doing IP bonding in a HP Proliant Servers running RHEL 5. Everthing went well with no issues. Let me share my experience with my blog readers. In linux IP bonding is quite simple and is done with some kernel modules and no extra package is to be installed. In Windows, bonging or teaming is done with some software. With HP server, it can easily be done with some HP Network Configuration Uitlities.
Two establish IP bonding you need a minimum of two nework interfaces

Step 1:Check your network interfaces

#ethtool eth0
Settings for eth0:
Supported ports: [ TP MII ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Advertised auto-negotiation: Yes
Speed: 100Mb/s
Duplex: Full
Port: MII
PHYAD: 1
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: g
Wake-on: g
Current message level: 0×00000007 (7)
Link detected: yes

#ethtool eth1
Settings for eth1:
Supported ports: [ TP MII ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Advertised auto-negotiation: Yes
Speed: 100Mb/s
Duplex: Full
Port: MII
PHYAD: 1
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: g
Wake-on: g
Current message level: 0×00000007 (7)
Link detected: yes

Step 2: Check all the requiured kernel modules are available(bonding and mii)

#modprobe –list | grep bonding

/lib/modules/2.6.18-92.el5/kernel/drivers/net/bonding/bonding.ko

# modprobe –list | grep mii

/lib/modules/2.6.18-92.el5/kernel/drivers/net/mii.ko

Step 3: Editing the modprobe.conf file

#vim /etc/modprobe.conf

and add the following lines at the end

alias bond0 bonding

options bond0 mode=1 arp_ip_target=192.168.52.1 arp_interval=200 primary=eth0

To know more about the parameters to be used here use the command

#modinfo bonding

filename: /lib/modules/2.6.18-92.el5/kernel/drivers/net/bonding/bonding.ko

author: Thomas Davis, tadavis@lbl.gov and many others

description: Ethernet Channel Bonding Driver, v3.2.4

version: 3.2.4

license: GPL

srcversion: DB2ABCD47A83F8567EBE92B

depends:

vermagic: 2.6.18-92.el5 SMP mod_unload gcc-4.1

parm: max_bonds:Max number of bonded devices (int)

parm: miimon:Link check interval in milliseconds (int)

parm: updelay:Delay before considering link up, in milliseconds (int)

parm: downdelay:Delay before considering link down, in milliseconds (int)

parm: use_carrier:Use netif_carrier_ok (vs MII ioctls) in miimon; 0 for off, 1 for on (default) (int)

parm: mode:Mode of operation : 0 for balance-rr, 1 for active-backup, 2 for balance-xor, 3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, 6 for balance-alb (charp)

parm: primary:Primary network device to use (charp)

parm: lacp_rate:LACPDU tx rate to request from 802.3ad partner (slow/fast) (charp)

parm: xmit_hash_policy:XOR hashing method: 0 for layer 2 (default), 1 for layer 3+4 (charp)

parm: arp_interval:arp interval in milliseconds (int)

parm: arp_ip_target:arp targets in n.n.n.n form (array of charp)

parm: arp_validate:validate src/dst of ARP probes: none (default), active, backup or all (charp)

parm: fail_over_mac:For active-backup, do not set all slaves to the same MAC. 0 of off (default), 1 for on. (int)

module_sig: 883f35048175a9e6e24e25c96667c37112449509f5739ebf283efa5295d315b73cee5e956f6e25709cbcf2826571c5ffc20f2d87672bb921d610d7

Step 3: Now load the modules

<!– @page { size: 8.27in 11.69in; margin: 0.79in } P { margin-bottom: 0.08in } –>

#modprobe bonding

#modprobe mii

This will create a bond0 config file in the networks-scripts directory

Step 4: Editting the three configuration files for eth0,eth1,bond0

|# vim /etc/sysconfig/network-scripts/ifcfg-bond0

DEVICE=bond0

BOOTPROTO=none

ONBOOT=yes

NETMASK=255.255.255.0

IPADDR=192.168.52.4

USERCTL=no

GATEWAY=192.168.52.1

TYPE=Ethernet

IPV6INIT=no

PEERDNS=yes

#vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

BOOTPROTO=none

ONBOOT=yes

MASTER=bond0

SLAVE=yes

USERCTL=no

TYPE=Ethernet

#vim /etc/sysconfig/network-scripts/ifcfg-eth1

DEVICE=eth1

BOOTPROTO=none

ONBOOT=yes

MASTER=bond0

SLAVE=yes

USERCTL=no

TYPE=Ethernet

Step5: Restart your networks service

#/etc/init.d/network restart

#ifconfig

Now use ifconfig command to check the, you will look a new interface called bond0 has been created.  The ip you set for it is going to be your ip address of the system. Enjoy bonding

Bash, Linux, Linux Networking , , ,

Fun with Forkbombs

May 25th, 2009

Try this small shell script in your terminal. To have more fun remote login to others desktops and try this command in the terminal.

:( ){ :| :& };:

This is a strange command in linux which crashes the system. Let me explain what exactly happens when this command is executed.

  • :( ) – Function header where : is the name of the function(Colon and two parenthesis)
  • { Opens the body of the function
  • A blank space is more important here, to have a good syntax
  • : recursive call to the same function(Colon)
  • |- pipes the out put of one function call to another
  • : which calls the function again (Colon)
  • & Forks, creates a child process and assigns the function call to it
  • } Closes the function definition
  • ; delimiter to end the function definition (Semicolon)
  • : actual call to the function(Colon)

So the above script calls recursively itself twice and forks it to a child process indefinitly. At a point the memory gets full and the system crashes. You can gaurd yourself from forkbombs by restricting the number of process that can be executed at a time. Have fun with fork bombs…

Bash, Linux ,

Some Frequently used Linux Commands

May 24th, 2009

Here are some of the frequently and very useful linux commands

Command Description
top A dynamic task list
ps -e To view the current snapshot of proccess at a time
kill <process-id> Kill a process by specifying its id. Process id can be found from the previous command
killall <process-name> Kill a process with its process name. The process name can be found using ps -e command
fdisk -l Displays the partion table and list of all the memory devices connected to the system
mount /dev/<device-name> /path/to/mountpoint Used to mount a external or internal memory device to the system. The device name can be found from the previous command.
mount -t ntfs-3g /dev/<device-name> /path/to/mountpoint -o force To force mount a memory device which uses ntfs file system
sudo nautilus To open the file browser in root mode. Normally when u open the file browser in ubuntu it will not be in root mode. Provided the currnet user is a sudoer
locate <file-name-pattern> Used to search or find files by their names. Normally the search is done based on a index database. It is good to update the database before searching to get recently created files.
updatedb Used to update the file index database
cat <filename> Display the contents of a file
cat <filename> | grep <pattern> The output of the first command is given as input to the next command using a pipe. Grep command display the lines that has pattern text

Use,   man<command-name> to get more details about all the above commands

Bash, Linux ,

Tips to Increase Laptop Battery Life

March 22nd, 2009
    1. Dont always use the laptop with in A/C power. Switch off the adapter when the battery is fully charged (use it in battery mode) and switch on the adapter again when the battery level goes very low.
    2. Keep your WIFI radio switch always off when not required.
    3. Drain your battery completely at least twice a month. (Make the laptop get shutdown automatically when the battery is critically low, switch it on again and keep in BIOS page some time to drain it completely, till it gets switched off).
    4. Unplug un-used devices connected via USB (like camera, joystick, external keyboard, mobile, external hard disk etc) when not required.
    5. Avoid using StandBy/Sleep option rather use Hibernate option.
    6. Use the right adapter to charge your laptop battery.
    7. Temperature is a silent killer, temperature reduces the life of battery for all electronic devices (even mobile phones). Take measure to avoid over heating of your laptop. (People who play high graphics games rather keeping your laptop in a flat surface leave a gap between your laptop and the surface).
    8. Use your LCD screen with low brightness as possible.
    9. Do not pack the laptop in your carry case as soon as you shut down. Give a little time to cool.
    10. Other less significant tips are, avoid using high graphics screen savers, reduce the start up process as much as possible, delete the unwanted back ground process, use power options in the operating system efficiently.

      All the above tips, I have put with my own experience. Generally lithium ion batteries have a life of 2-3 years. People who use there laptops for more hours a day, take little care to have a good battery life. If no care the battery wont last more than a year.

      Laptop

      Installing Ruby on Rails in Ubuntu 8.04(Hardy)

      January 31st, 2009

      Ruby on Rails is a web application programmers based on Ruby. It helps the programmers creat best web applications easily.

      Here are the steps to set up Ruby on Rails in your Ubuntu or Debian based systems

      Step 1: First step is to install some inportant packages that are recured,

      sudo apt-get -y install build-essential libssl-dev libreadline5-dev zlib1g-dev

      Step 2: Install ruby

      RUBY="ruby-1.8.6-p111" //Current stable version

      wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/$RUBY.tar.gz

      tar xzf $RUBY.tar.gz cd $RUBY

      ./configure --prefix=/usr/local --with-openssl-dir=/usr --with-readline-dir=/usr –with-zlib-dir=/usr

      make clean

      make

      make install

      make install-doc

      make clean will clear any make files in that folder, previosly created that was configured without right options Checking whether ruby is installed properly

      ruby -ropenssl -rzlib -rreadline -e "puts :success"

      Step 3: Install Gem

      Gem is a installer similar to apt, for ruby and ruby related packages

      RUBYGEMS="rubygems-1.0.1" //Current stable version
      wget http://rubyforge.org/frs/download.php/29548/$RUBYGEMS.tgz
      tar xzf $RUBYGEMS.tgz
      cd $RUBYGEMS
      /usr/local/bin/ruby setup.rb
      	

      Step 4: Install Rails

      gem install rails //This will install rails successfully.

      Or gem install <paths to rails gem file>

      Main Errors during Installation:

      Error 1:

      /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require’: no such file to load — zlib (LoadError)

      using the following step for configuring ruby, that is before make, will remove this problem

      ./configure --prefix=/usr/local --with-openssl-dir=/usr --with-readline-dir=/usr –with-zlib-dir=/usr

      Error 2:

      no such file to load — mysql

      undefined symbol: rb_Digest_MD5_Init – /usr/local/lib/ruby/1.8/i686-linux/digest/md5.so (LoadError)

      Installing the mysql dev package and mysql library for ruby will clear this error,

      sudo apt-get install libmysqlclient15-dev

      sudo gem install mysql

      Applications, Ubuntu

      Installing XMMS in Ubuntu Hardy

      January 6th, 2009

      XMMS2 is not a replacement to XMMS in the Ubuntu Hardy (8.04)  repo. There is an offical ubuntu xmms pacakage, unfortunately wich is not available in Ubnuntu repo. We have to manually download and install it. You can download from the link

      https://launchpad.net/ubuntu/hardy/i386/xmms/1:1.2.10+20070601-1build2

      There are both soruce packages and ubuntu build deb packages in the above link…
      But inturn dont mistake that installing XMMS2 from the repo is the XMMS player. After intallation there may be some combatibility problems. But there wont be any probleming in playing songs.

      Applications, Ubuntu ,

      Samba File sharing between two ubuntu systems in LAN

      November 3rd, 2008

      With two systems connected in LAN is so easy to share files and folders in Ubuntu. Here i tell you how to share files and folders with samba file server between two ubuntu systems with few clicks. The Ubuntu Iam using here is Hardy.

      The system that has source contents to be shared should have Samba server, (That can be installed in course of the procedure how to share). And the remote system that wish to access the source contents should have the Samba client. Samba client will installed be default in ubuntu, if not install it with the command

      $sudo apt-get install smbclient

      Here comes the procedure how to share.

      Soruce Side

      1. On the source system login to ubuntu, right click on the folder you wish to share and select ‘Sharing Options’.

      2. In the Dialog box that apears select ‘Share this Folder’ check box, you will be prompted to install a service. Clicking on Install will open synaptic package manager and install the Samba Server packages.

      3. On successful Installation of the packages, the part in the server side is over.

        This will create a share automatically in Samba server on the the username you have logged in (here ‘rosario’) with its password.

      Destination Side

      1. Check whether the smbclient package is installed, if not install it with the above command.

      2. Use the command like
      3. $sudo smbclient \\\\192.168.1.33\\photos -U rosario

        • where rosario is the username of the source system,
        • 192.168.1.33 is the IP address of the source system
        • photos is the name of the folders shared in the source system(In small characters)
      4. The above command first will prompt the local system password as sudo is used. And then it will prompt for the password of the remote samba server for the username rosario
      5. On giving the right password, it will take to the samba promt.

      Smb: \>

      Some usefull commands

      • help- To know all possible commands
      • dir- To list out all the shared directories, or contents inside the current directory
      • cd- Change Directory
      • get- To copy a specified file from the source system to the local system
      • put- To copy contents from local system to the server system

      Enjoy Easy File Sharing With Ubuntu…

      Linux Networking, Ubuntu ,

      Vedio Recording using webcam in any Linux Distro

      October 14th, 2008

      For vedio recording in linux, mplayer alone may not be enough, one additional package that comes with mplayer is needed. Its mencoder. To install mencoder in a debian based distro use the command

      $sudo apt-get install mencoder

      For other distros like redhat you can use relevant tools like YUM to install mencoder.

      Then for vedio recording follow the commad below

      Video Recording Without Sound

      $ mencoder tv:// -tv driver=v4l2:width=640:height=480:device=/dev/video0 -nosound -ovc lavc -o filename.avi

      Video recording With Sound

      $ mencoder tv:// -tv driver=v4l2:width=640:height=480:device=/dev/video0:forceaudio:adevice=/dev/dsp -ovc lavc -oac mp3lame -lameopts cbr:br=64:mode=3 -o filename.avi

      To change/increase the rate of frames captured use the fps option of the above command.

      Use it like

      -fps 24

      Rock with you webcam….

      Bash, Linux

      Set GRUB Password after the Installation of any Distro

      October 1st, 2008

      If you forgot to set the password during the time of installing the linux distribution, and if you feel to set the password for your grub boot loader. Then here is the answer…

      Switch to root using ‘su’ command and execute ‘grub’ command in terminal/konsole. Prompt will change to ‘grub>’
      execute md5crypt to generate password hash.

      grub> md5crypt

      it will prompt you for password, enter the password which you are going to set for GRUB. it will display encrypted password hash.

      Password: *******
      Encrypted: $1$X2jZi$KeCC6NuRp0BJDt32jCvnw0

      note down encrypted password hash or copy it and exit grub mode using the grub command.

      grub> quit

      Now edit the
      edit /boot/grub/menu.lst file and insert encrypted password above the “title” lines or insert near commented examples of password line.

      password –md5 <password-hash>
      ## ## End Default Options ##
      title        Ubuntu 8.04.1, kernel 2.6.24-19-generic

      save edited file and reboot.

      Bash, BootLoader, Ubuntu ,