在RHEL上为生产配置和安装python2.6.7及mod_wsgi3.3
这是一个很长的问题,详细描述了我从一开始所做的所有事情。希望这些信息能帮到你。 我正在开发一个django应用程序,需要将其部署到生产服务器上。这个生产服务器是一个由IT部门管理的虚拟服务器,我没有根权限。他们给了我在/swadm和/home/swadm目录下管理我的模块安装的权限。 所以我计划创建以下的安排:
/swadm/etc/httpd/conf
,在这里我维护httpd.conf文件/swadm/etc/httpd/user-modules
,在这里我维护我的apache模块(mod_wsgi)/swadm/var/www/django/app
,在这里我维护我的django代码/swadm/usr/local/python/2.6
,在这里我将维护我的python 2.6.7安装,包含django、south等模块/home/swadm/setup
,在这里我将存放所需的源代码压缩包,并进行所有的构建和安装/home/swadm/workspace
,在这里我将维护正在开发的应用程序代码
系统已经安装了python2.4.3和python2.6.5,但IT部门建议我如果需要安装很多自定义模块,就应该维护我自己的python安装(我确实需要)。
于是我下载了python2.6.7的源代码。我需要确保python安装时其共享库是可用的。当我仅用--enable-shared
和--prefix=/swadm/usr/local/python/2.6
选项运行配置脚本时,它会被安装,但奇怪的是却指向了系统的python2.6.5安装。
$ /swadm/usr/local/python/2.6/bin/python
Python 2.6.5 (r265:79063, Feb 28 2011, 21:55:45)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
所以我按照在非标准位置构建带有--enable-shared的Python的说明运行了配置脚本,
./configure --enable-shared --prefix=/swadm/usr/local/python/2.6 LDFLAGS="-Wl,-rpath /swadm/usr/local/python/2.6/lib"
同时确保我提前创建了目录(如链接所示),以避免预期中的错误。现在输入/swadm/usr/local/python/2.6/bin/python
可以启动正确的python版本2.6.7。接下来我开始配置和安装mod_wsgi。我这样配置它:
./configure --with-python=/swadm/usr/local/python/2.6/bin/python
生成的Makefile试图将模块安装到/usr/lib64/httpd/modules
,但我没有写权限,所以我修改了Makefile,将其安装到/swadm/etc/httpd/user-modules
。(可能有命令参数,但我没能弄明白)。模块创建得很好。我使用的测试wsgi脚本是:
import sys
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'
output = output + str(sys.version_info)
output = output + '\nsys.prefix = %s' % repr(sys.prefix)
output = output + '\nsys.path = %s' % repr(sys.path)
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
输出结果出乎意料:
Hello World!(2, 6, 5, 'final', 0)
sys.prefix = '/swadm/usr/local/python/2.6'
sys.path = ['/swadm/usr/local/python/2.6/lib64/python26.zip', '/swadm/usr/local/python/2.6/lib64/python2.6/', '/swadm/usr/local/python/2.6/lib64/python2.6/plat-linux2', '/swadm/usr/local/python/2.6/lib64/python2.6/lib-tk', '/swadm/usr/local/python/2.6/lib64/python2.6/lib-old', '/swadm/usr/local/python/2.6/lib64/python2.6/lib-dynload']`
所以你看,mod_wsgi模块仍然配置为使用系统的python 2.6.5安装,而不是我自定义的版本。 我尝试了在mod_wsgi文档中详细描述的各种方法:
- 在httpd.conf中设置
WSGIPythonHome
为/swadm/usr/local/python/2.6
,并将WSGIPythonPath
设置为/swadm/usr/local/python/2.6/lib/python2.6
在python配置目录中创建一个符号链接,指向libpython2.6.so文件
$ ln -s ../../libpython2.6.so
当我执行ldd libpython2.6.so
时,看到的结果是:
$ ldd libpython2.6.so
linux-vdso.so.1 => (0x00007fffc47fc000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b666ed62000)
libdl.so.2 => /lib64/libdl.so.2 (0x00002b666ef7e000)
libutil.so.1 => /lib64/libutil.so.1 (0x00002b666f182000)
libm.so.6 => /lib64/libm.so.6 (0x00002b666f385000)
libc.so.6 => /lib64/libc.so.6 (0x00002b666f609000)
/lib64/ld-linux-x86-64.so.2 (0x00000031aba00000)
而ldd mod_wsgi.so
的结果是:
$ ldd /swadm/etc/httpd/user-modules/mod_wsgi.so
linux-vdso.so.1 => (0x00007fff1ad6e000)
libpython2.6.so.1.0 => /usr/lib64/libpython2.6.so.1.0 (0x00002af03aec7000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00002af03b270000)
libdl.so.2 => /lib64/libdl.so.2 (0x00002af03b48c000)
libutil.so.1 => /lib64/libutil.so.1 (0x00002af03b690000)
libm.so.6 => /lib64/libm.so.6 (0x00002af03b893000)
libc.so.6 => /lib64/libc.so.6 (0x00002af03bb17000)
/lib64/ld-linux-x86-64.so.2 (0x00000031aba00000)
我一直在尝试重新安装和重新配置python和mod_wsgi,但都没有成功。请告诉我我哪里出错了。 (抱歉发了这么长的帖子)
总结:系统没有根权限,默认的python安装。我在维护自己的python和python模块。mod_wsgi使用自定义python配置和构建,但在运行测试脚本时仍然指向系统的python。
更新:
在浏览StackOverflow时(早该这么做),我发现了Graham Dumpleton在mod_wsgi python2.5 ubuntu 11.04问题中的回答,这解决了我的错误。现在当我执行ldd mod_wsgi.so
时,看到它链接到了正确的python共享库。我现在使用我的自定义python安装安装了Django和MySQLdb。现在我遇到了这个错误:
The following error occurred while trying to extract file(s) to the Python egg
cache:
[Errno 13] 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.
所以我通过执行export PYTHON_EGG_CACHE=/swadm/var/www/.python-eggs
来更改PYTHON_EGG_CACHE
的值,但仍然遇到同样的错误。我正在进一步调查。解决后会更新。
1 个回答
解决Egg缓存问题的方法是通过在WSGI脚本中设置环境变量:
http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Access_Rights_Of_Apache_User
或者在Apache配置中设置:
http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIPythonEggs http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess
使用哪种方法取决于你是用嵌入模式还是守护进程模式。