Python CGI in Apache httpd server

Pre-requisite: Working Apache Server.

 

In httpd-vhosts.conf:

Add below content inside <VirtualHost …>

ScriptAlias /cgi-bin/ "/cgi-bin/" 
<directory "<httpd-installed-path="">/cgi-bin/">
         Options Indexes FollowSymLinks ExecCGI
         AddHandler cgi-script .cgi .py
         AllowOverride None
         Require all granted 

Now create a new file ‘test.py’ in /cgi-bin/  with content:

#!/usr/bin/env python

import cgi
cgi.test()

Make sure the below line is not commented in httpd.conf

LoadModule cgi_module modules/mod_cgi.so

Start/Restart apache server.

You can verify the loaded cgi module using the command:

sudo [path/]apachectl -M | grep cgi

Try below url in web-browser:

http://<ip/hostname>:/cgi-bin/test.py

eg:
http://localhost:1025/cgi-bin/test.py
http://localhost:80/cgi-bin/test.py

You will get a page with details on current working directory, command line arguments, etc..

Configure VMs which talk to each other.

Hi,

This blog will walk you through setting up three different fedora VMs : ram, daisy and rincy; who will be talking to each other and also with the host machine; very soon…

Pre-requirements:

  • 3VMs already setup inVirtualBox.
    • See mine below. Name doesn’t matter much. You may use your own. My VMs run fedora OS. Depending on your OS, you may have to Google a little more on how to set different configurations. I am sure, Google will be happy to help you.

Screen Shot 2015-03-15 at 9.29.44 pm

 

A little help with my vocabulary:

host machine : machine which runs VirtualBox
VM           : Virtual Machine

First we need a Host-Only Adapter. This needs to be created in the Virtual Box and not in any particular VMs. So lets do that first.

Go to the preference of VirtualBox. Mine is a Mac. Don’t confuse with the screenshots if you are using another host OS.

Screen Shot 2015-03-15 at 8.49.51 pmGo to the “Network” tab and choose “Host-only Networks” sub-tab and create a new Adapter by clicking on the green plus sign you see on the right side of the window. There comes the new adapter “vboxnet0”

Screen Shot 2015-03-15 at 8.50.23 pm

Double-click “vboxnet0” or click on the screw-driver icon on the right side of the window after highlighting “vboxnet0”  to see it’s settings. Below are the default values and we are fine with it unless you really want to change it. You can see the “Lower Address Bound and “Upper Address Bound”. We can choose IP from this range for our machines.

Screen Shot 2015-03-15 at 8.51.04 pm

Press “OK” if you made some change else “OK” or “Cancel” as you like and we have a working “Host-only Adapter” now.

Now we should connect it to all three VMs.

Highlight a VM and click “Settings” (on the top of the window between “New” and “Show”)

Screen Shot 2015-03-15 at 8.51.41 pm

Select the “Network” tab from the window which appeared. Leave “Adapter 1” as we need it to be Attached to “NAT” for accessing internet via host machine. Select “Adapter 2” and select “Host-only Adapter” option from the drop down menu of “Attached to:”. Ofcourse the value for “Name:” is our newly created “vboxnet0”.

Screen Shot 2015-03-15 at 8.52.14 pm

Set “Promiscuous Mode:” to “Allow All” or “Allow VMs”. Putting it to “Deny” will stop any communication via this Adapter. Hope you too don’t want that as our intention is otherwise.

Screen Shot 2015-03-15 at 8.52.39 pm

Now we need to boot into the VMs and assign a static-IP for each.

I will start will “ram” VM.

In RedHat/Fedora we can use “system-config-network” for this purpose.

Install it using the below command. You should have root privilege or sudo privilege for this.Screen Shot 2015-03-15 at 9.18.47 pmOnce installed, run “system-config-network” command to start assigning the static IP.

Screen Shot 2015-03-15 at 9.19.24 pmThat will take you to something like this:

