Centos5配置Python mod_wsgi Django安裝osqa


Centos5配置Python mod_wsgi Django安裝osqa – 遠方博客

Centos5配置Python mod_wsgi Django安裝osqa

作者 FarLee 2011年9月29日 22:17:37   ‖瀏覽(2,503)

Unix/Linux系統下基本上都預安裝了Python,但有時候它的版本可能不是符合我們要求的。比如在安裝osqa–一個基於python和Django框架搭建的問答社區網站開源程序時,對Python的版本要求2.6+,而Centos 5下默認安裝的是2.4版本。

因此下面記錄的是在Centos Linux vps上編譯安裝Python 2.7.2,python模塊,Apache服務器上啟用mod_wsgi,配置Django,搭建osqa開源問答社區網站的步驟。建議編譯安裝之前先通讀全文,這里是按一般編譯安裝步驟是出現問題,然后解決問題的思路記錄的。 

Centos編譯安裝Python 2.7

下載Python 2 的當前最新版本:

wget http://python.org/ftp/python/2.7.2/Python-2.7.2.tgz
tar -zxpvf Python-2.7.2.tgz -C /opt
cd /opt/Python-2.7.2
./configure

編譯安裝第一步就出現如下錯誤,無法繼續安裝:

checking for –without-gcc… no
checking for gcc… no
checking for cc… no
checking for cl.exe… no
configure: error: in `/opt/Python-2.7.2′:
configure: error: no acceptable C compiler found in $PATH
See `config.log’ for more details

需要安裝gcc編譯環境:yum -y install gcc
然后再執行:

./configure
make
make install

Centos系統默認Python 2.4版本安裝在/usr/lib/python2.4 目錄下(python >>> import sys
>>> sys.path)。 查看README文件得知Python 2.7新版本會默認安裝在目錄:/usr/local/lib/python2.7/。如果系統默認版本也是在相同的安裝目錄,則要使用make altinstall而不是make install,才不會導致因覆蓋系統默認的Python版本而導致出現系統出現未知問題。

Python 模塊安裝

Python 編譯安裝完畢后,可能發現一些python模塊需要安裝。這列舉了兩種方法:

1.使用Python推薦並自帶的Distutils安裝模塊:下載源碼-》解壓-》cd到模塊解壓目錄-》執行/usr/local/bin/python2.7 setup.py install –prefix=/usr/local。關於使用Distutils安裝Python 模塊的官方詳細介紹:Installing Python Modules

2.使用easy_install: 使用easy_install之前要先安裝python 的setuptools工具模塊:

Setuptools 模塊
在下載頁面選擇2.7版本下載:setuptools-0.6c11-py2.7.egg

cd ~
wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg#md5=fe1f997bc722265116870bc7919059ea
sh setuptools-0.6c11-py2.7.egg –prefix=/usr/local

提示錯誤:

zipimport.ZipImportError: can’t decompress data; zlib not available

(注:也可用ez_setup.py直接安裝,或通過yum倉庫安裝setuptools模塊(yum install python-setuptools),不過容易安裝在系統默認的python版本中,不是我們需要的編譯安裝的Python新版本。)

Zlib 模塊
上面的錯誤提示缺少zlib 庫,實際上我們在上面編譯安裝Python結束時就應該出現了這個提示:

Python build finished, but the necessary bits to build these modules were not found:
_bsddb _curses _curses_panel _sqlite3 _ssl _tkinter bsddb185 bz2 dbm gdbm readline sunaudiodev zlib

需要啟用zlib模塊需要然后重新編譯一下Python源碼安裝包:

cd /opt/Python-2.7.2
vi Modules/Setup
搜索zlib,去掉 #zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz 前面的注釋。
./configure
make

又出現錯誤提示:

./Modules/zlibmodule.c:112: error: ‘compobject’ has no member named
make: *** [Modules/zlibmodule.o] Error 1

Update:上面安裝zlib的方法失敗,原來Python 2.7得先安裝zlib庫:

cd /opt/Python-2.7.2/Modules/zlib
./configure make && make install

然后再重新編譯安裝python,也不用再編輯去掉Modules/Setup文件中的#zlib zlibmodule.c 的注釋。
測試一下,無錯誤提示:

python2.7 >>> import zlib >>> exit()

再回到上面安裝setuptools模塊的安裝,提示成功:

Installing easy_install script to /usr/local/bin
Installing easy_install-2.7 script to /usr/local/bin
Installed /usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg

使用easy_install安裝其他python模塊

