在 RHEL 上配置和安装 python2.6.7 和 mod_wsgi3.3 以进行生产

发布于 2024-12-07 06:11:26 字数 5608 浏览 0 评论 0原文

这是一个很长的问题,详细说明了我从一开始所做的一切。希望有帮助。 我正在开发 Django 应用程序,需要将其部署到生产服务器上。生产服务器是IT管理的虚拟服务器,我没有root访问权限。他们授予我管理 /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 我将在其中 维护 python 2.6.7 安装将存储所需的源 tarball 并进行所有构建和安装。
  • /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.
>>>

因此,我按照 Building 的说明运行了配置脚本Python with --enable-shared in non-standard location as

./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 文档

  • Set WSGIPythonHome 到 /swadm/usr/local/python/2.6WSGIPythonPath/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 但无济于事。请让我知道我哪里出错了。 (抱歉,帖子很长)

TLDR;具有非 root 访问权限的系统默认安装了 python。我正在维护自己的 python 和 python 模块。使用自定义 python 配置和构建的 mod_wsgi,当我运行打印出 sys version_info 和路径的测试脚本时,仍然指向系统的 python。

更新: 在浏览 stackoverflow 时(应该早点完成),我在 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 的值。但我仍然遇到同样的错误。我正在调查更多。当我解决这个问题时会更新。

This is a long question detailing all that I did from the start. Hope it helps.
I am working on a django application and need to deploy it on to the production server. The production server is a virtual server managed by IT, and I do not have the root access. They have given me rights to manage the installations of my modules in /swadm and /home/swadm.
So I have planned to do create the following arrangement:

  • /swadm/etc/httpd/conf where I maintain httpd.conf
  • /swadm/etc/httpd/user-modules where I maintain my apache modules (mod_wsgi)
  • /swadm/var/www/django/app where I maintain my django code
  • /swadm/usr/local/python/2.6 where I will maintain my python 2.6.7 installation with modules like django, south etc.
  • /home/swadm/setup where I will be storing the required source tarballs and doing all the building and installing out of.
  • /home/swadm/workspace where I will be maintaining application code that is in development.

The system has python2.4.3 and python2.6.5 installed but IT recommended that I maintain my own python installation if I required lot of custom modules to be installed (which I would be).

So I downloaded python2.6.7 source. I needed to ensure python is installed such that its shared library is available. When I ran the configure script with only the option --enable-shared and --prefix=/swadm/usr/local/python/2.6, it would get installed but surprisingly point to the system's installation of 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.
>>>

So I ran the configure script following instructions from Building Python with --enable-shared in non-standard location as

./configure --enable-shared --prefix=/swadm/usr/local/python/2.6  LDFLAGS="-Wl,-rpath /swadm/usr/local/python/2.6/lib"

Also making sure I had created the directories beforehand ( as the link suggests) to avoid the errors expected. Now typing /swadm/usr/local/python/2.6/bin/python would start the correct python version 2.6.7. So I moved on to configuring and installing mod_wsgi. I configured it as

./configure --with-python=/swadm/usr/local/python/2.6/bin/python

the Makefile that was created tries to install the module into /usr/lib64/httpd/modules and I have no write permissions there, so I modified the makefile to install into /swadm/etc/httpd/user-modules. (There might be a command argument but I could not figure it out). The module got created fine. A test wsgi script which I used was

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]

And the output shown was, surprisingly

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']`

So you see somehow the mod_wsgi module still got configured with the system's python 2.6.5 installation and not my custom one.
I tried various things detailed in the mod_wsgi documentation

  • Set WSGIPythonHome in httpd.conf to /swadm/usr/local/python/2.6 and
    WSGIPythonPath to /swadm/usr/local/python/2.6/lib/python2.6
  • Created a symlink in the python config directory to point to the libpython2.6.so file

    $ ln -s ../../libpython2.6.so

When I do ldd libpython2.6.so this is what I see:

$ 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)

And ldd mod_wsgi.so gives

$ 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)

I have been trying re-installing and re-configuring python and mod_wsgi but to no avail. Please let me know where I am going wrong.
(Sorry for the very long post)

TLDR; System with non-root access has default python installation. I am maintaining my own python and python modules. mod_wsgi configured and built with the custom python, still points to the system's python when I run a test script that prints out the sys version_info and path.

UPDATE:
On Browsing through the stackoverflow (should have done it earlier) I found this answer by Graham Dumpleton on mod_wsgi python2.5 ubuntu 11.04 problem which solved the error for me. Now when I do ldd mod_wsgi.so I see that it is linked to the correct shared library of python. I now installed Django and MySQLdb using my custom python install. And Now I am facing this error:

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.

So I did change the value of PYTHON_EGG_CACHE by doing export PYTHON_EGG_CACHE=/swadm/var/www/.python-eggs. but I am still getting the same error. I am investigating more. Will update when I solve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南城旧梦 2024-12-14 06:11:27

通过在 WSGI 脚本中设置环境变量解决了 Egg 缓存问题:

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

使用后两者中的哪一个取决于是使用 emebedded 模式还是守护程序模式。

Egg cache issue solved by setting environment variable in WSGI script:

http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Access_Rights_Of_Apache_User

or in Apache configuration:

http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIPythonEggs
http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess

Which of latter two is used depends on whether using emebedded mode or daemon mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文