Screen Shot 2015-03-15 at 9.19.34 pm

Highlight “Device Configuration” from it using arrow keys and press “Enter/Return” key.

Screen Shot 2015-03-15 at 9.19.53 pmWe need to edit “vboxnet0”, so highlight it. If you don’t see that, you may highlight “” and press Enter.

Make sure your settings match the below ones. You may choose any IP from the range we saw earlier. I go for 192.168.56.[101-103]. Need not be adjacent ones.

Use tab and arrow keys to highlight the “OK” button and press Enter after the changes.

Screen Shot 2015-03-15 at 9.20.03 pmClick “Save”.

Screen Shot 2015-03-15 at 9.20.26 pm

Then “Save&Quit”

Screen Shot 2015-03-15 at 9.20.37 pm

With this, we have assigned static-IP 192.168.56.101 to VM: ram.

Now lets do a small trick to ease our path ahead; a small DNS resolution.

Lets use the names ram, daisy and rincy instead of the IPs.

For that, open the /etc/hosts file.

Screen Shot 2015-03-15 at 9.21.30 pmAnd add below lines to it.Screen Shot 2015-03-15 at 9.21.51 pm

Like wise, assign 192.168.56.102 to daisy and 192.168.56.103 to rincy and update the /etc/hosts file.

Now reboot all the VMs to be on the safer side. and try the below commands in all three VMs and host machine.

ping -c 3 ram
ping -c 3 daisy
ping -c 3 rincy

You will get the below output which means rincy, ram and daisy started talking to each other..

Screen Shot 2015-03-15 at 9.23.12 pmTry from the host machine and you will see Big-Daddy can talk to his children now..

Screen Shot 2015-03-15 at 9.24.52 pmSo there comes an end to this blog..

LDAP_CONTROL_RELAX undeclared while installing python-ldap

ERROR:

Modules/constants.c: In function ‘LDAPinit_constants’:
Modules/constants.c:158: error: ‘LDAP_OPT_DIAGNOSTIC_MESSAGE’ undeclared (first use in this function)
Modules/constants.c:158: error: (Each undeclared identifier is reported only once
Modules/constants.c:158: error: for each function it appears in.)
Modules/constants.c:380: error: ‘LDAP_CONTROL_RELAX’ undeclared (first use in this function)
error: command ‘gcc’ failed with exit status 1

 

Solution:
Install openldap24-libs & openldap24-libs-devel :
sudo yum install openldap24-libs-devel
sudo yum install openldap24-libs

Run the below commands and get the unique list of directories from the output:
Add those folders to setup.cfg file in the below section:
[_ldap]
library_dirs = /opt/openldap-RE24/lib /usr/lib
include_dirs = /opt/openldap-RE24/include /usr/include/sasl /usr/include

Now run the installation command:
python setup.py install

apachectl status – How to solve lynx: command not found

apachectl status” will not work if you don’t have lynx ; text-based web browser.

You realize that when you get below message while using the command.

Error
bin/apachectl: line 95: lynx: command not found

Install the lynx and tell it to Apache httpd, he will surely understand.

Download the lynx from here. Change your working directory to the extracted folder and run the command:

./configure --prefix=<path-to-install-lynx>
make
make install

During lynx installation, it may stop with an error:

configure: error: No curses header-files found

Run the below command before proceeding:

sudo yum search ncurses-devel

Now it’s time to tell httpd about lynx.

In apachectl file (either in bin or conf directory), change the line starting with LYNX=”lynx -dump” to LYNX=”<lynx-installed-path>/lynx -dump”

apachectl status will start working..

Apache httpd: APR not found; error while installation

During Apache httpd installation if you get below error, that means Apache Portable Run-time environment was not found or not installed.

checking for APR... no
configure: error: APR not found.  Please read the documentation.

and/or

checking for APR-util... no
configure: error: APR-util not found. Please read the documentation.