easy_install ElementTree Markdown html5lib python-openid

Django 安裝

Django 安裝當然也可以easy_install: easy_install django django-debug-toolbar
這里使用Distutils下載Django編譯安裝:

cd ~
wget http://www.djangoproject.com/download/1.3.1/tarball/
tar xzvf Django-1.3.1.tar.gz
cd Django-1.3.1
sudo python2.7 setup.py install

Django 2.7.3 的安裝目錄是:/usr/local/lib/python2.7/site-packages/django
Ubuntu 10.04 默認python版本使用easy install安裝的Django 安裝目錄為:/usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django

Apache mod_wsgi 安裝配置

安裝 Apache和mysql : yum install httpd mysql-server
Apache和mysql數據庫的安裝配置可參考搭建Centos 5.5搭建LAMP
題外話:因nginx啟動占用了80端口:

(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down

更改Apache默認80端口:vi /etc/httpd/conf/httpd.conf 修改 Listen 80為其他端口。

mod_wsgi 安裝
下載 mod_wsgi

wget http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz
tar xvfz mod_wsgi-3.3.tar.gz -C /opt
cd /opt/mod_wsgi-3.3
./configure –with-python=/usr/local/bin/python

出現問題:./configure: line 1704: apxs: command not found
Google之,傳需要安裝httpd-devel才能編譯:yum install httpd-devel,安裝httpd-devel出現依賴性問題:

Error: Missing Dependency: httpd = 2.2.3-53.el5.centos.1 is needed by package httpd-devel-2.2.3-53.el5.centos.1.i386 (updates).
Error: Missing Dependency: apr-util = 1.2.7-11.el5_5.2 is needed by package apr-util-devel-1.2.7-11.el5_5.2.i386 (base)

httpd 已經安裝,卻提示缺少依賴未安裝,原因是Apache版本不符。卸載后重新安裝相符版本:

yum remove apr-util httpd httpd-tools 我的情況是會同時卸載了依賴 php 5.3.6-1.w5(php-fastcgi不受影響)。
yum install httpd httpd-devel

mod_wsgi 順利configure之后,
make:出現很多warning:unused variable / defined but not used
make install:安裝完成:

Libraries have been installed in:
/usr/lib/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.

make clean:清除編譯安裝過程中產生的臨時文件。

修改Apache 配置文件,啟用mod_wsgi模塊

vi /etc/httpd/conf/httpd.conf
加入 LoadModule wsgi_module modules/mod_wsgi.so
/etc/init.d/httpd restart

OSQA 搭建配置

OSQA 和 Mysql
這里安裝OSQA,我們不用PostgreSQL,而用Apache熟悉的Mysql數據庫,需要先安裝 MySQL-python:yum install MySQL-python
並創建好數據庫,防止中文亂碼,使用utf-8:

CREATE DATABASE osqa DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;

OSQA 安裝

yum install subversion
svn co http://svn.osqa.net/svnroot/osqa/trunk/ /srv/www/osqa
(Checked out revision 1174.)
cd /srv/www/osqa
mkdir cache
chown -R apache:apache .
chmod u+w cache
chmod u+w log
chmod u+w forum/upfiles

編輯osqa.wsgi
cp osqa.wsgi.dist osqa.wsgi
vi osqa.wsgi 加刪除線的地方是要修改的
sys.path.append(‘/srv/www‘)
sys.path.append(‘/srv/www/osqa‘)
os.environ['DJANGO_SETTINGS_MODULE'] = ‘osqa.settings’

編輯settings_local.py
cp settings_local.py.dist settings_local.py
vi settings_local.py 修改數據庫信息和APP_URL等幾個地方:
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: ‘osqa數據庫名詞’,
‘USER’: ‘root用戶名’,
‘PASSWORD’: ‘passw密碼’,
‘HOST’: ”,
‘PORT’: ”,
}
}
APP_URL = ‘http://farlee.info
LANGUAGE_CODE = ‘zh_CN’ 安裝osqa中文版

修改apache配置文件:
vi /etc/httpd/conf.d/mod_wsgi.conf 加入

WSGISocketPrefix /var/run/wsgi
WSGIPythonHome /usr/local/lib/python2.7

#NOTE: all urs below will need to be adjusted if
#settings.FORUM_SCRIPT_ALIAS !=” (e.g. = ‘forum/’)
#this allows “rooting” forum at [http://example.com/forum], if you like
Listen 8080

