Run a C Program on a Linux Server

ManOx picture ManOx · Jan 30, 2013 · Viewed 14.2k times · Source

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 have a lack of knowledge in the world of Computer Science.

How can I/ Is it possible, to run a C program on a Hosted Server. To where I could go to http://mysite.com/myspecialcprogram.c and it would run? Or better yet, to what extent can I use a high level language like C to program for my server?

It should also be noted that I have a Dedicated Linux box running apache. So I have full access.

Answer

user1129665 picture user1129665 · Jan 30, 2013

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 <stdio.h>
#include <time.h>

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("<!DOCTYPE html>");
    puts("<head>");
    puts("  <meta charset=\"utf-8\">");
    puts("</head>");
    puts("<body>");
    puts("   <h3>Hello world!</h3>");
    printf("   <p>%s</p>\n", time_str);
    puts("</body>");
    puts("</html>");

    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
<!DOCTYPE html>
<head>
  <meta charset="utf-8">
</head>
<body>
   <h3>Hello world!</h3>
   <p>2013/01/30 08:07:29</p>
</body>
</html>
$ 

Using FastCGI:

#include <fcgi_stdio.h>
#include <stdio.h>
#include <time.h>

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("<!DOCTYPE html>");
        puts("<head>");
        puts("  <meta charset=\"utf-8\">");
        puts("</head>");
        puts("<body>");
        puts("   <h3>Hello world!</h3>");
        printf("   <p>%s</p>\n", time_str);
        puts("</body>");
        puts("</html>");
    }

    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
<!DOCTYPE html>
<head>
  <meta charset="utf-8">
</head>
<body>
   <h3>Hello world!</h3>
   <p>2013/01/30 08:15:23</p>
</body>
</html>
$
$ # 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.