So install APR and APR-util and give httpd those paths. Httpd will be happy to run for you after that… 🙂

Go further if you want to know how to install APR.

You can download APR and APR-util from here.

Then extract those to separate folders.

First change to the extract APR directory and install it. (Tip: cd command to change directory)

sudo ./configure --prefix=<apr-path>

sudo make

sudo make install

So you have a working APR. Next is APR-util.

Change to the extract APR-Util directory and install it. (Tip: cd command to change directory)

sudo ./configure --prefix=<apr-util-path> --with-prefix=<apr-path>

sudo make

sudo make instal

Install zlib packages with the command (optional; you can turn this off at your will):

yum install zlib zlib-devel

Apart from APR and APR-Util, you may need to install pcre too. So download it from here first.

Change to the directory and use the below commands to install.

sudo ./configure --prefix=<pcre-path>

sudo make

sudo make install

Now lets make httpd understand that we have a working pcre, APR and APR-util.

./configure --enable-file-cache --enable-cache --enable-disk-cache --enable-mem-cache --enable-deflate --enable-expires --enable-headers --enable-usertrack --enable-cgi --enable-vhost-alias --enable-rewrite --enable-so --prefix=<httpd-path> --with-apr=<apr-path> --with-apr-util=<apr-util-path> --with-pcre=<pcre-path>

# Rest is history make make install

Change the Java (JVM) used by tomcat.

Enterprise systems will have different versions of Java installed in it. A system administrator may have to set up Tomcat to use a particular one out of it. By default Tomcat uses the OS default; ie. the one installed into OS path. So how can we make it run with another version of Java.

This is easily achieved using the below commands(Open a terminal and run the below commands, don’t close it.):

export JAVA_HOME=<path-to-java>

or

export JRE_HOME=<path-to-java>

Run the start-up script from the same terminal and you will see from the logs that Tomcat picked up the Java from the path you set.

Notes:

JRE_HOME is set to JAVA_HOME if not set. So setting any of these variables suffice.

Shared folder for Fedora VM in VirtualBox

Hi,

I found a little hard to setup a shared folder for my VMs. So thought of sharing it..

So, assuming you already have your host machine, VirtualBox installed on it and a VM running Fedora (any linux), there we go..

Highlight your VM in VirtualBox window.

Screen Shot 2015-03-17 at 3.40.41 pmYes, it is powered off. Bring up the settings window for the VM clicking the below icon.

Screen Shot 2015-03-17 at 3.41.02 pmChoose the “Shared Folders” tab and you will see the list of folders shared with VM.Screen Shot 2015-03-17 at 3.41.22 pmClick on the plus sign you see on the left side and you get another window like the one below.

Screen Shot 2015-03-17 at 3.41.44 pmYou may choose the folder of the host machine you want to share and tick the “Auto-mount” option. Click OK.

Screen Shot 2015-03-17 at 4.12.42 pm

The folder you chose to share is now visible and we are done with the VirtualBox settings. Click OK to come out of it.

Screen Shot 2015-03-17 at 4.13.05 pm

Now boot up the VM. We need to install a few packages in Fedora for this. You may use below command to install the same.

Screen Shot 2015-03-17 at 5.11.50 pmThis will install latest gcc and kernel-devel packages. Guest addition may get arrogant, at times and gives error that it can’t find a particular version of kernel-devel. I will explain how to solve that later.

Now we can install “Guest Addition”

Click on the “Devices” menu and select the below highlighted option.Screen Shot 2015-03-17 at 4.15.35 pmSome OS automatically run the autorun script from CD. Otherwise open a file-browser and open the CD contents. You will see “autorun.sh” file there. Drag and drop it to Terminal. It will first authenticate you and start installing “Guest Addition”.

Screen Shot 2015-03-17 at 4.59.33 pmScreen Shot 2015-03-17 at 4.53.26 pm

You will get below output 99% of the times and that means just a reboot and you have a host-folder available in VM.

