c/c++语言开发共享在Linux服务器上运行C程序

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

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

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

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

    另一种方法是使用FastCGI运行它。 它会更快,你需要对代码进行一些修改才能使它工作,例如CGI:

    #include  #include  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/htmln"); puts(""); puts(""); puts(" "); puts(""); puts(""); puts(" 

    Hello world!

    "); printf("

    %s

    n", time_str); puts(""); puts(""); return 0; }

    编译它:

     $ # 'cgi-bin' path may be different than yours $ sudo gcc example.c -o /usr/lib/cgi-bin/example $ wget -q -O - https://localhost/cgi-bin/example      

    Hello world!

    2013/01/30 08:07:29

    $

    使用FastCGI:

     #include  #include  #include  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/htmln"); puts(""); puts(""); puts(" "); puts(""); puts(""); puts(" 

    Hello world!

    "); printf("

    %s

    n", time_str); puts(""); puts(""); } 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 - https://localhost/cgi-bin/example.fcgi      

    Hello world!

    2013/01/30 08:15:23

    $ $ # 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/htmln"); 

    这将打印:

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

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

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

     gcc -o myprog myprog.c 

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

    您应该问您的主机是否可以通过SSH提供shell访问。

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

    所以你上传你的文件(通过FTP,SCP或其他),然后编译它。 到目前为止都很好。

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

    在我的系统上,我的文件在:

     /srv/httpd/htdocs 

    我的cgi-bin在:

     /srv/httpd/cgi-bin 

    通常,cgi-bin目录将具有别名,以使其看起来位于文档根目录中。 所以你可以通过https://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

    需要了解更多c/c++开发分享在Linux服务器上运行C程序,也可以关注C/ C++技术分享栏目—计算机技术网(www.ctvol.com)!

      以上就是c/c++开发分享在Linux服务器上运行C程序相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/980180.html

      (0)
      上一篇 2021年12月13日
      下一篇 2021年12月13日

      精彩推荐