**TASK 2: MY 2nd BLOG POST**

A.HOW TO MAKE YOUR PC A WEB SERVER?

B.HOW TO RUN A C++ PROGRAM THROUGH CGI?

A. THE Ist PART……….

WEB SERVER: A web server is an information technology that processes requests via HTTP, the basic network protocol used to distribute information on the World Wide Web. The term can refer either to the entire computer systemor specifically to the software that accepts and supervises the HTTP requests.

I am here going to do it on ubuntu..
INSTALLATION PROCESS

GO to the TERMINAL.

GIVE Commands like these to install APACHE2(A Web Server)

sudo apt-get update

sudo apt-get install apache2

IT WILL TAKE SOME TIME TO INSTALL..

You can check whether its instaled properly or not by typing the command

service apache2 statusScreenshot from 2016-02-11 20:37:30

You can check it other way too by typing localhost or your IP ADDRESS in any browser,then it will show this page

Screenshot from 2016-02-11 20:39:15

THAT’S IT, its so simple within 2 steps… your system is a WEB BROWSER now

B.NOW, THE 2nd PART…

There are many ways to configure apache2 under different directories,one way is explained here:

By default apache2 is stored under etc folder in ubuntu

  • cd  /etc/apache2
  • ls
  • cd sites available ‘or’ cd conf-enabled
  • ls
  • cat 000-default.conf ‘or’ cat serve-cgi-bin.conf
  • cd /usr/lib/cgi-bin

 

 

IMPORTANT:

For giving PERMISSIONS to your cgi bin and to your saved program you need to give these commands other wise you can not save your program under CGI-BIN

sudo a2enmod cgi

sudo/etc/init.d/apache2 restart

sudo service apache2 restart

CGI: It stands for common gateway interface.Its a standard way for web server to pass web user’s request to an application program and to receive data back to forward to the user

STEP1: PROGRAM

U need to save your program in cgi-bin folder so that it can run on LOCALHOST……..

The common MIME(multipurpose internet mail extensions) types are text/plain for unformatted text and text/html for HTML formated content.

Short for Multipurpose Internet Mail Extensions, a specification for formatting messages so that they can be sent over the Internet. Many e-mail clients now support MIME, which enables them to send and receive graphics, audio, and video files via the Internet mail system.

Screenshot from 2016-02-11 21:21:05

this is a simple program written using html content too.

now , i had saved this program with name second.cgi..so

in terminal type these commands now

STEP2: EXECUTION

Screenshot from 2016-02-11 21:25:34

STEP3: DISPLAY THROUGH CGI

Screenshot from 2016-02-19 15:37:21

or you can change the background colour by using this instruction in program

cout<< “<body bgcolor= blue> “

Screenshot from 2016-02-19 16:01:51

Now, TO RUN A C++ USER INPUT PROGRAM THROUGH CGI………

What’s new here is the part after the “?”. The information after the “?” character is known as a query string. When the server is passed a URL with a query string, it calls the CGI program identified in the first part of the URL (before the “?”) and then stores the part after the “?” in the environment variable QUERY_STRING. A Query String, by its name suggest that it is a string used to ‘Query’. The query string begins after the first ‘?’ character. Query strings are usually used to pass a list of variables

The following is a CGI program called fact.cpp that uses query information supplied by the form. (form is dicussed after this)

#include <iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int main()
{
string value, s; // declaration
s = getenv(“QUERY_STRING”); // s stores the query string extracted from URL
int a =s.length(); // a stores length of query string
int b =s.find(“=”); // b stores the index number or simply number where “=” is found in the string
value = s.substr(b+1,a); // value stores the value part of the string specified after “=”
double fact = 1;
double factorial;
int n= atof(value.c_str()); // atof() converts string to int which gets stored in n

for (int i=1; i<=n; i++) // for loop for calculating factorial
{
fact= fact*i;
}
factorial = fact;

cout<< “Content-Type : text/html \n\n”;

cout<< “<html> <head> <title> factorial </title> </head>”;

cout<< “<body bgcolor= green> “;

cout<< “<h2 color= blue> Factorial of “<< n <<” is: “<< factorial << “</h2>”;
cout<< ” </body> </html>”;
return 0;
}

 

 

This program is placed in /usr/lib/cgi-bin

Compile it using the command :
g++ fact.cpp -o fact.cgi
This creates an executable file fact.cgi in /usr/lib/cgi-bin
Make sure, fact.cgi has executable permissions. If not, Change the permissions of this file as :
chmod 777 fact.cgi

