我无法使用 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

在我的本地环境中,使用 Python 2.7.10,我的 Django 项目似乎 运行 使用 .manage.py 运行 服务器非常好。但是当我尝试使用相同版本的 python 2.7.10 将项目部署到我的 Debian Wheezy 服务器时,它遇到了 500 内部服务器错误。检查我的 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

我尝试转移到 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>

几天来我一直在努力解决这个问题,所以任何帮助都会非常感谢appreciated.Thank你!

尝试重新创建您的虚拟环境。

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

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/ link 作为有关如何将 virtualenv 与 apache 关联的详细信息。我建议你也试试 uwsgi。它与 DJango 一起玩得非常好。

"undefined symbol" 错误表明您的 psycopg2 安装可能有问题。我建议您卸载并重新安装它,并确保它没有使用任何 pip 缓存或任何东西,而是进行干净的构建。

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

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

再次感谢大家!