天天看點

Python+Apache+CGI完全配置

作業系統環境:Ubuntu 15.10

0.需求原因

    想在我的Linux上架設Apache來運作CGI程式,友善以後用Apache部署Python的Web應用,但遇到各種各樣的問題,網上找的答案要麼都太舊了(4/5年前跟現在已經差很多了),要麼就不完整,是以這裡總結一下,作為最新版本吧。

1.Apache安裝

    采用的是源碼編譯安裝的形式。首先要說明的是,要成功安裝Apache,需要依次安裝完下面的軟體(函數庫)才算是成功地安裝了Apache:

(1)apr安裝

下載下傳位址:http://apr.apache.org/

注意這個網站上有兩個相應的軟體下載下傳,一個是apr,另外一個是下面将要安裝的apr-util,由于英文名字看起來太容易混淆,是以一定要看清楚下載下傳的是哪個。這裡我下載下傳的是: apr-1.5.2.tar.gz

第一步:解包

xpleaf@leaf:~/下載下傳$ tar xvf apr-1.5.2.tar.gz 
......
apr-1.5.2/mmap/win32/
apr-1.5.2/mmap/win32/mmap.c
apr-1.5.2/apr-config.in      

第二步:運作configure檢測和設定編譯選項

xpleaf@leaf:~/下載下傳/apr-1.5.2$ ./configure 
......
config.status: creating include/arch/unix/apr_private.h
config.status: executing libtool commands
rm: cannot remove 'libtoolT': No such file or directory
config.status: executing default commands      

    正常情況下是沒有問題的,如果出現問題,可以自行查找,這個好解決。

第三步:編譯和安裝

xpleaf@leaf:~/下載下傳/apr-1.5.2$ make
......
xpleaf@leaf:~/下載下傳/apr-1.5.2$ sudo make install
......      

    OK,這樣的話,apr就安裝完成了。

(2)apr-util安裝

    下載下傳位址跟上面的是一樣的,這裡我下載下傳的版本是:apr-util-1.5.4.tar.gz

    apr-util安裝的步驟跟上面的差不多,不過有一步要注意的,就是在執行./configure指令的時候需要指定apr的安裝目錄:

xpleaf@leaf:~/下載下傳/apr-util-1.5.4$ ./configure
......
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
Applying apr-util hints file rules for x86_64-unknown-linux-gnu
checking for APR... no
configure: error: APR could not be located. Please use the --with-apr option.
#注意看上面的報錯提示      

    先找到apr的安裝目錄:

xpleaf@leaf:~/下載下傳/apr-util-1.5.4$ whereis apr
apr: /usr/local/apr      

    再添加相應參數執行apr-util的./configure指令:

xpleaf@leaf:~/下載下傳/apr-util-1.5.4$ ./configure --with-apr=/usr/local/apr
......
config.status: creating test/Makefile
config.status: creating include/private/apu_config.h
config.status: executing default commands      

    這樣的話預編譯就算完成了,再執行下面的make和sudo make install:

xpleaf@leaf:~/下載下傳/apr-util-1.5.4$ make
xpleaf@leaf:~/下載下傳/apr-util-1.5.4$ sudo make install      

    OK,這樣的話,apr-util的安裝也完成了。

(3)pcre安裝

    這不是必需的,前提是你的系統原來已經安裝了它的庫,如果還沒有安裝的話,在你編譯Apache的時候就會出現下面的錯誤提示:

configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

    是以如果你也出現了這樣的情況,請先下載下傳安裝pcre。

    下載下傳位址:http://sourceforge.net/projects/pcre/files/pcre/

    這裡我下載下傳的版本是:pcre-8.38.tar.gz

    安裝步驟跟上面的一樣,而且這裡也不會出現什麼問題,直接給出步驟好了:

xpleaf@leaf:~/下載下傳$ tar zxvf pcre-8.38.tar.gz 
xpleaf@leaf:~/下載下傳/pcre-8.38$ ./configure
xpleaf@leaf:~/下載下傳/pcre-8.38$ make
xpleaf@leaf:~/下載下傳/pcre-8.38$ sudo make install      

    pcre也安裝完之後,下面就可以真正安裝Apache了。

(4)Apache安裝

    在上面的三個軟體安裝完成之後,這下面才開始安裝Apache.

    下載下傳位址:http://httpd.apache.org/download.cgi

    我下載下傳的版本是:httpd-2.4.18.tar.gz

a.安裝   

    隻要上面的幾步完成了,安裝apache就比較容易了,依次執行下面的指令就可以了。

xpleaf@leaf:~/下載下傳$ tar zxvf httpd-2.4.18.tar.gz 
xpleaf@leaf:~/下載下傳/httpd-2.4.18$ ./configure --enable-module=shared
xpleaf@leaf:~/下載下傳/httpd-2.4.18$ make
xpleaf@leaf:~/下載下傳/httpd-2.4.18$ sudo make install      

b.參數說明

     需要注意的是,如果在執行./configure時沒有指定apache的安裝目錄,預設是安裝在/usr/local/apache2,你也可以通過下面的方式來檢視它的安裝目錄:

xpleaf@leaf:~$ whereis apache2
apache2: /usr/local/apache2      

    另外,上面在執行./configure時添加了-enable-module=shared參數,即通過把子產品編譯成動态共享對象,讓Apache啟動時動态加載,這樣以後需要加載新子產品時,隻需在配置檔案中設定即可。當然,為了這樣的便捷性而損失一定的性能是否值得,個人衡量。是以這個參數你不加也是沒有問題的。

