せっかくのVPSなので、c++でcgiを書いてみます。
環境はCentOS5.5で、apache・g++がインストール済みとします。
・ソースコード
test.cpp
- #include <iostream>
- using namespace std;
- int main()
- {
- cout << "Content-Type: text/html" << endl << endl;
- cout << "<html>" << endl;
- cout << "<head><title>test</title></head>" << endl;
- cout << "<body>Hello world!</body>" << endl;
- cout << "</html>" << endl;
- }
ソースコードで行っていることは、最初に
- cout << "Content-Type: text/html" << endl << endl;
後はHTMLを出力しています。
・コンパイル
g++ -o test.cgi test.cpptest.cppからcgi: test.cgiを作成します。
・apacheの設定
/etc/httpd/conf/httpd.confを開き
<Directory "/cgiを置くディレクトリ"> AllowOverride None Options None ExecCGI Order allow,deny Allow from all </Directory>を追加。
#AddHandler cgi-script .cgiのコメントを外し、apacheを再起動。
(コメントを外さないと、ブラウザでアクセスした時にcgiが実行されずにダウンロードダイアログが表示されます。)
ブラウザからcgiにアクセスし、[Hello world!]と表示されれば成功です。
0 コメント:
コメントを投稿