0
votes

I have configured cgi-bin and trying to run c++ object file but instead of displaying results on browser the executable file is getting downloaded.

Steps I have followed:

Steps to configure cgi-bin in public_html and public_html. Run following commands in terminal

  1. $ sudo a2enmod cgi
  2. $ sudo a2enmod cgid
  3. $ sudo a2enmod userdir
  4. $ sudo service apache2 restart
  5. $ mkdir ~/public_html
  6. $ cd ~/public_html
  7. $ mkdir cgi-bin
  8. $ cd /etc/apache2
  9. $ sudo vim sites-available/000-default.conf
  10. Add following text in file:


ScriptAlias /cgi-bin/ /home/*/public_html/cgi-bin/
 <Directory "/home/*/public_html/cgi-bin">
         AllowOverride None
         Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
         SetHandler cgi-script
         Order allow,deny
         Allow from all
 </Directory>
  1. $ sudo service apache2 restart

After this I have created a c++ file with this code

#include <iostream>

using namespace std;

int main ()
{

    cout << "Content-type:text/html \n\n";
    cout << "Hello World - First CGI Program";

    return 0;
}

Compiled and linked it using this

g++ -o example example.cpp

When I am running it on browser http://localhost/~username/public_html/cgi-bin/example

The example object file is getting downloaded instead of showing result on browser.

1
This seems like a simple walk-through of configuring cgi-bin that may have your answer.Ken Y-N
(Excuse my comment deletion!) Perhaps the problem is the executable bit is not set for all users - does chmod a+x example make a difference or is it already rwxrwxr-x or the like?Ken Y-N
Yes it was permissions problem, now executable files are working fine on browsers. thanks.Navneet Ojha

1 Answers

1
votes

The Script was not correctly written in default configuration file. Otherwise the steps given in question are all correct and works fine. That code needs to be written inside VirtualHost tag given in the configuration file.