Screen Shot 2015-03-17 at 5.26.14 pm

The shared folder will be auto-mounted in /media.

Screen Shot 2015-03-17 at 5.30.02 pmThe 1% I left earlier is the case when “Guest Addition” fails to install with an error about the kernel-devel version. You may get below screen in such cases.

Screen Shot 2015-03-17 at 4.54.16 pmIn such cases you get a command from the above output.

yum install kernel-devel-3.17.4-301.fc21.x86_64

Just run the below part out of it and do the “Guest Addition” installation again and you are good to go..

yum install kernel-devel-3.17.4

Now let me take you through another issue you may face. Your folder will get listed in /media. But you may not have permission to open it. In my case, “Shared” is the shared-folder. /media/sf_Shared is the path in VM where it is mounted.

ls /media/sf_Shared

The above command will give an output like this:Screen Shot 2015-03-18 at 9.48.20 pm

This is because of the difference of UID in host and VM machines.

Screen Shot 2015-03-18 at 9.24.30 pm

The easiest way to sort this out is to add the user account to vboxsf.

usermod -a -G vboxsf

I went for a reboot (of VM) after that and things worked well.

May be you want to do this too.

Create a link in your home directory to access the shared directory mounted in /media. Make sure your home directory does not have “Shared” folder already.

ln -s /media/sf_Shared ~/Shared

So now you may use ~/Shared as your window to the host-machine..

Clone vdi image (.vdi) without error

Clone vdi image (.vdi) without the UID error.

Use the below command via terminal.

VBoxManage clonevdi <ImageYouWantToCopy.vdi> <DestinationImage.vdi>

If you get below error, that means one of your VM is using the .vdi image. Shutdown that VM and run the command again.

VBoxManage: error: Failed to lock source media 'ImageYouWantToCopy.vdi'

VBoxManage: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component Medium, interface IMedium, callee nsISupports

VBoxManage: error: Context: "CloneTo(dstDisk, ComSafeArrayAsInParam(l_variants), NULL, progress.asOutParam())" at line 740 of file VBoxManageDisk.cpp

C++ build tool: boost bjam intro

Reference Material : HERE

Source code (hello_sree.cc):

Screen Shot 2014-02-26 at 2.56.26 PM

build file (jamroot.jam or Jamroot): Screen Shot 2014-02-26 at 7.00.22 PM

Now in terminal, change the present working directory to the folder where the code and build-file resides. Run any of the below commands:

Screen Shot 2014-02-26 at 7.01.05 PMORScreen Shot 2014-02-26 at 7.00.45 PMORScreen Shot 2014-02-26 at 7.01.52 PMORScreen Shot 2014-02-26 at 7.03.09 PM

Ensure you have a space between “hello_sree.cc” and “;” in the build file. Otherwise you will get the below error:Screen Shot 2014-02-26 at 7.03.23 PM

Screen Shot 2014-02-26 at 7.03.33 PM

You will get this message at the successful run:Screen Shot 2014-02-26 at 7.04.01 PMYou will see a bin folder with a hierarchy of folders and executable file created as part of successful build of our program

Linux Device Driver: 1 of n

Lets try for a “hello world” Linux driver this time. Yeah, I know there are heck lot of tutorials dealing with the same but since I am trying it out myself, I would like to share the steps I followed and the experience I gained out of it.
Drivers, not just in Linux but in all Operating Systems, lie directly on top of hardware, abstracting the device specific operations. This reduces the dependency of Operating System on hardware and thereby it makes easy for an operating system to switch between different hardware.
Didn’t get that? Let me try that once more. Before that let me tell you one fact: devices are categorized into three sets in Linux (this one is specific to Linux and Unix):

  1. Character Devices    – devices which read one byte at a time.
  2. Block Devices        – devices which a block (usually 512 bytes) at a time.
  3. Network Devices        – devices which enables transfer(to be specific, send and receive) of packets over network.

