Man*_*nOx 9 c webserver hosted
这个问题我肯定已经回答了,老实说我不知道如何通过搜索来询问它.所以请原谅我缺乏知识,因为这是我在计算机科学领域缺乏知识的唯一地方.
如何在托管服务器上运行C程序是否可行.我可以去哪里http://mysite.com/myspecialcprogram.c它会运行?或者更好的是,我可以在多大程度上使用像C这样的高级语言为我的服务器编程?
还应该注意的是,我有一个运行apache的专用Linux盒子.所以我有完全访问权限.
小智 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;
}
Run Code Online (Sandbox Code Playgroud)
编译它:
$ # '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>
$
Run Code Online (Sandbox Code Playgroud)
使用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;
}
Run Code Online (Sandbox Code Playgroud)
编译它:
$ # 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
$
Run Code Online (Sandbox Code Playgroud)
在poth程序中,有:
puts("Content-type: text/html\n");
Run Code Online (Sandbox Code Playgroud)
这将打印:
Content-type: text/html[new line]
[new line]
Run Code Online (Sandbox Code Playgroud)
没有它,Apache将抛出500服务器内部错误.
你需要编译你的C程序.Linux随GNU C编译器(gcc)一起发布.在紧要关头,您可以使用命令行编译您的程序:
gcc -o myprog myprog.c
Run Code Online (Sandbox Code Playgroud)
这意味着您需要shell访问权限.在评论中,人们建议通过PHP使用该system函数运行shell命令.出于安全原因,您的PHP安装可能已禁用此命令.
您应该问您的主机是否可以通过SSH提供shell访问.
如果您的主机可以这样做,您可以运行终端会话(如果您在本地使用Windows,则使用PuTTY,或者在大多数其他系统上使用命令行ssh).
所以你上传你的文件(通过FTP,SCP或其他),然后编译它.到目前为止都很好.
现在您需要Web服务器才能运行它.通常,您无法从Web服务器的文档根目录执行二进制文件.您需要将二进制文件放入已配置的cgi-bin目录中.这通常位于文档根目录的一个目录级别.
在我的系统上,我的文件在:
/srv/httpd/htdocs
Run Code Online (Sandbox Code Playgroud)
我的cgi-bin在:
/srv/httpd/cgi-bin
Run Code Online (Sandbox Code Playgroud)
通常,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
Run Code Online (Sandbox Code Playgroud)
示例适用于我的特定系统.
这一切都假定您要运行程序并通过HTTP获取输出.如果不是这样,那么只需拥有shell访问就足够了.如果你不理解这个答案,那么我建议你做一些关于Linux,网络和HTTP的认真阅读.
以下是获取CGI工作的Apache指南:http://httpd.apache.org/docs/2.2/howto/cgi.html