Apache2 CGI 方式的設定

2021-06-16 09:19:12 字數 1375 閱讀 5052

一、apache2 cgi 設定以ubuntu為例,首先安裝apache2:

sudo apt-get install apache2

然後配置apache2伺服器:

sudovim /etc/apache2/sites-enabled/000-default

找到documentroot和scriptalias並修改如下:

documentroot /var/www #指定**的根目錄

scriptalias /cgi-bin/ /home/hongqun/web/cgi-bin/ #指定/cgi-bin/ 連線到的目錄

加入下面內容,讓apache把.py檔案視為cgi指令碼來執行:

addhandler cgi-script .cgi .py

完成以上配置後重啟apache2:

sudo /etc/init.d/apache2 restart

二、以cgi方式執行python編寫test.py檔案,存放於前面配置的路徑下,檔案的內容如下:

#!/usr/bin/env python

import os

print "content-type: text/html\n"

print ""

print ""

print ""

fo = os.popen('ls -al /')

for s in fo.readlines():

print ""

print s

print ""

fo.close()

print ""

修改檔案的許可權:

chmod a+x test.py
這個指令碼的作用是列出的ubuntu的根目錄結構,在瀏覽器中訪問可以得到類似以下的結果:

設定Apache執行CGI程式

設定apache執行cgi程式 訪問 時訪問的是 另外目錄下的 cgi 程式。如此設定後,cgi bin 目錄下的任何檔案都以 cgi 程式執行。設定 cgi bin 目錄下的檔案用 cgi 執行。設定 cgi bin 之外目錄下的檔案中可以執行 cgi 程式,在 options 後加上 execc...

配置Apache對CGI的支援

在http.conf或 etc apache2 mods enable cgi.load 檔案裡,我的是在目錄 usr local apache2 conf 下面的http.conf 加入cgi支援 loadmodule cgi module usr lib apache2 modules mod ...

CGI在Apache上的配置

想寫個前台頁面傳送ajax請求到後台用c語言編寫的cgi程式,並成功返回結果。原始碼都很簡單,都有,結果在除錯配置時遇到了乙個又乙個問題。特記錄之。過程參考 1 先貼 ajaxtest1.htm suggest instruction cgi main.c include include inclu...