2014-06-05

在 Centos 下安裝 mod_wsgi (無廢話版)

前言
mod_python 及 mod_wsgi 要安裝哪一個較好?
若你 goo 完資料後答案很明顯, 就是 mod_wsgi. 因為
1. mod_wsgi 效率較好
2. mod_python 最後會淘汰不再更新





這裡使用的環境是 Apache 2.2

mod_wsgi 下載:
請到官網下載mod_wsgi,
https://github.com/GrahamDumpleton/mod_wsgi/releases

本範例使用 mod_wsgi 4.1.3,位置如下
https://github.com/GrahamDumpleton/mod_wsgi/archive/4.1.3.tar.gz
使用 wget 指令下載

# wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.1.3.tar.gz


解壓

# tar -xvf 4.1.3.tar.gz


安裝

# cd mod_wsgi-4.1.3
# ls
configure  configure.ac  docs  images  LICENCE  Makefile.in  MANIFEST.in  README.rst  setup.py  src  tests  tox.ini  win32
# ./configure
checking for apxs2... no
checking for apxs... no
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for prctl... yes
checking Apache version... ./configure: line 2755: apxs: command not found
./configure: line 2755: apxs: command not found
./configure: line 2756: apxs: command not found
./configure: line 2759: /: is a directory


錯誤訊息 apxs: command not found 是因為 Centos 本身就是最簡潔,最不帶哩哩扣扣的 OS,故它預設不會帶 apxs.
手動安裝 apxs , 指令
# yum install httpd-devel.x86_64
若你是 32bits CPU, 請用
# yum install httpd-devel.i686


再重新 Configure 就會發現成功了
# ./configure
checking for apxs2... no
checking for apxs... /usr/sbin/apxs
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for prctl... yes
checking Apache version... 2.2.15
checking for python... /usr/bin/python
configure: creating ./config.status
config.status: creating Makefile



若你要指定 python 則請用參數 --with-python, 例如我是用 python3.3, 並且不破壞 Centos 內建的 python2.6 (即雙 python), 故我的 configure 參數如下
#./configure --with-python=/usr/local/bin/python3.3

最後 make and install
#make
#make install

# make install
/usr/sbin/apxs -i -S LIBEXECDIR=/usr/lib64/httpd/modules -n 'mod_wsgi' src/server/mod_wsgi.la
/usr/lib64/httpd/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool' src/server/mod_wsgi.la /usr/lib64/httpd/modules
/usr/lib64/apr-1/build/libtool --mode=install cp src/server/mod_wsgi.la /usr/lib64/httpd/modules/
libtool: install: cp src/server/.libs/mod_wsgi.so /usr/lib64/httpd/modules/mod_wsgi.so
libtool: install: cp src/server/.libs/mod_wsgi.lai /usr/lib64/httpd/modules/mod_wsgi.la
libtool: install: cp src/server/.libs/mod_wsgi.a /usr/lib64/httpd/modules/mod_wsgi.a
libtool: install: chmod 644 /usr/lib64/httpd/modules/mod_wsgi.a
libtool: install: ranlib /usr/lib64/httpd/modules/mod_wsgi.a
libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/sbin" ldconfig -n /usr/lib64/httpd/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/lib64/httpd/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 755 /usr/lib64/httpd/modules/mod_wsgi.so


大功告成!!
若你想知道 mod_wsgi 在 Apache 的 configuration 請看這篇

安裝過程中遇到的其它問題

 如果你在安裝過程中遇到類似以下錯誤訊息
`a local symbol' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/python2.4/config/libpython2.4.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
apxs:Error: Command failed with rc=65536
.
make: *** [mod_wsgi.la] Error 1

這個原因是 mod_wsgi 是用 64bit compile 的, 而你的 python 可能是 32bit lib
解法就是在 make python 時使用參數 --enable-shared

# PYTHON_CONFIGURE_OPTS="--enable-unicode=ucs4 --enable-shared" 
# export PYTHON_CONFIGURE_OPTS 
# pyenv install 3.4.3


若你是下載 source code 安裝的話
請參考這篇的指令 http://missions5.blogspot.tw/2014/04/centos-6-python34.html


還有其它問題? 找找看這篇有沒有解法
https://code.google.com/p/modwsgi/wiki/InstallationIssues


沒有留言:

張貼留言