在Linux服务器上运行C程序

10

我相信这个问题已经有答案了,但我不知道如何通过搜索来提问。所以请原谅我的无知,因为这是我在计算机科学领域唯一缺乏知识的地方之一。

我是否可以在托管服务器上运行C程序?比如我可以访问http://mysite.com/myspecialcprogram.c并且程序能运行。或者更好的办法,我能用C这种高级语言编写我的服务器程序吗?

值得注意的是,我有一个专用的Linux服务器,正在运行apache,因此我拥有完全的访问权限。


如果您的主机允许并且php安全模式未启用,同时禁用函数也未禁用system等功能,那么您可以通过类似于system(“ls -la”)的方式,通过PHP运行您的C程序。 - Vahid Farahmand
@VahidFarahmand 你说的是通过 PHP 实现什么?抱歉,我对服务器方面的所有内容都非常陌生。 - ManOx
上传一个 PHP 文件,上传你的 C 程序,在你的 PHP 文件中运行 system("yourcfile") 命令,PHP 将在共享主机上运行你的 C 程序。 - Vahid Farahmand
2个回答

9

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

另一种方法是使用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
$ 

在许多编程语言中,都有以下内容:

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

这将打印出以下内容:
Content-type: text/html[new line]
[new line]

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

7

您需要编译C程序。Linux自带GNU C编译器(gcc)。如果必要,可以使用命令行编译您的程序:

gcc -o myprog myprog.c

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

您应该向您的主机提供商询问是否可以通过SSH提供shell访问权限。

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

因此,您需要上传文件(通过FTP、SCP或其他方式),然后编译它。到目前为止一切都很好。

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

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

/srv/httpd/htdocs

我的 cgi-bin 目录在:

/srv/httpd/cgi-bin

通常cgi-bin目录将被设置别名,以便它显示为在您的文档根目录中。因此,您可以通过http://mysite.com/cgi-bin/myprog运行脚本。您需要在Web服务器上启用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: changing ownership of `blah/blah/blah' Operation not permitted"。同时,当我访问http://mysite.com/cgi-bin/myprog时,出现了500内部服务器错误。我猜这是由于上述问题导致的。 - ManOx
现在不用担心所有权问题。我假设您已经找到了cgi-bin目录,编译了程序,并将编译后的二进制文件复制到其中,而且用户“apache”和组“apache”在您的系统上存在。这在不同的发行版中可能会有所不同。我使用Slackware,因此如果您的Web服务器将文件保存在其他位置,您需要去查找。 - paddy
我使用 Plesk。实际上,当你说我有一个 Apache 用户时,我不确定你的意思是什么... 我目前以我的 Plesk 设置管理员身份登录。是的,我已经编译它并将其放在了我的 cgi-bin 文件夹中。 - ManOx
一步一个脚印。忘记Apache用户。您可以通过允许每个人执行您的文件(chmod a+rx myprog)来暂时绕过权限。 - paddy
测试网页时仍然出现500错误。但是当我运行该命令时,没有任何错误提示。 - ManOx
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接