在ubuntu下安裝apache一個命令就能夠搞定 sudo apt-get install apche (連續按兩次tab,會出現提示),順其天然的安裝就好了。若是說安裝過程失敗,提示須要某些依賴庫的時候,運行sudo apt-get -f install就能夠了。html
安裝完成以後運行 apache2 -v 查看apache版本ios
Server version: Apache/2.4.7 (Ubuntu)
Server built: Jan 14 2016 17:45:23apache
利用瀏覽器訪問網頁 192.168.0.112(換成你的ip地址) 若是能正常看到網頁打開了表示apache安裝沒問題。ubuntu
運行cd /etc/apache2 進入到apache2目錄下,在這個目錄查找httpd.conf。若是沒有,直接新建一個httpd.conf。vim
在裏面寫入, #表示註釋內容瀏覽器
LoadModule cgi_module /usr/lib/apache2/modules/mod_cgi.so #加載cgi庫文件
addHandler cgi-script .cgi .pl .py .sh #cgi支持的後綴名 測試
在用vim打開apache2.confui
在最後面加上 Include httpd.conf 表示包含httpd.conf,在apache在啓動的時候會加入這個配置文件。因此要重啓apache(apaech2 restart)。spa
提示:sudo vim httpd.conf命令行
1.輸入i表示開始插入,在vim下面能夠看到當前的vim的insert狀態
2.寫完以後,按下esc表示命令行模式,這時在按下shfit+: 再按下wq 表示退出並保存(注意沒有的+,+表示多個鍵一塊兒按下)
3.修改apache2.conf 時一樣使用sudo vim apache2.conf
下面是測試代碼
#include <iostream> using namespace std; int main(void) { 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 << "<h1>\n"; cout << "hello world!\n"; cout << "</h1>\n"; cout << "<h1>\n"; cout << "hello apache!\n"; cout << "</h1>\n"; cout << "</body>\n"; cout << "</html>\n"; return 0; }
使用 g++ -Wall -g hello.cpp -o hello.cgi 注意要以cgi結尾不然執行腳本的時候可能不能正常執行。
再將hello.cgi 拷貝到 /var/www/cgi-bin/. 若是提示目錄不存在的就去建立目錄 sudo mkdir /var/www/cgi-bin -p
再從瀏覽器訪問 192.168.0.112/cgi-bin/hello.cgi 表示完成cgi配置。若是沒有成功不用擔憂,多試幾回,查看apache錯誤日誌分析問題(這個很重要,由於我第一次也沒有配置成功的),日誌地址爲/var/log/apache2/