[Note] 2023-03-13 C++/Cgi mac os demo

mac os 自带 Apache server,用其来托管 C++ 编译后的 cgi 程序

修改配置

修改配置文件
$ sudo vim /etc/apache2/httpd.conf
取消这里的注释

<IfModule mpm_prefork_module>
        LoadModule cgi_module libexec/apache2/mod_cgi.so
</IfModule>

修改这里的 OptionsExecCGI

<Directory "/Library/WebServer/CGI-Executables">
    AllowOverride None
    Options ExecCGI
    Require all granted
</Directory>

解开这里的 handler (可以添加其他文件后缀名)

AddHandler cgi-script .cgi

启动

# or sudo apachectl start
$ sudo apachectl restart

查看请求日志

$ tail -f /var/log/apache2/error_log

准备展示文件

hello-cgi.cpp

#include <iostream>

int main() {

  std::cout << "Content-type:text/html\r\n\r\n";
  std::cout << "<html>\n";
  std::cout << "<head>\n";
  std::cout << "<title>Hello World</title>\n";
  std::cout << "</head>\n";
  std::cout << "<body>\n";
  std::cout << "<h2>Hello World</h2>\n";
  std::cout << "</body>\n";
  std::cout << "</html>\n";

  return 0;
}

编译 g++ hello-cgi.cpp -g -Wall -std=c++14 -o hello-cgi.cgi
移动到 web-server 目录 sudo mv hello-cgi.cgi /Library/WebServer/CGI-Executables
访问 http://localhost/cgi-bin/hello-cgi.cgi

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP...
    一川烟草i蓑衣阅读 645评论 0 0
  • 学习Python有一段时间了,在对语法基本了解的基础上,想进行一些较高程度的编程训练。在网上找到了www.runo...
    小鸟快跑了阅读 895评论 0 0
  • Apache 与 Tomcat 配置 Apache 配置(Linux) 安装sudo apt-get instal...
    wswenyue阅读 4,126评论 0 7
  • 在mac上配置CGI:搞了两大天,遍寻个大论坛贴吧谷歌百度,教程倒是一大把,但都是这在之前几年的旧东西,现在的鬼技...
    京哥阅读 3,022评论 6 6
  • 你是否想使用Python语言创建一个网页,或者处理用户从web表单输入的数据?这些任务可以通过Python CGI...
    Kean_Qi阅读 1,077评论 3 0