Explanation:Every file and folder contain a 8-bit data that control the permissions. At its basic binary form, it will be “000”, which means no permissions of any form is granted. When you set a “Read” permission, it will add 4-bit to the data, making it “100” (in binary format) or a “4” in the usual decimal format. Setting a “Write” permission will add 2-bit to the data, making it “010” and “2” in decimal form. Lastly, setting an “Execute” permission adds 1-bit to the data, which will result in “001”, or “1” in decimal form. In short:

  • Read is equivalent to ‘4’.
  • Write is equivalent to ‘2’.
  • Execute is equivalent to ‘1’

When we want to set permissions, we just add up the number. For example, to set the permissions to read and write, we will use ‘6’ (4 + 2) for the permission. For read, write and execute, we will use ‘7’ (4 + 2 + 1) for the permission. Here’s the different permutation:

0 – no permission
1 – execute
2 – write
3 – write and execute
4 – read
5 – read and execute
6 – read and write
7 – read, write, and execute

now,
Make sure the cgi-bin directory has executable permissions. If not, change them by executing the chmod command in the directory /usr/lib as:
chmod 777 cgi-bin

Form supplies the query string to this CGI program. The following is the program, named as form.cpp that contains the form in CGI format.

#include<iostream>
using namespace std;

int main()
{
cout<<“Content-Type: text/html \n\n”;
cout<<” <html><head><title>C++</title></head>”;
cout<<“<body bgcolor = green ><form action=/cgi-bin/fact.cgi method=get><p> <h3>Enter any Number</h3></p><input type=text name=n><p><h4><input type = submit value= Submit ></h4> </p></form></body></html>”;
return 0;
}

This program is also placed in /usr/lib/cgi-bin
Compile it using the command :
g++ form.cpp -o form.cgi
This creates an executable file form.cgi in /usr/lib/cgi-bin
Make sure, form.cgi has executable permissions. If not, Change the permissions of this file as :
chmod 777 form.cgi
Some of these commands may require root permissions. For this, use sudo before the command.

Screenshot from 2016-02-17 21:30:16Screenshot from 2016-02-17 21:30:20

NOTE..

GIVE DATATYPE LONG OR DOUBLE INSTEAD OF INT . SO, THAT UA PROGRAM CAN SHOW OUTPUT UPTO MORE NUMBERS……

Another program is to print a table…

#include <iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int main()
{
string value, s; // declaration
s = getenv(“QUERY_STRING”); // s stores the query string extracted from URL
int a =s.length(); // a stores length of query string
int b =s.find(“=”); // b stores the index number or simply number where “=” is found in the string
value = s.substr(b+1,a); // value stores the value part of the string specified after “=”
int c=1,table;
int n= atof(value.c_str()); // atof() converts string to int which gets stored in n
int m=10;
cout<< “Content-Type : text/html \n\n”;

cout<< “<html> <head> <title> table </title> </head>”;
cout<< “<body bgcolor= green> “;
for (int i=0; i<=m; i++) // for loop for calculating factorial
{
c=n*i;
table=c;
cout<< “<h2 color= blue>”<< i <<” * “<<n<<“=”<< table << “</h2>”;}

cout<< ” </body> </html>”;
return 0;
}

 

and let tb here is one which supplies QUERY STRING to CGI.. its saved as tb.cgi

#include<iostream>
using namespace std;

int main()
{
cout<<“Content-Type: text/html \n\n”;
cout<<” <html><head><title>C++</title></head>”;
cout<<“<body bgcolor = red ><form action=/cgi-bin/tab.cgi method=get><p> <h3>Enter any Number</h3></p><input type=text name=n><p><h4><input type = submit value= Submit ></h4> </p></form></body></html>”;
return 0;
}

Screenshot from 2016-02-19 20:08:29Screenshot from 2016-02-19 20:08:34

STRUGGLES:

  1. Didn’t know first about give permissions to cgi folder and program.
  2. TERMINAL is case sensitive. do write commands properly
  3. check the location where cgi-bin is stored
  4. got to know litttle bit about html by doing this task as i m not familiar with this language.

 A presentation copy of  this project is available on

presentation on reveal.js

THANKS…….. 🙂

Advertisements

One thought on “**TASK 2: MY 2nd BLOG POST**

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s