【问题标题】:Run a C Program on a Linux Server在 Linux 服务器上运行 C 程序
【发布时间】:2013-01-30 03:29:24
【问题描述】:

这个问题我肯定已经回答了,但老实说我不知道​​如何通过搜索来问它。所以请原谅我缺乏知识,因为这是我在计算机科学领域真正缺乏知识的唯一地方之一。

我如何/是否可以在托管服务器上运行 C 程序。我可以去哪里http://mysite.com/myspecialcprogram.c 它会运行?或者更好的是,我可以在多大程度上使用像 C 这样的高级语言来为我的服务器编程?

还应该注意的是,我有一个运行 apache 的专用 Linux 机器。所以我有完全的访问权限。

【问题讨论】:

  • 如果您的主机允许并且未启用 php 安全模式并且禁用功能没有禁用系统等功能,是的!使用类似 system("ls -la"); 的东西运行你的 c 程序;通过 php
  • @VahidFarahmand 通过 php 是什么意思?再次抱歉,所有这些服务器的东西都很新
  • 上传一个php文件,上传你的c程序,在你的php do system("yourcfile")中,php会在一个共享主机中运行你的c程序

标签: c webserver hosted


【解决方案1】:

正如@paddy 已经提到的,一种方法是将其作为 CGI 运行。但是,程序会运行缓慢,启动时间长。

另一种方法是使用FastCGI 运行它。它会更快,您只需要对代码进行一些修改即可使其正常工作,例如 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;
}

编译:

$ # '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>
$ 

使用 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;
}

编译:

$ # 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
$ 

在 poth 程序中,有:

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

这将打印:

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

没有它,Apache 将抛出 500 服务器内部错误。

【讨论】:

    【解决方案2】:

    您需要编译您的 C 程序。 Linux 随 GNU C 编译器 (gcc) 一起分发。紧要关头,您可以使用命令行编译程序:

    gcc -o myprog myprog.c
    

    这意味着您需要 shell 访问权限。在 cmets 中,人们建议使用 system 函数通过 PHP 运行 shell 命令。出于安全原因,您的 PHP 安装可能禁用了此命令。

    你真的应该问问你的主机他们是否可以通过 SSH 提供 shell 访问。

    如果您的主机可以这样做,您可以运行终端会话(如果您在本地有 Windows,则使用 PuTTY,或者在大多数其他系统上使用命令行 ssh)。

    所以您上传文件(通过 FTP、SCP 或其他方式),然后编译它。到目前为止一切顺利。

    现在您需要您的网络服务器来允许它运行。通常,您无法从 Web 服务器的文档根目录执行二进制文件。您需要将二进制文件放入配置的cgi-bin 目录中。这通常位于文档根目录的上一级目录。

    在我的系统上,我的文档位于:

    /srv/httpd/htdocs
    

    我的 cgi-bin 在:

    /srv/httpd/cgi-bin
    

    一般来说,cgi-bin 目录会使用别名,以便它看起来在您的文档根目录中。所以你可以通过http://mysite.com/cgi-bin/myprog 运行你的脚本。您确实需要在您的网络服务器上启用 CGI。在 Linux 上,您的 Web 服务器可能是 Apache。

    此外,服务器需要权限才能运行该文件。这意味着适当用户的执行权限。紧要关头:

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

    示例适用于我的特定系统。

    这一切都假设您想要运行您的程序并通过 HTTP 获取输出。如果不是这种情况,只需拥有 shell 访问权限就足够了。如果您不理解此答案,那么我建议您认真阅读有关 Linux、网络和 HTTP 的内容。

    这是一个让 CGI 工作的 Apache 指南:http://httpd.apache.org/docs/2.2/howto/cgi.html

    【讨论】:

    • 我确实理解它,但是当我尝试 chown 命令时出现此错误:“chown:更改 `blah/blah/blah' 的所有权操作不允许”。此外,我在访问mysite.com/cgi-bin/myprog 时收到 500 内部服务器错误。我猜是因为上面的问题。
    • 暂时不用担心所有权。我假设你已经找到了你的 cgi-bin 目录,编译了你的程序,将编译后的二进制文件复制到那里,并且用户 apache 和组 apache 存在于你的系统上。这在分布之间有所不同。我用的是 Slackware,所以如果你的网络服务器把东西放在别处,你需要去寻找。
    • 我使用 plesk。实际上,当您说我有一个 apache 用户时,我不确定您的意思……我目前以管理员身份登录我的 plesk 设置。是的,我已经编译了它,并将它放在我的 cgi-bin 文件夹中
    • 一次一步。忘记 apache 用户。您现在可以通过允许所有人执行您的文件 (chmod a+rx myprog) 来绕过权限。
    • hmm... 测试网页时仍然收到 500 错误。但是,当我运行该命令时,它不会给我任何错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多