我无法使用 Python 2.7.10 在我的生产环境中部署 Django 1.8.5

Posted

技术标签:

【中文标题】我无法使用 Python 2.7.10 在我的生产环境中部署 Django 1.8.5【英文标题】:I'm unable to deploy Django 1.8.5 on my production environment with Python 2.7.10 【发布时间】:2016-01-23 10:28:38 【问题描述】:

在我的本地环境中,使用 Python 2.7.10,我的 Django 项目使用 .manage.py runserver 似乎运行得非常好。但是当我尝试使用相同版本的 python 2.7.10 将项目部署到我的 Debian Wheezy 服务器时,它遇到了 500 internal server error。检查我的 apache 日志后,我发现错误在这两者之间交替出现:

[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201] mod_wsgi (pid=1973): Target WSGI script '/var/www/proj/proj/proj_wsgi.py' cannot be loaded as Python module.
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201] mod_wsgi (pid=1973): Exception occurred processing WSGI script '/var/www/proj/proj/proj_wsgi.py'.
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201] Traceback (most recent call last):
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]   File "/var/www/proj/proj/proj_wsgi.py", line 21, in <module>
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]     application = get_wsgi_application()
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]     django.setup()
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]     apps.populate(settings.INSTALLED_APPS)
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/apps/registry.py", line 78, in populate
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]     raise RuntimeError("populate() isn't reentrant")
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201] RuntimeError: populate() isn't reentrant

还有这个:

[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201] mod_wsgi (pid=1973): Target WSGI script '/var/www/proj/proj/proj_wsgi.py' cannot be loaded as Python module.
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201] mod_wsgi (pid=1973): Exception occurred processing WSGI script '/var/www/proj/proj/proj_wsgi.py'.
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201] Traceback (most recent call last):
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/proj/proj/proj_wsgi.py", line 21, in <module>
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     application = get_wsgi_application()
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     django.setup()
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     apps.populate(settings.INSTALLED_APPS)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     app_config.import_models(all_models)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     self.models_module = import_module(models_module_name)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     __import__(name)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/contrib/auth/models.py", line 41, in <module>
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     class Permission(models.Model):
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/db/models/base.py", line 139, in __new__
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     new_class.add_to_class('_meta', Options(meta, **kwargs))
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/db/models/base.py", line 324, in add_to_class
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     value.contribute_to_class(cls, name)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/db/models/options.py", line 250, in contribute_to_class
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     return getattr(connections[DEFAULT_DB_ALIAS], item)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/db/utils.py", line 240, in __getitem__
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     backend = load_backend(db['ENGINE'])
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/db/utils.py", line 111, in load_backend
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     return import_module('%s.base' % backend_name)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     __import__(name)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 24, in <module>
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201] ImproperlyConfigured: Error loading psycopg2 module: /var/www/ven/lib/python2.7/site-packages/psycopg2/_psycopg.so: undefined symbol: PyUnicodeUCS2_AsUTF8String

我尝试了许多解决方案,例如通过 Google 提供的所有这些链接,但仍然无济于事。 Django stops working with RuntimeError: populate() isn't reentrant Django populate() isn't reentrant

我尝试迁移到 python 2.7.3 并且 django 项目设法工作,但我需要 2.7.10 版本中包含的 pickle 中的一些编码功能,所以我需要使用它。

我什至尝试在 python 2.7.10 上从头开始重新安装一个全新的 Django 1.8.5 项目,但没有成功,出现相同的错误。

我的 proj_wgsi.py 是:

    import os
    import sys
    import site
    from django.core.wsgi import get_wsgi_application

    # Add the site-packages of the chosen virtualenv to work with
    site.addsitedir('/var/www/ven/lib/python2.7/site-packages')

    # Add the app's directory to the PYTHONPATH
    sys.path.append('/var/www/proj')
    sys.path.append('/var/www/proj/proj')


    # Activate your virtual env
    activate_env=os.path.expanduser('/var/www/ven/bin/activate_this.py')
    execfile(activate_env, dict(__file__=activate_env))

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings")


    application = get_wsgi_application()

我在 /etc/apache2/sites-enabled/000-default 中的 apache 中的虚拟主机配置是

