Install and Enable CGI-BIN on Apache Server for Ubuntu

It’s time to go a little old-school and lay out how to enable the Common Gateway Interface (CGI) for your Apache server. You might be wondering why we need CGI. It’s simple: CGI allows the web server to interact with external programs. These programs can range in purpose and scope, but primarily they help to add dynamic content to a website. Most often these applications are Perl programs with the extension .pl, but CGI execution is not limited to Perl.

Get started with CGI

One of the stumbling blocks most people come up against with CGI is getting their Apache server to recognize the CGI directory and to allow for the execution of commands from within that directory. I will demonstrate this process on a Ubuntu 11.04 server running the latest LAMP stack. For the most part, this LAMP stack is default, so a fresh install of LAMP will do just fine.

Let’s first talk about the directories. If you look in the /var/www (the document root of Apache), you will find a sub-directory called cgi-bin. This is not where your Perl programs and other various files will be placed. Within the /usr/lib/ directory, you will find another cgi-bin directory; it is the repository for your executables. If that directory does not exist, create it with the command: 

$ sudo mkdir /usr/lib/cgi-bin.

Now, make sure the permissions look like this:
drwxr-xr-x  3 root root         4096 2011-11-23 09:08 cgi-bin
Issue the command ls -l /usr/lib | less and scroll to check your directory’s permissions. If it’s not as you see above, issue the following commands:
$ sudo chmod 755 /usr/lib/cgi-bin

$ sudo chown root.root /usr/lib/cgi-bin
Now that your directory is ready, it’s time to configure Apache. Remember, this is Ubuntu, so you will have to make a few simple modifications if you’re using a different distribution with a different take on the Apache web server.

Configure Apache

A directive must be created so Apache knows about CGI — where its directories are located and what it can do. In some Apache configurations, this is done within the httpd.conf file. Because this is Ubuntu, we are going to add the directive to /etc/apache2/apache2.conf
$ sudo nano /etc/apache2/apache2.conf
Open that file with your favorite text editor and, before you add anything, search for this section:
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

<Directory "/usr/lib/cgi-bin">

AllowOverride None

Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

Order allow,deny

Allow from all

If the above section does not exist, add it in the <Virtualhost *:80> section just above the line:
ErrorLog ${APACHE_LOG_DIR}/error.log
Save that file and be ready to restart Apache. To restart the server, issue the command:
$ sudo service apache2 restart

Test it out

Now it’s time to test this baby out. Let’s create a file (call it test.pl), and the file’s contents should be:
#!/usr/bin/perl -w
print "Content-type: text/htmlrnrn";

print "Hello there!<br />nJust testing .<br />n";

for ($i=0; $i<10; $i++)


print $i."<br />";