ServerAdmin admin@your.server.com
DocumentRoot /srv/www/osqa
ServerName farlee.info

#run mod_wsgi process for django in daemon mode
#this allows avoiding confused timezone settings when
#another application runs in the same virtual host
WSGIDaemonProcess OSQA
WSGIProcessGroup OSQA

#force all content to be served as static files
#otherwise django will be crunching images through itself wasting time
Alias /m/ /srv/www/osqa/forum/skins/
Alias /upfiles/ /srv/www/osqa/forum/upfiles/

Order allow,deny
Allow from all

#this is your wsgi script described in the prev section
#this is your wsgi script described in the prev section
WSGIScriptAlias / /srv/www/osqa/osqa.wsgi

CustomLog /var/log/httpd/osqa/access_log common
ErrorLog /var/log/httpd/osqa/error_log

創建相關目錄: mkdir -p /var/run/wsgi mkdir /var/log/httpd/osqa

創建導入數據庫表,當前目錄為/srv/www/osqa執行:python2.7 manage.py syncdb
出現錯誤提示:

File “/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py”, line 14, in
raise ImproperlyConfigured(“Error loading MySQLdb module: %s” % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

前面我們已經安裝了MySQL-python,為什么提示沒有MySQLdb模塊?Google之說是centos下安裝了MySQL-python之后還得安裝python-devel 和 mysql-devel:yum install python-devel mysql-devel,但是重啟mysqld 之后仍然出錯。(安裝后出現php下的mysql數據庫也無法連接的情況:修復數據庫mysqlcheck -A -o -r -p無效,修改用戶名密碼后可以,可能重裝mysql后用戶名不符合要求或權限發生改變)。

使用easy_install-2.7 MySQL-python 出現錯誤:

_mysql.c: In function ‘_mysql_ConnectionObject_getattr’:
_mysql.c:2444: 錯誤:‘_mysql_ConnectionObject’ 沒有名為 ‘open’ 的成員
error: Setup script exited with error: command ‘gcc’ failed with exit status 1

再使用Distutils安裝:
下載mysql-python:http://sourceforge.net/projects/mysql-python/
wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz/download
tar zxvf MySQL-python-1.2.3.tar.gz -C /opt
cd /opt/MySQL-python-1.2.3
python2.7 setup.py install
出現同樣錯誤:

_mysql.c:36:23: 錯誤:my_config.h:沒有那個文件或目錄
_mysql.c:38:19: 錯誤:mysql.h:沒有那個文件或目錄
_mysql.c:39:26: 錯誤:mysqld_error.h:沒有那個文件或目錄
_mysql.c:40:20: 錯誤:errmsg.h:沒有那個文件或目錄
_mysql.c:76: 錯誤:expected specifier-qualifier-list before ‘MYSQL’
_mysql.c:90: 錯誤:expected specifier-qualifier-list before ‘MYSQL_RES’
_mysql.c: In function ‘_mysql_Exception’:
_mysql.c:120: 警告:隱式聲明函數 ‘mysql_errno’
….
_mysql.c:2422: 錯誤:初始值設定元素不是常量
_mysql.c:2422: 錯誤:(在 ‘_mysql_ResultObject_memberlist[0].offset’ 的初始化附近)
_mysql.c: In function ‘_mysql_ConnectionObject_getattr’:
_mysql.c:2444: 錯誤:‘_mysql_ConnectionObject’ 沒有名為 ‘open’ 的成員
error: command ‘gcc’ failed with exit status 1

Linux Python如何安裝Mysqldb模塊這篇文章介紹解決辦法為:
vi site.cfg#mysql_config = /usr/local/bin/mysql_config 注釋去掉並改成本地的正確位置:
mysql_config = /usr/bin/mysql_config。但仍然出現同樣錯誤。查看提示 vi README,需要mysql-devel:

* Red Hat Linux packages:
- mysql-devel to compile
- mysql and/or mysql-devel to run

安裝 yum install mysql-devel,再執行:python2.7 setup.py install,編譯安裝Mysqldb模塊成功:
Installed /usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-i686.egg

然后再同步數據庫:cd -> syncdb
Would you like to create one now? (yes/no): 選擇no (osqa搭建好后注冊的第一個用戶為超級用戶)。

無法打開網站,
osqa網站沒有錯誤日志 /var/log/httpd/osqa/error_log;
查看Apache錯誤日志 vi /var/log/httpd/error_log: ImportError: No module named site

究竟是mod_wsgi還是django出現問題呢?
根據這篇文章提示進行測試。結果:mod_wsgi 沒問題,測試django時出現了問題:
執行命令:django-admin.py startproject hello創建python新Django項目。
提示:-bash: /usr/local/bin/django-admin.py: .: bad interpreter: 權限不夠 Permission denied
編輯:vi /usr/local/bin/django-admin.py 找到開頭:

#!.
from django.core import management

if __name__ == “__main__”:
management.execute_from_command_line()

vi /usr/local/lib/python2.7/site-packages/django/bin/django-admin.py 發現第一行和不一樣:
#!/usr/bin/env python 復制這一行到/usr/local/bin/django-admin.py第一行,即可順利建立hello項目。
cd /srv/www
django-admin.py startproject hello

修改Apache配置文件Listen 端口號之后,訪問網站出現:503 Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.

查看apache錯誤日志:vi /var/log/httpd/error_log

[Fri Sep 23 13:28:38 2011] [error] [client 183.62.15.26] (13)Permission denied: mod_wsgi (pid=32539): Unable to connect to WSGI daemon process ‘hello.djangoserver’ on ‘/etc/httpd/logs/wsgi.32500.0.1.sock’ after multiple attempts.

mod_wsgi 配置

vi /etc/httpd/conf.d/hello.conf 加入 WSGISocketPrefix /var/run/wsgi。測試Django成功:

It worked!
Congratulations on your first Django-powered page.
Of course, you haven’t actually done any work yet. Here’s what to do next:
If you plan to use a database, edit the DATABASES setting in hello/settings.py.
Start your first app by running python hello/manage.py startapp [appname].
You’re seeing this message because you have DEBUG = True in your Django settings file and you haven’t configured any URLs. Get to work!

確保mod_wsgi和django都測試成功之后,重啟httpd訪問osqa站,還是出現出現錯誤,等待片刻才行:

[Sat Sep 24 08:27:27 2011] [error] [client 183.62.15.26] mod_wsgi (pid=14167): Exception occurred processing WSGI script ‘/srv/www/wsgi/app.wsgi’.
[Sat Sep 24 08:27:27 2011] [error] [client 183.62.15.26] IOError: failed to write data

注意官方教程中設置 WSGIPythonHome 為這種:WSGIPythonHome /usr/local/lib/python2.7
這導致了上面提到的這個錯誤的出現:ImportError: No module named site
應該用 sys.prefix作為WSGIPythonHome的值,即:WSGIPythonHome /usr/local

繼續調試,提示:內部服務器錯誤500 Internal Server Error

[Sat Sep 24 02:18:19 2011] [error] [client 183.62.15.26]
ExtractionError: Can’t extract file(s) to egg cache
The following error occurred while trying to extract file(s) to the Python egg cache:
Permission denied: ‘/var/www/.python-eggs’
The Python egg cache directory is currently set to:
/var/www/.python-eggs
Perhaps your account does not have write access to this directory? You can change the cache directory by setting the PYTHON_EGG_CACHE environment variable to point to an accessible directory.

mkdir -p /var/python/eggs
/etc/httpd/conf/httpd.conf 文件加入 WSGIPythonEggs /var/python/eggs
設置了仍然無效,WSGIPythonEggs好像只對embedded mode 起作用?
同理httpd.conf設置 SetEnv PYTHON_EGG_CACHE /var/python/eggs 設置仍然無效;
mkdir -p /var/www/.python-eggs
chown -R apache:apache /var/www/.python-eggs
也無效.

只剩load_middleware的錯誤,去掉httpd.conf中的PYTHON_EGG_CACHE設置,重啟apache,終於OSQA界面出現!

—————————-最新版本配置文件—————————

WSGISocketPrefix /var/run/wsgi
WSGIPythonHome /usr/local/
WSGIPythonEggs /var/python/eggs

#NOTE: all urs below will need to be adjusted if
#settings.FORUM_SCRIPT_ALIAS !=” (e.g. = ‘forum/’)
#this allows “rooting” forum at [http://example.com/forum], if you like
Listen 8800

ServerAdmin admin@your.server.com
DocumentRoot /srv/www/osqa
ServerName farlee.info

#run mod_wsgi process for django in daemon mode
#this allows avoiding confused timezone settings when
#another application runs in the same virtual host
WSGIDaemonProcess osqa processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup osqa

#force all content to be served as static files
#otherwise django will be crunching images through itself wasting time
Alias /m/ /srv/www/osqa/forum/skins/

Order allow,deny
Allow from all

Alias /upfiles/ /srv/www/osqa/forum/upfiles/

Order allow,deny
Allow from all

#this is your wsgi script described in the prev section
WSGIScriptAlias / /srv/www/osqa/osqa.wsgi

CustomLog /var/log/httpd/osqa/access_log common
ErrorLog /var/log/httpd/osqa/error_log

關於osqa 1174版本一些問題,參考

1.無法搜索到標題中內容,無法搜索到問題的任何內容(tag 和user可以)
問題描述: http://meta.osqa.net/questions/10378/why-does-question-search-on-my-osqa-instance-not-work
搜索不到內容及解決辦法1:http://meta.osqa.net/questions/8198/why-search-engine-is-not-displaying-nothing-no-matter-what-keyword-i-enter
解決辦法2:http://meta.osqa.net/questions/8340/facebook-login-wont-login 去掉該模塊功能。
解決辦法3:使用sphinx 全文搜索http://meta.osqa.net/questions/9215/sphinx-search-isnt-work

http://meta.osqa.net/questions/3404/is-sphinx-search-actively-being-supported

http://meta.osqa.net/questions/6949/can-i-get-search-looking-in-answers-as-well-as-questions-with-mysql-db 搜索問題和答案。

2.搜索不到任何內容:
1). adding ‘mysqlfulltext’ into disabled modules from settings_local.py 然后可以搜索(使用默認搜索?)。
2). 答案中的文字內容搜不到:forum-》models-》question.py 加入class QuestionManager(NodeManager):
def search(self, keywords):
return False, self.filter(models.Q(title__icontains=keywords) | models.Q(body__icontains=keywords) | models.Q(children__body__icontains=keywords))

