Question

How to run c programmes in browser using apache2 in ubuntu 14.04 ?

Posted September 5, 2014 6.3k views

Hi, I am basically from electronics background, but i have got interest in programing for web application, i am very new to it. Find very interesting and moreover i am enjoying it.
I installed apache2 on ubuntu, i hosted some virtual host sites with index.html files.
but i want to run c programme on browser. i went through cgi configuration for apache2, but was not successful. when i try for any /home/*/public_html/cgi-bin/ for DocumentRoot. apache2 gives DocumentRoot not found error, and get 403 and 404 error when try to access on browser. So how to run c executable file on browser using apache2 in ubuntu 14.04.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

2 answers

To enable cgi-bin run:

sudo a2enmod cgi
sudo service apache2 restart

Then copy your executable to the directory /usr/lib/cgi-bin/ Assuming the name of your program is hello.cgi it will now be available at http://your.ip.address/cgi-bin/hello.cgi

  • Thank you, how the configuration on apache looks like, my current one is this, but it says DocumentRoot not found.

    <VirtualHost *:80>      
    
            ServerAdmin webmaster@localhost
    
            #DocumentRoot /var/www/html
    
            DocumentRoot /home/suhas/public_html/cgi-bin
            <Directory /home/suhas/public_html/cgi-bin>
                    AllowOverride None
                    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                    Order allow,deny
                    AddHandler cgi-script .cgi
                    Allow from all
            </Directory>
    
    
    
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    
            #Include conf-available/serve-cgi-bin.conf
    
    </VirtualHost>
    

    i am getting this error.

    * Restarting web server apache2                                                                                                                AH00112: Warning: DocumentRoot [/home/suhas/public_html/cgi-bin] does not exist
    AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 192.168.4.6. Set the 'ServerName' directive globally to suppress this message
    
  • Does the folder /home/suhas/public_html/cgi-bin actually exist? Even so, you probably don’t want that as the document root? You probably want something like:

                    DocumentRoot /home/suhas/public_html/
                    ScriptAlias /cgi-bin/ /home/suhas/public_html/cgi-bin
                    <Directory "/home/suhas/public_html/cgi-bin">
                            AllowOverride None
                            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                            Require all granted
                    </Directory>
    
[deleted]
  • Thanks for this explanation!

    I would like to add that those modifications worked once I applied in the right file

    /etc/apache2/sites-available/000-default.conf
    

    After I read other tutorials I made the modifications in

    /etc/apache2/apache2.conf
    

    but they never worked. Also, trying mod-fcgi I applied the modifications there and in this file

    /etc/apache2/mods-available/fcgid.conf
    

    But it did not work either. Now cgi and fcgi are working fine because the configuration is in the right place.

    The <VirtualHost *:80> in your example hint me where to look for.

    Thanks!

    Nikolai

Submit an Answer