By categorizing devices, Linux expects driver of each category to expose a standard interface. Now what is the use of that? Suppose two devices dev1 and dev2 have its own specific driver driv1 and driv2. If both drivers has a common interface (functions through which it gives out its services), say both implemented f1() and f2() functions, then a program using driv1 one will call f1() and f2() to use the dev1. Suppose at one point of time the user wants to replace dev1 with dev2; since the driver driv2 too follows the standard and has implementation of f1() and f2(), there will be no change in the calling process and it need not be recompiled. Thereby we could isolate the changes to a very small section. This is a good programming practice even outside driver-programming.
In this post, we will try to write our first driver which can be used with Linux. Driver-programming is a little bit different from our usual programming. Program execution in our everyday programming starts in ‘main’ function but in driver-programming main function disappears. Lets see our hello-world without any further ado.

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE(“Dual BSD/GPL”);

static int hello_kernel_init(void)
{
    printk(KERN_ALERT “Hello Kernel, at your service..\n”);
    return 0;
}

static void hello_kernel_exit(void)
{
    printk( KERN_ALERT “Goodbye Kernel, I am no longer at your service..\n”);
}

module_init(hello_kernel_init);
module_exit(hello_kernel_exit);

Don’t worry about the weird function calls: printk. Hope you already got the idea about module_init and module_exit. Those are to register our functions (hello_kernel_init and hello_kernel_exit) as functions to be called at the time of module initialization and exit. Good question: “when does that happen”. A driver module is invoked as part of any software requesting for a hardware service. That request from application goes to operating system and o/s (short form for operating system) loads the driver module specific to that hardware to serve the application. Leave those topics for the time being, we will cover it in coming chapters. ‘printk’ is just a ditto of printf function in C language; the difference is it has an identifier -> KERN_ALERT at the starting of first argument which tells the kernel the priority of the message printed by that printk function. There are other similar identifiers like KERN_INFO, KERN_WARNING, etc . Driver-modules are not supposed to use any library functions other than those implemented in kernel. This is because we wont link our code with libraries at the time of compilation. Linking and loading is the job of kernel. So when kernel links our module, if it sees any reference to functions which it doesn’t implement, it will cause a failure of our code, if not an entire system failure at rare cases.
Forgot to tell you that my development environment is Ubuntu with kernel version 3.2.0
Now I need a makefile to ease my compilation. This simple code of-course should have a simple make-file as well and there it goes:
obj-m := hello_kernel.o
Yes, just one line and the kernel build system will take care of the rest.
At the time of ‘making’ we should give the path of our kernel source. To find that we should firt find the kernel version you are currently running on. Run ‘uname -r‘ at your terminal.

unamer

You will get something like “3.2.0-40-generic”, don’t worry if it’s just numbers. Now the kernel source tree path will be “/lib/modules/3.2.0-40-generic/build”. This will be a soft link to original path and this will do for us.
Now we have our source code in “hello_kernel.c” and make file “Makefile”.
Now run the command
make -C /lib/modules/3.2.0-40-generic/build/ M=`pwd` modules
This command starts by changing its directory to the one provided with -C option, which is your kernel source directory and finds the kernel’s top-level make-file. The M= option is to help the make utility to traverse back to our current directory to build target: modules

make
Now our module is ready to be loaded. Run the below commands.

insmodlsmodrmmod

You need to have sudo privilege since we are messing with kernel. insmod will load the driver module, lsmod will list all the loaded modules and we are grepping our module from the result and rmmod removes the driver.

Hope you notice that we had a few printk statements in our code but nothing appeared in the console. It is because all kernel specific logs go to “/var/log/syslog” file in Ubuntu. Likewise different Linux distros will have its own specific file. You will see whatever we printed out in our driver code in those file. Just run the below command in our case:

cat /var/log/syslog | tail

log

There ends our first step towards mastering Linux Device Drivers. Wait till the next chapter for advanced topics..