使用Apache搭建http服務器實現(xiàn)CGI功能
一、環(huán)境搭建
搭建環(huán)境CentOS7.5.
專門處理 HTTP 請求的服務器,也被稱為 Web 服務器。 常用的 Web 服務器有 Apache和 Nginx ,當然幾大巨頭五聯(lián)網(wǎng)公司也都有其獨自研發(fā)的 Web 服務器,比如阿里巴巴的Tengine 。 我們使用 Apache 作為 Web 服務器,并按照下面 5 個步驟安裝好 Apache 。
1、下載安裝包
安裝lynx命令,是一種以文本方式查看網(wǎng)頁的工具,當然你也可以選擇直接百度下載
參考:https://blog.csdn.net/u011641885/article/details/45459199
lynx http://httpd.apache.org/download.cg
我下載的是2.4.54版本。
2、安裝依賴包
解壓,進入解壓目錄,
gzip -d httpd- 2.4.54.tar.gz tar xvf httpd-2.4.54.tar cd httpd-2.4.54
這時候直接是安裝不了的,需要依賴包
(1)下載相關包
gzip -d httpd- 2.4.54.tar.gz tar xvf httpd-2.4.54.tar cd httpd-2.4.54
(2)解決apr not found
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
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問題
unzip -o pcre-8.10.zip cd pcre-8.10 ./configure --prefix=/usr/local/pcre make && make install
安裝完所有依賴包之后執(zhí)行
./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
接下來執(zhí)行
makemake install
3、修改配置文件
vi /usr/local/apache/conf/httpd.conf
打開該行注釋
#ServerName www.example.com:80
4、啟動服務
/usr/local/apache/bin/apachectl start
停止服務
/usr/local/apache/bin/apachectl stop
重啟服務
/usr/local/apache/bin/apachectl restart
5、測試
在你的瀏覽器輸入你的機器的IP地址,就是你配置apache的機器IP,提示以下代表安裝成功。
二、測試CGI
1、修改配置文件
vi /usr/local/apache/conf/httpd.conf
打開該行注釋,開啟模塊
#LoadModule cgid_module modules/mod_cgid.so #LoadModule alias_module modules/mod_alias.so
2、重啟服務
/usr/local/apache/bin/apachectl restart
3、創(chuàng)建CGI腳本
在/usr/local/apache/cgi-bin/ 目錄下創(chuàng)建cgiscript腳本,腳本內容如下:
#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; }
g++ -o test cgiscript
4、瀏覽器測試
在瀏覽器中輸入以下內容:
http://192.168.122.1/cgi-bin/test
到此這篇關于使用Apache搭建http服務器,實現(xiàn)CGI的文章就介紹到這了,更多相關Apache http服務器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
CentOS7 安裝 zabbix 4.0 教程(圖文詳解)
這篇文章主要介紹了CentOS7 安裝 zabbix 4.0 教程,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10linux環(huán)境下設置shadowsocks+polipo全局代理
XX-NET僅僅可以是用戶通過瀏覽器訪問外面世界,而非系統(tǒng)全局代理,使用shadowsocks+polipo技術可使功能進一步拓展2018-09-09Linux使用less高效讀取GC日志的實現(xiàn)方法
在Linux環(huán)境中,日志分析是運維和開發(fā)人員日常工作中不可或缺的一部分,特別是對于Java應用的垃圾回收(GC)日志,本文將結合實際案例,詳細講解如何使用 less 命令高效讀取和分析GC日志,需要的朋友可以參考下2025-04-04