<VirtualHost *:80>
    ServerName 128.133.218.444
    ServerAdmin webmaster@localhost
    ServerAlias 128.133.218.444
    WSGIDaemonProcess 128.133.218.444 python-path="/var/www/proj:/var/www/ven/lib/python2.7/site-packages"
    WSGIProcessGroup 128.199.218.180
    WSGIScriptAlias / /var/www/proj/proj/proj_wsgi.py process-group=128.199.218.180
    WSGIPassAuthorization On
    DocumentRoot /var/www/proj

    #<Directory />
    #   Options FollowSymLinks
    #   AllowOverride None
    #</Directory>
    #<Directory /var/www/>
    #   Options Indexes FollowSymLinks MultiViews
    #   AllowOverride None
    #   Order allow,deny
    #   allow from all
    #</Directory>
    <Directory /var/www/proj>
        Order allow,deny
        Allow from all
    </Directory>
    <Directory /var/www/proj/proj/static>
        Order deny,allow
        Allow from all
    </Directory>
    <Directory /var/www/proj/proj/media>
            Order deny,allow
            Allow from all
    </Directory>
    <Directory /var/www/proj/proj>
        <Files wsgi.py>
            Order allow,deny
            allow from all
        </Files>
    </Directory>

    #ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    #<Directory "/usr/lib/cgi-bin">
    #   AllowOverride None
    #   Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    #   Order allow,deny
    #   Allow from all
    #</Directory>

    ErrorLog $APACHE_LOG_DIR/error.log
    #ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog $APACHE_LOG_DIR/access.log combined
</VirtualHost>

这几天我一直在尝试解决这个问题,因此我们将非常感谢您的帮助。谢谢!

【问题讨论】:

我已经看到 RuntimeError: populate() isn't reentrant 错误发生在缺少像 psycopg2 这样的数据库驱动程序时。 @Brandon 我确实安装了 psycopg2。不过,此错误不会在 python 2.7.3 中发生。仅在我将 python 升级到 2.7.10 后才发生。 在这种情况下我会做的是开始注释掉registry.py 中的异常处理程序,直到您遇到实际问题。 过去当我遇到这样的问题时,这是我没有预料到的代码错误,直到我在生产中运行runserver 时才看到框来测试部署。你能做到吗? 我会注释掉这个:github.com/alsoicode/django/blob/master/django/apps/… 以及之后的任何异常处理程序,直到你找出真正的异常被吞下的原因。 【参考方案1】:

尝试重新创建您的 virtualenv。

如果还没有使用,请使用它。

sudo apt-get install python-virtualenv

这看起来像是与 postgres-python 安装相关的错误

virtualenv /some/dir/path
source /some/dir/path/bin/activate

Prepare django and psycopg2 and whatever other libraries you use

pip install psycopg2 django # possibly other libraries

这个http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/ 链接详细介绍了如何将您的virtualenv 与apache 关联。我建议你也试试 uwsgi。它与 DJango 配合得非常好。

【讨论】:

感谢您的回答。我照你说的做了,并用答案更新了问题。你的方法奏效了!【参考方案2】:

“未定义符号”错误表明您的 psycopg2 安装可能有问题。我建议您卸载并重新安装它,并确保它没有使用任何 pip 缓存或其他任何东西,而是进行干净的构建。

【讨论】:

【参考方案3】:

非常感谢大家帮助指导我解决这个挑战。我已经设法解决了这个问题。 首先,对于这个错误:

ImproperlyConfigured: Error loading psycopg2 module: /var/www/ven/lib/python2.7/site-packages/psycopg2/_psycopg.so: undefined symbol: PyUnicodeUCS2_AsUTF8String

我用./configure --enable-unicode=ucs4.重新编译了python v2.7.10 然后我用重新编译的版本重新创建了一个新的虚拟环境。

关于模块的第二个错误,我发现 Wayne Werner 所说的一些库版本错误,无法正常运行以与 django 项目兼容。我只是确保我升级了库(pip install -U [libraries])

再次感谢大家!

【讨论】:

以上是关于我无法使用 Python 2.7.10 在我的生产环境中部署 Django 1.8.5的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 PHP exec 运行 gsutil

我无法在我的 Python 项目中使用多线程

无法使用 Webpack 在生产环境中使用 Bootstrap CSS

将模块安装到 2.7.10 而不是 2.7.13 导致默认 python2.7.13 无法在 MAC 终端窗口上导入这些模块

我无法在我的 Sphinx 文档中使用 Python 突出显示颜料

python 批量生产10万接入用户