c.無法啟動的解決方案

    安裝完成後,就可以啟動了,因為采用的是編譯安裝,是以需要用下面的方式啟動:

xpleaf@leaf:~$ sudo /usr/local/apache2/bin/apachectl start
/usr/local/apache2/bin/httpd: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory      

    但是你會發現又報錯了,而且報的錯為:無法加元libpcre.so.1庫函數。

    libpcre.so.1,很熟悉的名字,前面不是已經安裝了?确實是安裝的,隻是Apache沒有找到它而已,檢視一下:

xpleaf@leaf:~$ ldd /usr/local/apache2/bin/httpd 
    linux-vdso.so.1 =>  (0x00007ffdf9fe6000)
    libpcre.so.1 => not found
    libaprutil-1.so.0 => /usr/local/apr/lib/libaprutil-1.so.0 (0x00007f8192a47000)
    libapr-1.so.0 => /usr/local/apr/lib/libapr-1.so.0 (0x00007f8192813000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f81925f5000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f819222b000)
    libexpat.so.0 => /usr/local/apr/lib/libexpat.so.0 (0x00007f8192002000)
    libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007f8191dca000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f8191bc6000)
    /lib64/ld-linux-x86-64.so.2 (0x00005637a587b000)      

    這時我們把pcre對應的該庫函數軟體連結到/lib目錄就可以了:

xpleaf@leaf:~$ sudo ln -s /usr/local/lib/libpcre.so.1 /lib      

    下面再啟動試試:

xpleaf@leaf:~$ sudo /usr/local/apache2/bin/apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message      

    OK,這時再打開浏覽器,經典的"It works"顯示出來了:

Python+Apache+CGI完全配置

    這樣的話,Apache的安裝也算是完成了,下面就說說CGI的配置。

2.CGI配置

    Apache支援CGI配置,下面就來詳細說明配置的過程。

    找到安裝Apache的目錄/usr/local/apache2/conf,并對httpd.conf配置檔案進行修改,如下面的說明:

(1)加載cgi子產品

    大概在httpd.conf的137行:

#LoadModule cgid_module modules/mod_cgid.so      

    去掉注釋:

LoadModule cgid_module modules/mod_cgid.so      

(2)設定cgi腳本檔案路徑

    大概在httpd.conf的329行:

ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"      

    可以看到apache将"/usr/local/apache2/cgi-bin/"映射為/cgi-bin/,即當你通過浏覽器通路/cgi-bin/目錄時,實際上就是通路"/usr/local/apache2/cgi-bin/"目錄,為了友善,我将其設定為如下:

ScriptAlias /cgi-bin/ /home/xpleaf/Source_Code/cgi_for_py/      

    注意這裡路徑末尾一定要加/,否則apache是無法打到該路徑下的檔案的。

(3)設定cgi路徑的通路權限

    大概在httpd.conf195行:

<Directory />
    AllowOverride none
    Require all denied
</Directory>      

    将上面的内容全部修改為下面的内容:

<Directory "/home/xpleaf/Source_Code/cgi_for_py/">
   AllowOverride None
   Options +ExecCGI
   Order allow,deny
   Allow from all
</Directory>      

    注意這裡的目錄是修改為前面我們改的:/home/xpleaf/Source_Code/cgi_for_py/

    這樣的修改是有原因的,如果沒有在這裡設定權限,當你在浏覽器中通路你的cgi腳本檔案時,得到的可能是這樣的提示:

Forbidden

You don't have permission to access /cgi-bin/hello.py on this server.

(4)設定apache可解釋python的cgi腳本檔案

    大概在httpd.conf的386行:

#AddHandler cgi-script .cgi      

    去掉注釋,将其修改為:

AddHandler cgi-script .cgi .py      

    加上.py後,就可以解釋python的腳本檔案了,如果你需要解釋shell的腳本檔案,可以添加.pl,其它的類似。

    OK,完成上面的4步之後,CGI配置就算完成了。不過在配置完成之後,我們需要重新開機一下Apache服務:

xpleaf@leaf:/usr/local/apache2/conf$ sudo /usr/local/apache2/bin/apachectl restart
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message      

3.添加CGI腳本檔案

(1)建立cgi腳本檔案

    在/home/xpleaf/Source_Code/cgi_for_py/目錄下添加下面一個檔案,并命名為hello.py:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

print "Content-type:text/html"
print
print '<html>'
print '<head>'
print '<title>Hello</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'      

    注意#!/usr/bin/env python一定要加上,否則會出現500錯誤提示。   

(2)設定cgi腳本檔案的檔案權限為755    

xpleaf@leaf:~/Source_Code/cgi_for_py$ chmod 755 hello.py
xpleaf@leaf:~/Source_Code/cgi_for_py$ ls -l hello.py
-rwxr-xr-x 1 xpleaf xpleaf 289  1月 31 16:02 hello.py      

    此步非常非常重要,如果忘記這步,無論你怎麼找配置檔案也找不出錯誤,就算看代碼也不會發現有錯誤,然後通過浏覽器通路時會一直顯示500錯誤,是以一定要記得該步操作!   

4.通過浏覽器通路cgi腳本檔案

    直接在浏覽器中輸入localhost/cgi-bin/hello.py或127.0.0.1/cgi-bin/hello.py就可以通路我們的cgi腳本檔案了:

Python+Apache+CGI完全配置

    終于可以了!