您的位置:首页 > 运维架构 > Linux

CentOS 6.4平台下C++(cgi-bin)开发Web Server(Hello world)

2017-12-11 20:59 561 查看
服务器配置:

yum install httpd
vim  /etc/httpd/conf/httpd.conf


httpd.conf文件中所有和cgi-bin相关的内容:

Listen 80
DocumentRoot "/var/www/cgi-bin"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>

<Directory "/var/www/cgi-bin">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

<Directory "/var/www/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>

<Directory /var/www/cgi-bin/>
Options Indexes FollowSymLinks ExecCGI
AllowOverride None
allow from all
</Directory>


/var/www/cgi-bin目录下文件如下:

# tree
.
|-- a.cgi
|-- index.html
|-- server.cpp
`-- sh.cgi


其中,a.cgi是server.cpp编译后生成的文件。

g++ server.cpp -o a.cgi


server.cpp代码如下:

#include <iostream>
using namespace std;

int main ()
{

cout << "Content-type:text/html\r\n\r\n";
cout << "<html>\n";
cout << "<head>\n";
cout << "<title>Hello World - 第一个 CGI 程序</title>\n";
cout << "</head>\n";
cout << "<body>\n";
cout << "<h2>Hello World! 这是我的第一个 CGI 程序</h2>\n";
cout << "</body>\n";
cout << "</html>\n";

return 0;
}


执行效果如下:



sh.cgi 代码如下:

#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "Hello world"


效果如下:



index.html代码如下:

<title>test </title>


执行效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息