Save that file in /usr/lib/cgi-bin and give it 755 permissions. Now open that file in your web browser (point the browser to http://IP_OF_SERVER/cgi-bin/test.pl where IP_OF_SERVER is the address of the server), and you should see the following:
Hello there!
Just testing .
If you see that, CGI is working. You can dump your programs into the /usr/lib/cgi-bin/ directory and start using them It’s simple and effective.

Linux: Howto Make a Directory Command

How do I make directory under Linux operating systems using the command prompt or bash shell?

You need to use the mkdir command to create new folders or directories under Linux operating systems. A directory (also known as folder in MS-Windows/OS X) is nothing but a container for other directories and files.

mkdir command Syntax

The mkdir command has the following syntax:

mkdir dirname
mkdir dirname1 dirname2
mkdir [option] dieNameHere
mkdir -p dir1/dir2/dir3


Open a terminal and use the mkdir command to create empty directories. The following command would create a directory called foo:
$ mkdir foo
To list directories, enter:
$ ls
$ ls -l

The following command would create two directories within the current directory:
$ mkdir tom jerry
$ ls -l

The -p option allows you to create parent directories as needed (if parent do not already exits). For example, you can create the following directory structure:
$ mkdir -p ~/public_html/images/trip

Sample mkdir demo

Animated gif 01: mkdir in action under Linux / Unix like operating systems

Animated gif 01: mkdir in action under Linux / Unix like operating systems

How To Use chmod and chown Command

How do I use chmod and chown command under Linux / Unix operating systems?
Use the chown command to change file owner and group information. Use the chmod command to change file access permissions such as read, write, and access.

chown command

chown command changes the user and/or group ownership of for given file. The syntax is:
chown owner-user file
chown owner-user:owner-group file
chown owner-user:owner-group directory
chown options owner-user:owner-group file


First, list permissions for demo.txt, enter:
# ls -l demo.txt
Sample outputs:
-rw-r--r-- 1 root root 0 Aug 31 05:48 demo.txt
In this example change file ownership to vivek user and list the permissions, run:
# chown vivek demo.txt
# ls -l demo.txt

Sample outputs:
-rw-r--r-- 1 vivek root 0 Aug 31 05:48 demo.txt
In this next example, the owner is set to vivek followed by a colon and a group onwership is also set to vivek group, run:
# chown vivek:vivek demo.txt
# ls -l demo.txt

Sample outputs:
-rw-r--r-- 1 vivek vivek 0 Aug 31 05:48 demo.txt
In this example, change only the group of file. To do so, the colon and following GROUP-name ftp are given, but the owner is omitted, only the group of the files is changed:
# chown :ftp demo.txt
# ls -l demo.txt

Sample outputs:
-rw-r--r-- 1 vivek ftp 0 Aug 31 05:48 demo.txt
Please note that if only a colon is given, or if NEW-OWNER is empty, neither the owner nor the group is changed:
# chown : demo.txt
In this example, change the owner of /foo to “root”, execute:
# chown root /foo
Likewise, but also change its group to “httpd”, enter:
# chown root:httpd /foo
Change the owner of /foo and subfiles to “root”, run:
# chown -R root /u
  • -R – Recursively change ownership of directories and their contents.

chmod command

The information about the chmod command is covered in our previous tutorial – “how to use change user rights using chomod command“.

How to Reinstall apache2 properly in linux

If you have issues with apache2 and you want to reinstall it but you are getting errors,then here it isthe solution for you.
For example, me i had an issue with apache, when i tried to start it i got this error message  :
root[email protected]:/etc/apache2# /etc/init.d/apache2   start
.: 45: Can't open /etc/apache2/envvars
Now i want to reinstall apache, but first i need to remove it completely  (If you currently have it removed improperly, reinstall it by “sudo apt-get install apache2″ before to use the command bellow). To do that i will use this command :
 sudo apt-get remove --purge apache2 apache2-utils
This command will completely remove all apache2 configuration files and directories.
– Reinstall again apache using the normal command
sudo apt-get install apache2

Now your config files and directories in /etc/apache2 all be back and at their defaults as well as the “apache2-utils previously removed.

[email protected]:/etc/apache2# /etc/init.d/apache2   start
 * Starting web server apache2                                 
using for ServerName
httpd (pid 7235) already running   [ OK ]

How to Find a File in Ubuntu Linux

Use the following commands in your terminal

  • find / -iname filename or partial filename
    Find a File in Linux Step 1Bullet1.jpg
  • find / -iname *.conf

    Find a File in Linux Step 1Bullet2.jpg
  • This will find every instance conf no matter where it is.
  • The / after find tells find to look in every directory below and including the root of the filesystem.
  • The – in front of name tells Linux to not worry about caps.

You can use wildcards such as find / -iname wiki* to find, for example, “wikiHow.dat”.

Find a File in Linux Step 2.jpg
  • There are many variables you can use with find.

Type “man find” or “info find” at a terminal prompt for more information then you care to know. Skip the ” ” when entering the commands.

Find a File in Linux Step 3.jpg
  • If the list of files is extensive, you can pipe the command to “less” so you can scroll back for forth.
    • find / -iname *.conf | less
  • If you want to send the search results into a file to be read later
    • find / -iname *.conf > ~/myfile
The tilde ~ represents your home directory so if your user name is Joe you can find the filemyfile in /home/joe.
  • The locate command will often find files much faster, but it’s not always up to date.
    • locate important-paper
    • locate conf

Rename File Command Line Ubuntu Linux

You need to use the mv command. It is used to rename and move files and directories. The general syntax is as follows:
mv old-file-name new-file-name
mv [options] old-file-name new-file-name
mv file1 file2
In this example, the following command would rename a file called resumezzz.pdf to resume.pdf. Open a command-line terminal (select Applications > Accessories > Terminal), and then type:
mv resumezzz.pdf resume.pdf
If resumezzz.pdf is located in /home/vivek/docs/files directory, type:
cd /home/vivek/docs/files
mv resumezzz.pdf resume.pdf
mv /home/vivek/docs/files/resumezzz.pdf /home/vivek/docs/files/resume.pdf
Use the ls command to view files:
ls -l file1
ls -l file1 file2
ls -l /home/vivek/docs/files/*.pdf
ls -l *.pdf

Linux rename a file syntax

In short, to rename a file:
mv file1 file2
You can get verbose output i.e. mv command can explain what is being done using the following syntax:
mv -v file1 file2
Sample outputs:
`file1' -> `file2'
To make mv interactive pass the -i option. This option will prompt before overwriting file:
mv -i file1 file2
Sample outputs:
mv: overwrite `file2'? y

Detailed information about mv command

You can also view the manual page on mv using the following command:
man mv
info mv

Changing screen resolution from command line in Ubuntu and Debian VM

So I needed to change the default resolution on a Debian VM that I was working on, and I couldn’t find the place to change the monitor resolution size settings. I found an easy terminal command as the fix.

Run these in command line terminal :

this will display available resolutions. Then you can perform the following:

xrandr -s 1024×768 This would change the screen resolution to 1024×768 (provided your monitor supports 1024×768)

How to rename files in Linux Ubuntu

To rename files in Ubnutu it is very easy. Use the command for the desired filename name to be renamed.

mv desktop-login.ogg desktop-009.ogg
“mv” as in “move”, there’s no renaming tool as moving file has the same effect. Execute in the same directory where the files are, or use full paths:
mv /path/to/desktop-login.ogg /path/to/desktop-009.ogg