Apache开始第一个cgi程序

简书排版
http://www.jianshu.com/p/2dd2fd2a8943

服务器OS: Centos 6.5 x64
本机OS: Ubuntu 14.04 x64

基于源码安装配置

准备

#下载地址  http://apr.apache.org/download.cgi
apr-1.5.2.tar.gz
apr-util-1.5.4.tar.gz
#官网  http://www.pcre.org/
pcre-8.38.tar.gz
#官网  http://httpd.apache.org/
httpd-2.4.23.tar.gz

进入服务器
首先安装gcc和个g++(如果没有的话)

yum install -y gcc gcc-c++

1.安装apr

tar -vxzf apr-1.5.2.tar.gz
cd apr-1.5.2
./configure --prefix=/usr/local/apr
make
make install

2.安装apr-util

tar -vxzf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr
make
make install

3.安装pcre

tar -vxzf pcre-8.38.tar.gz
cd pcre-8.38
./configure --prefix=/usr/local/pcre
make
make install

4.安装Apache

tar -vxzf httpd-2.4.23.tar.gz
cd httpd-2.4.23
./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre --enable-ssl --with-ssl=/usr/local/openssl
make
make install

安装好之后修改配置文件

vi /usr/local/apache/conf/httpd.conf
找到#ServerName www.example.com:80 去掉注释改为
ServerName localhost:80

#找到如下一行,去掉注释
Loadmodule cgid_module modules/mod_cgid.so
#找到<Directory /usr/local/apache/cgi-bin>修改为
<Directory /usr/local/apache/cgi-bin>
      Require all granted
      AllowOverride None
      Options +ExecCGI
      Order allow,deny
      Allow from all
</Directory>

保存退出

进入/usr/local/apache/cgi-bin目录
新建文件main.cpp添加下列内容

#include <iostream>

using namespace std;

int main() {
    cout << "Content-type:application/json\r\n\r\n";
    cout << "Welcome...\n";
    return 0;
}

然后编译

g++ -o welcome main.cpp

进入bin目录开启Apache服务

#启动
./apachectl start
#重启
./apachectl restart
#停止
./apachectl stop

如果访问不了,记得要关闭端口,或者关闭防火墙

vi /etc/sysconfig/iptables 
#在-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT之后添加
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
#重启防火墙
service iptables restart
#或者直接关闭防火墙
service iptables stop

浏览器输入
http://192.168.1.200/cgi-bin/welcome
如图所示

第一个cgi程序终于玩出来了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值