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

时间:2015-10-23 19:19:03

标签: python django python-2.7 mod-wsgi wsgi

在我的本地环境中,使用Python 2.7.10,我的Django项目似乎使用.manage.py runserver运行得非常好。但是当我尝试使用相同版本的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

我尝试了很多解决方案,例如下面所有这些链接都通过谷歌但仍无济于事。 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中的虚拟主机conf是

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

我一直试图解决这个问题几天,所以任何帮助都会受到高度赞赏。谢谢!

3 个答案:

答案 0 :(得分:0)

尝试重新创建你的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一起玩得非常好。

答案 1 :(得分:0)

&#34;未定义的符号&#34;错误表明你的psycopg2安装可能有问题。我建议你卸载并重新安装它,并确保它没有使用任何pip缓存或任何其他东西,但干净的构建。

答案 2 :(得分:0)

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

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

再次感谢大家!