apache python 模組mod_wsgi的編譯安裝
周海漢
2012.10.26
前言
要讓apache支援django,首先要支援python。wsgi是目前效率最高的支援python的模組,遵循wsgi標準。
關於apache的編譯安裝,請參考我此前寫的《apache httpd 2.4.3編譯安裝》,本篇介紹如何讓apache支援wsgi。
下載 [[email protected] ~]$ wget http://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz
編譯 [[email protected] ~]$ tar zxvf mod_wsgi-3.4.tar.gz
[
centos 5.5自帶的apache沒有apxs模組,所以需要自己編譯apache,參考《apache httpd 2.4.3編譯安裝》,使其支援apxs。
另外,需要最新python版本。centos自帶的是python 2.4,至少需要python 2.6 以上。我這裡採用最新版python 2.7.2.,到http://www.python.org下載。
安裝python 2.7
[[email protected] ~]$ tar zxvf Python-2.7.2.tgz
[[email protected] Python-2.7.2]$ ./configure
這裡,必須用–enable-shared,生成動態庫,否則會遇到wsgi不能編譯的問題。
[[email protected] Python-2.7.2]$ make
[[email protected] Python-2.7.2]$ sudo make install
/usr/local/lib/python2.7
/usr/local/bin/python
[
這是32位和64位版本搞混的問題 參考http://code.google.com/p/modwsgi/wiki/InstallationIssues
將/usr/local/lib下的python和庫刪除 [[email protected] Python-2.7.2]$ ./configure –enable-shared [[email protected] Python-2.7.2]$ make [[email protected] Python-2.7.2]$ sudo make install [[email protected] lib]$ python python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
找不到庫,需用ldconfig指定庫路徑
[[email protected] local]$ sudo ldconfig /usr/local/lib [[email protected] local]$ python Python 2.7.2 (default, Oct 26 2012, 11:52:55)
繼續編譯wsgi
[[email protected] mod_wsgi-3.4]$ ./configure –with-apxs=/usr/local/apache2/bin/apxs –with-python=/usr/local/bin/python2.7 [[email protected] mod_wsgi-3.4]$ make [[email protected] mod_wsgi-3.4]$ sudo make install
配置
修改httpd.conf配置,增加
LoadModule wsgi_module modules/mod_wsgi.so
為了防止原始碼被下載,一般將原始碼放在DocumentRoot之外的目錄。
但需要配置httpd.conf裡面目錄訪問許可權Allow from all,否則訪問時會引起403,not allowed錯誤。 我將wsgi測試程式放在/home/zhouhh/www,該目錄需要允許apache使用者有可讀許可權。 [[email protected] apache2]# vi conf/httpd.conf #same as sys.prefix WSGIPythonHome /usr/local
AllowOverride none
Require all denied
Require all granted Order allow,deny Allow from all </Directory> <Directory “/home/zhouhh/www”> Order allow,deny Allow from all </Directory>
HTTP請求處理指令碼
#WSGIScriptAlias /test /usr/local/apache2/htdocs/t.wsgi WSGIScriptAlias /test /home/zhouhh/www/test.wsgi <Directory “/usr/local/apache2/htdocs”> … Order allow,deny Allow from all </Directory> [[email protected] apache]# apachectl -k stop [[email protected] apache]# apachectl -k start
檢查一下模組
[[email protected] www]$ apachectl -l Compiled in modules: core.c mod_so.c http_core.c event.c [[email protected] www]$ apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_event_module (static) authn_file_module (shared) authn_core_module (shared) authz_host_module (shared) authz_groupfile_module (shared) authz_user_module (shared) authz_core_module (shared) access_compat_module (shared) auth_basic_module (shared) reqtimeout_module (shared) filter_module (shared) mime_module (shared) log_config_module (shared) env_module (shared) headers_module (shared) setenvif_module (shared) version_module (shared) unixd_module (shared) status_module (shared) autoindex_module (shared) dir_module (shared) alias_module (shared) rewrite_module (shared) wsgi_module (shared)
此時能看到wsgi_module也已經載入 [[email protected] modules]$ ldd mod_wsgi.so libpython2.7.so.1.0 => /usr/local/lib/libpython2.7.so.1.0 (0x00002b9d6e909000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b9d6ecd3000) libdl.so.2 => /lib64/libdl.so.2 (0x00002b9d6eeee000) libutil.so.1 => /lib64/libutil.so.1 (0x00002b9d6f0f3000) libm.so.6 => /lib64/libm.so.6 (0x00002b9d6f2f6000) libc.so.6 => /lib64/libc.so.6 (0x00002b9d6f579000) /lib64/ld-linux-x86-64.so.2 (0x00000037d9200000)
可以看到mod_wsgi.so指向的庫是正確的路徑
測試 [[email protected] www]$ pwd /home/zhouhh/www [[email protected] www]$ cat test.wsgi
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
此時訪問 http://hadoop47/test.wsgi Not Found 訪問 http://hadoop47/test Hello World!
參考
如非註明轉載, 均為原創. 本站遵循知識共享CC協議,轉載請註明來源