10

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.

ManOx
  • 1,935
  • 5
  • 23
  • 37
  • if it's allowed in your hosting and php safe mode is not enabled and disabled function haven't disabled functions like system, yes! run your c program with something like system("ls -la"); via php – Vahid Farahmand Jan 30 '13 at 03:31
  • @VahidFarahmand what do you mean via php? Sorry, again, very new to all of this server stuff – ManOx Jan 30 '13 at 03:46
  • upload a php file, upload your c program, in your php do system("yourcfile"), php will run your c program in a shared hosting – Vahid Farahmand Jan 30 '13 at 03:47

2 Answers2

9

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.

7

You need to compile your C program. Linux is distributed with the GNU C Compiler (gcc). In a pinch, you can compile your program with the command line:

gcc -o myprog myprog.c

This means you need shell access. In the comments, people have suggested running shell commands via PHP with the system function. It is possible that your PHP installation has disabled this command for security reasons.

You should really ask your host whether they can provide shell access via SSH.

If your host can do that, you can run a terminal session (using PuTTY if you have Windows locally, or command-line ssh on most other systems).

So you upload your file (via FTP, SCP, or whatever), then compile it. All good so far.

Now you need your web server to allow it to run. Generally you can't execute binary files from a your web server's document root. You need to put the binary into the configured cgi-bin directory. This usually resides one directory level up from your document root.

On my system, my documents are in:

/srv/httpd/htdocs

And my cgi-bin are in:

/srv/httpd/cgi-bin

Generally the cgi-bin directory will be aliased so that it appears to be in your document root. So you could run your script via http://mysite.com/cgi-bin/myprog. You do need to have CGI enabled on your webserver. On Linux, your web server will probably be Apache.

Also, the server needs permission to run the file. That means execute permissions for the appropriate user. In a pinch:

chmod 0770 /srv/httpd/cgi-bin/myprog
chown apache:apache /srv/httpd/cgi-bin/myprog

Example is for my particular system.

This all assumes that you want to run your program and get output via HTTP. If that's not the case, simply having shell access should be enough. If you didn't understand any of this answer, then I recommend you do some serious reading about Linux, networking, and HTTP.

Here is an Apache guide for getting CGI working: http://httpd.apache.org/docs/2.2/howto/cgi.html

paddy
  • 60,864
  • 6
  • 61
  • 103
  • I did understand it, however I got this error when I tried the chown command: "chown: changing ownership of `blah/blah/blah' Operation not permitted". Also, I got the 500 internal server error when going to http://mysite.com/cgi-bin/myprog. I'm guessing that's because of the problem above. – ManOx Jan 30 '13 at 04:08
  • Don't worry about ownership for now. I am assuming you have located your cgi-bin directory, compiled your program, copied the compiled binary into there, and that the user `apache` and group `apache` exist on your system. This varies between distributions. I use Slackware, so if your webserver keeps stuff elsewhere, you'll need to go looking. – paddy Jan 30 '13 at 04:11
  • I use plesk. And actually, I'm not sure what you mean when you say that I have a apache user... I'm currently logged in as the admin for my plesk setup. And yes I've compiled it, and put it in my cgi-bin folder – ManOx Jan 30 '13 at 04:12
  • One step at a time. Forget the apache user. You can get around the permissions for now by allowing everyone to execute your file (`chmod a+rx myprog`). – paddy Jan 30 '13 at 04:13
  • hmm... still getting the 500 error when I test the webpage. when I run that command however, it doesn't give me any error – ManOx Jan 30 '13 at 04:15
  • It could be that CGI is disabled in Apache. I just did a quick google, and the internet is full of information for you. Try working through this: http://httpd.apache.org/docs/2.2/howto/cgi.html -- don't forget you'll need to resart Apache after reconfiguring it. On slackware I can do `/etc/rc.d/rc.httpd restart` - maybe plesk has something similar. – paddy Jan 30 '13 at 04:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23607/discussion-between-manox-and-paddy) – ManOx Jan 30 '13 at 04:19