Run a C Program on a Linux Server

前端 未结 2 1510
庸人自扰
庸人自扰 2021-02-03 12:21

This question I\'m sure has been answered, I honestly don\'t know how to ask it via search though. So please excuse my lack of knowledge as this one of the only place I really h

2条回答
  •  花落未央
    2021-02-03 12:29

    One way is to run it as CGI, as @paddy already mentioned. However, the program will run slow, long startup time.

    Another way is to run it using FastCGI. It will be much more faster, you just need a few modifications on your code to make it works, for example as CGI:

    #include 
    #include 
    
    int main(int argc, char **argv)
    {
        time_t timer;
        char time_str[25];
        struct tm* tm_info;
    
        time(&timer);
        tm_info = localtime(&timer);
        strftime(time_str, sizeof(time_str), "%Y/%m/%d %H:%M:%S", tm_info);
    
        /* Without this line, you will get 500 error */
        puts("Content-type: text/html\n");
    
        puts("");
        puts("");
        puts("  ");
        puts("");
        puts("");
        puts("   

    Hello world!

    "); printf("

    %s

    \n", time_str); puts(""); puts(""); return 0; }

    Compile it:

    $ # 'cgi-bin' path may be different than yours
    $ sudo gcc example.c -o /usr/lib/cgi-bin/example
    $ wget -q -O - http://localhost/cgi-bin/example
    
    
      
    
    
       

    Hello world!

    2013/01/30 08:07:29

    $

    Using FastCGI:

    #include 
    #include 
    #include 
    
    int main(int argc, char **argv)
    {
        time_t timer;
        char time_str[25];
        struct tm* tm_info;
    
        while(FCGI_Accept() >= 0)   {
            time(&timer);
            tm_info = localtime(&timer);
            strftime(time_str, sizeof(time_str), "%Y/%m/%d %H:%M:%S", tm_info);
    
            /* Without this line, you will get 500 error */
            puts("Content-type: text/html\n");
    
            puts("");
            puts("");
            puts("  ");
            puts("");
            puts("");
            puts("   

    Hello world!

    "); printf("

    %s

    \n", time_str); puts(""); puts(""); } return 0; }

    Compile it:

    $ # Install the development fastcgi package, I'm running Debian
    $ sudo apt-get install libfcgi-dev 
     ...
    $ 
    $ # Install Apache mod_fcgid (not mod_fastcgi)
    $ sudo apt-get install libapache2-mod-fcgid
     ...
    $ 
    $ # Compile the fastcgi version with .fcgi extension
    $ sudo gcc example.c -lfcgi -o /usr/lib/cgi-bin/example.fcgi
    $ # Restart Apache
    $ sudo /etc/init.d/apache2 restart
    Restarting web server: apache2 ... waiting .
    $
    $ # You will notice how fast it is
    $ wget -q -O - http://localhost/cgi-bin/example.fcgi
    
    
      
    
    
       

    Hello world!

    2013/01/30 08:15:23

    $ $ # Our fastcgi script process $ ps aux | grep \.fcgi www-data 2552 0.0 0.1 1900 668 ? S 08:15 0:00 /usr/lib/cgi-bin/example.fcgi $

    In poth programes, there is:

    puts("Content-type: text/html\n");
    

    This will prints:

    Content-type: text/html[new line]
    [new line]
    

    Without it Apache will throw 500 server internal error.

提交回复
热议问题