3. 啟用Bootstrap mode更容易獲得勛章

4. email 發送仍有問題。apache無錯誤日志,查看安裝目錄下的錯誤日志 vi /srv/www/osqa/log/django.osqa.log

/srv/www/osqa/forum/utils/mail.py TIME: 2011-09-26 05:13:32,636 MSG: mail.py:create_and_send_mail_messages:106 Email sending has failed: No SSL support included in this Python,

安裝openssl 后重新編譯Python

wget http://www.openssl.org/source/openssl-1.0.0e.tar.gz
tar zxf openssl-1.0.0e.tar.gz -C /opt
cd /opt/openssl-1.0.0e
./config
make
make install

修改Setup.dist

# Socket module helper for socket(2)
#_socket socketmodule.c

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto

# Andrew Kuchling’s zlib module.
# This require zlib 1.1.3 (or later).
# See http://www.gzip.org/zlib/
zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

重新編譯安裝python。完成后發送email測試,出現錯誤:

class HTTPSConnectionWithTimeout(httplib.HTTPSConnection):
AttributeError: ‘module’ object has no attribute ‘HTTPSConnection’

據說還要安裝openssl-devel,已經安裝的openssl-devel是0.9.8版本。因此openssl也重新編譯安裝和openssl-devel 0.9.8e相應的版本:openssl-0.9.8r.tar.gz。

