Like Tree1Likes
  • 1 Post By Salem

CGI/Apache2 Help Please.

This is a discussion on CGI/Apache2 Help Please. within the C Programming forums, part of the General Programming Boards category; New to C, with basic LAMP skills. Program below is compiled and runs with ./pekoe, but no longer opens with ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    14

    CGI/Apache2 Help Please.

    New to C, with basic LAMP skills. Program below is compiled and runs with ./pekoe, but no longer opens with nano editor, and I get "No such directory" when attempting to cp or mv it to cgi-bin directory (either copying existing or renaming as .cgi file), yet directory exists. Apache2 is started and cgi path is correct. I am able to open pekoe file in Home with gedit (saved copy earlier), yet I notice .c extension not attached to file name now.

    Basically I'm unable to get this file in cgi-bin. Thanks in advance for getting me started here.
    Code:
    #include <stdio.h>
    
    main(){
    printf("C CGI Apache2\n");
    return 0;
    }
    apache2/sites-available/default (I run Debian):
    Code:
    ScriptAlias /cgi-bin/ /var/www/cgi-bin/
        <Directory "/var/www/cgi-bin">
            AllowOverride None
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Order allow,deny
            Allow from all
            AddHandler cgi-script .cgi 
        </Directory>
    I'm thinking (part of) the problem here is that I've already compiled pekoe.c? How to edit, cp mv etc. file after compiling/testing? I must be missing something, but just started with this today. Thanks.
    Last edited by pavl; 01-07-2012 at 04:26 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You first make a .c file, however you want to.
    You then compile that, into a new file.
    If you have compiled it with the same name as the .c file, you've now lost your source code, because your executable just replaced the source file. If you haven't done something silly like that, then wherever your original .c file is, it should still be waiting for you.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    thanks for response. so in future, how to retrieve code from the ./ executable, when the executable gives a response, not the code?

    I made a new .c file, moved it to cgi-bin, executed it then renamed it with .cgi extension, restarted apache2 and the .cgi file CODE appears in browser, but not the executed .c file. tried executing a .cgi file on the command line which of course doesn't work.

    cgi-bin is in /var/www now, if that makes any difference as far as headers are concerned?

    thanks for more help in getting this going. it's much different than php.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't retrieve code from an executable. Any time you change your source code, you have to recompile and put the new executable wherever it needs to be.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    How to make the .c file executable as .cgi?

    I've managed to rename the .c file to .cgi and put it in the cgi-bin folder, restart apache2, and the page loads but shows the code, not the executed program.

    Here is a simple tutorial I found online by Marshall Brain. What does he mean by
    On my Web server, I entered this program into the file simplest.c
    ? I know how to compile but obviously I"m not doing it on my server, which is running btw and I'm restarting it after every change...
    The simplest possible CGI script would, upon execution, create this simple, static page as its output. Here is how this CGI program would look if you wrote it in C:



    #include <stdio.h> int main() { printf("Content-type: text/html\n\n"); printf("<html>\n"); printf("<body>\n"); printf("<h1>Hello there!</h1>\n"); printf("</body>\n"); printf("</html>\n"); return 0; } On my Web server, I entered this program into the file simplest.c and then compiled it by saying:



    gcc simplest.c -o simplest.cgi
    Last edited by pavl; 01-07-2012 at 06:39 PM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Perhaps start with something like

    gcc simplest.c -o simplest.cgi
    mv simplest.cgi /var/www/cgi-bin/

    You might need chown as well, if apache is fussy about running executable files owned by someone else.

    > I made a new .c file, moved it to cgi-bin, executed it then renamed it with .cgi extension, restarted apache2 and the .cgi file CODE appears in browser
    Because to apache, a .c file is just another text file, so all it does is simply send the content to the browser (which displays it as simple text).

    > thanks for more help in getting this going. it's much different than php.
    Yes.
    One of the things which you'll notice with being "new to C" are the vast number of security holes you can put into code without even realising it.
    I hope this box of yours isn't facing the public internet, otherwise you might be learning some other things as well (like where is a good place to keep my reinstall disks for repeated use).

    Also on security, I hope you're not editing / compiling / surfing whilst logged in as root.
    quzah likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    Thanks Salem, got it working.
    Quote Originally Posted by Salem View Post
    Also on security, I hope you're not editing / compiling / surfing whilst logged in as root.
    I log in my machine as user, never root, but I log in terminal as su to start apache2, which is running while I edit/compile these c/cgi files and surf the net. Is this a problem? I was thinking of getting apache going in a vm in virtualbox and doing my C experiments there. Is that a better idea?

    thanks in advance. really appreciate it.
    Last edited by pavl; 01-08-2012 at 09:29 AM.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Well it's a better idea in the sense that you only need one physical box on/under your desk.
    Plus the snapshot feature means you can easily revert to a known good state with very little effort.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    14

    bad header

    compiled this as
    gcc 1.c -o 1.cgi and ./1.cgi runs in terminal, then cp 1.cgi file to cgi-bin, restart apache, and getting internal server error. log file says malformed header from script. bad header...suggestion please?
    Code:
    #include <stdio.h>
    
    main(){
    printf("Hello\n");
    return 0;
    }
    Last edited by pavl; 01-08-2012 at 11:06 AM.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Suggestion 1 is google your error messages
    https://encrypted.google.com/search?...pt.+bad+header

    Suggestion 2 is use the code in post #5, which outputs valid HTML.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    got it working, thanks.

    how to properly delete .c and .cgi files? I just rm them in terminal but notice they stick around when doing locate command, even though they don't show when doing ls command.

    regarding html, can we include multiple tags/elements <h><li><a> etc. in the same printf line? If not, is there a framework/template etc. you might recommend to make html easier?
    Last edited by pavl; 01-08-2012 at 01:00 PM.

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    ok so that works. forum must time out editing a post after x minutes.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21