首页 > 网站建设 > 使用Apache搭建http服务器实现CGI功能
2024
07-17

使用Apache搭建http服务器实现CGI功能

专门处理HTTP请求的服务器,也被称为Web服务器,常用的Web服务器有Apache和Nginx,当然几大巨头五联网公司也都有其独自研发的Web服务器,比如阿里巴巴的Tengine,这篇文章主要介绍了使用Apache搭建http服务器,实现CGI,需要的朋友可以参考下

一、环境搭建

搭建环境CentOS7.5.

专门处理 HTTP 请求的服务器,也被称为 Web 服务器。 常用的 Web 服务器有 Apache和 Nginx ,当然几大巨头五联网公司也都有其独自研发的 Web 服务器,比如阿里巴巴的Tengine 。 我们使用 Apache 作为 Web 服务器,并按照下面 5 个步骤安装好 Apache 。

1、下载安装包

安装lynx命令,是一种以文本方式查看网页的工具,当然你也可以选择直接百度下载

参考:https://blog.csdn.net/u011641885/article/details/45459199
1
lynx http://httpd.apache.org/download.cg

我下载的是2.4.54版本。

2、安装依赖包

解压,进入解压目录,

1
2
3
gzip -d httpd- 2.4.54.tar.gz
tar xvf httpd-2.4.54.tar
cd httpd-2.4.54

这时候直接是安装不了的,需要依赖包

(1)下载相关包

1
2
3
gzip -d httpd- 2.4.54.tar.gz
tar xvf httpd-2.4.54.tar
cd httpd-2.4.54

(2)解决apr not found

1
2
3
4
5
tar -zxf apr-1.4.5.tar.gz
cd apr-1.4.5
./configure --prefix=/usr/local/apr
make
make install

(3)解决APR-util not found

1
2
3
4
5
tar -zxf apr-util-1.3.12.tar.gz
cd apr-util-1.3.12
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config --enable-utf8
make
make install

(4)解决pcre问题

1
2
3
4
unzip -o pcre-8.10.zip
cd pcre-8.10
./configure --prefix=/usr/local/pcre
make && make install

安装完所有依赖包之后执行

1
./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre/bin/pcre-config

接下来执行

1
makemake install

3、修改配置文件

1
vi /usr/local/apache/conf/httpd.conf

打开该行注释

1
#ServerName www.example.com:80

4、启动服务

1
/usr/local/apache/bin/apachectl start

停止服务

1
/usr/local/apache/bin/apachectl stop

重启服务

1
/usr/local/apache/bin/apachectl restart

5、测试

在你的浏览器输入你的机器的IP地址,就是你配置apache的机器IP,提示以下代表安装成功。

二、测试CGI

1、修改配置文件

1
vi /usr/local/apache/conf/httpd.conf

打开该行注释,开启模块

1
2
#LoadModule cgid_module modules/mod_cgid.so
#LoadModule alias_module modules/mod_alias.so

2、重启服务

1
/usr/local/apache/bin/apachectl restart

3、创建CGI脚本

在/usr/local/apache/cgi-bin/ 目录下创建cgiscript脚本,脚本内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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 - First CGI Program</title>\n";
cout << "</head>\n";
cout << "<body>\n";
cout << "<h2>Hello World! This is my first CGI program</h2>\n";
cout << "</body>\n";
cout << "</html>\n";
return 0;
}
1
g++ -o test cgiscript

4、浏览器测试

在浏览器中输入以下内容:

http://192.168.122.1/cgi-bin/test

到此这篇关于使用Apache搭建http服务器,实现CGI的文章就介绍到这了。



本文》有 0 条评论

留下一个回复