Run a C Program on a Linux Server

心不动则不痛 提交于 2019-12-03 07:36:49

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/14596199/run-a-c-program-on-a-linux-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!