First CGI Program

Our first program uses C++ to calculate \(11+21\) and embed the obtained output into an html code. This will prove that our installation of the software and customizations of the server are working.

  • Use your terminal to ssh into the server and enter the folder /var/www/html/test01 with the command
    cd /var/www/html/test01
  • Use the editor nano create the source file of your first C++ program. We will choose the name progTest01 for our source file.
    nano progTest01.cpp
  • Type the following code in the editor nano. Once you are done, click on Ctrl+x and then choose y to save the file.
    int main(){
       std::cout<<"<!DOCTYPE html>\n";
       std::cout<<"<body> <h1> Hello CGI / C++ </h1> <p>Hi</p><p>Hi again!</p>";
       int x;
       int y;
       x=11; y=21; int z; z=x+y;
       std::cout<<"<p>The sum of the numbers "<<x<<" and "<<y<<" is "<<z;
       std::cout<<" </p>\n";
       return 0;
  • Compile the source code into a binary with the extension cgi. This is a way to do that
    c++ progTest01.cpp -o myWebsite.cgi
  • Open the browser and type the following in the URL field