$ ./config
$ make
$ make test
$ make install
再重新編譯python
./configure
make

提示現在Python版本其實並不需要修改Modules/Setup來啟用某些模塊了:

Previous versions of Python used a manual configuration process that
involved editing the file Modules/Setup. While this file still exists
and manual configuration is still supported, it is rarely needed any
more: almost all modules are automatically built as appropriate under
guidance of the setup.py script, which is run by Make after the
interpreter has been built.

If you rerun the configure script with different options, remove all
object files by running “make clean” before rebuilding. Believe it or
not, “make clean” sometimes helps to clean up other inexplicable
problems as well. Try it before sending in a bug report!

5. 修改后pyc文件不更新(python需要重新編譯時才自動檢查源代碼改動),手動編譯sudo python -m py_compile ‘/var/www/osqa/settings_local.py’。或者修改之后需要重啟apache(清除cache緩存):sudo /etc/init.d/apache2 restart

6. mail settings 使用googke apps
The e-mail settings of this community are not configured yet. We strongly recommend you to do that from the e-mail settings page as soon as possible.
Google app email:http://mail.google.com/support/bin/answer.py?answer=13287

7. 修改其他設置

參考osqa官方教程:OSQA RHEL, CentOS 5 Installation Guide

Ubuntu 下安裝osqa就方便多了,因為python 版本在ubuntu 10.04 之后一般都是python 2.6+。按官方教程配置即可:Ubuntu with Apache and MySQL


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM