我相信这个问题已经有答案了,但我不知道如何通过搜索来提问。所以请原谅我的无知,因为这是我在计算机科学领域唯一缺乏知识的地方之一。
我是否可以在托管服务器上运行C程序?比如我可以访问http://mysite.com/myspecialcprogram.c并且程序能运行。或者更好的办法,我能用C这种高级语言编写我的服务器程序吗?
值得注意的是,我有一个专用的Linux服务器,正在运行apache,因此我拥有完全的访问权限。
我相信这个问题已经有答案了,但我不知道如何通过搜索来提问。所以请原谅我的无知,因为这是我在计算机科学领域唯一缺乏知识的地方之一。
我是否可以在托管服务器上运行C程序?比如我可以访问http://mysite.com/myspecialcprogram.c并且程序能运行。或者更好的办法,我能用C这种高级语言编写我的服务器程序吗?
值得注意的是,我有一个专用的Linux服务器,正在运行apache,因此我拥有完全的访问权限。
一种方法是将其作为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]
您需要编译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
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
chmod a+rx myprog
)来暂时绕过权限。 - paddy