【问题标题】:Django and requirementsDjango 和要求
【发布时间】:2016-10-10 02:23:19
【问题描述】:

我有点卡住了。我不能让这个配置工作,我不知道为什么。我在下面的代码来自https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04。我坚持下去的原因是因为他将他的项目目录和他的项目命名为相同的东西。如果我是对的,那么他在哪里定位诸如 settings.py 和 wsgi.py 之类的文件应该是 /home/user/myproject/myproject/myproject 但我不确定了,因为我自己什至无法正确处理。在文档的前面,他 cd 进入了他创建的目录,这会将他放在 /home/user/myproject 中。然后他继续创建一个虚拟环境,输入它并运行django-admin startproject myproject。所以,如果这一切都成立,至少我在我自己的服务器上看到的告诉我,当你启动一个 django 项目时,它实际上会创建两个同名的文件夹,嵌套。我错了吗?有人可以帮我理顺下面的代码以使其更有意义吗?

. . .

    Alias /static /home/user/myproject/static
    <Directory /home/user/myproject/static>
        Require all granted
    </Directory>

    <Directory /home/user/myproject/myproject>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess myproject python-path=/home/user/myproject:/home/user/myproject/myprojectenv/lib/python2.7/site-packages
    WSGIProcessGroup myproject
    WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py

</VirtualHost>

这是我在 apache 日志中一直看到的内容:

[Sun Oct 09 11:48:15.875313 2016] [wsgi:warn] [pid 47964] mod_wsgi: Compiled for Python/3.5.1+.
[Sun Oct 09 11:48:15.875353 2016] [wsgi:warn] [pid 47964] mod_wsgi: Runtime using Python/3.5.2.
[Sun Oct 09 11:48:15.877537 2016] [mpm_prefork:notice] [pid 47964] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Sun Oct 09 11:48:15.877568 2016] [core:notice] [pid 47964] AH00094: Command line: '/usr/sbin/apache2'
[Sun Oct 09 11:48:18.767800 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] mod_wsgi (pid=47967): Target WSGI script '/home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py' cannot be loaded as Python module.
[Sun Oct 09 11:48:18.767851 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] mod_wsgi (pid=47967): Exception occurred processing WSGI script '/home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py'.
[Sun Oct 09 11:48:18.768339 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] Traceback (most recent call last):
[Sun Oct 09 11:48:18.768385 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]   File "/home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py", line 16, in <module>
[Sun Oct 09 11:48:18.768389 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]     application = get_wsgi_application()
[Sun Oct 09 11:48:18.768395 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]   File "/usr/lib/python3/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Sun Oct 09 11:48:18.768398 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]     django.setup()
[Sun Oct 09 11:48:18.768405 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]   File "/usr/lib/python3/dist-packages/django/__init__.py", line 17, in setup
[Sun Oct 09 11:48:18.768408 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Sun Oct 09 11:48:18.768413 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]   File "/usr/lib/python3/dist-packages/django/conf/__init__.py", line 48, in __getattr__
[Sun Oct 09 11:48:18.768423 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]     self._setup(name)
[Sun Oct 09 11:48:18.768430 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]   File "/usr/lib/python3/dist-packages/django/conf/__init__.py", line 44, in _setup
[Sun Oct 09 11:48:18.768433 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]     self._wrapped = Settings(settings_module)
[Sun Oct 09 11:48:18.768438 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]   File "/usr/lib/python3/dist-packages/django/conf/__init__.py", line 92, in __init__
[Sun Oct 09 11:48:18.768441 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Sun Oct 09 11:48:18.768446 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]   File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Sun Oct 09 11:48:18.768449 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]     return _bootstrap._gcd_import(name[level:], package, level)
[Sun Oct 09 11:48:18.768454 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Sun Oct 09 11:48:18.768460 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Sun Oct 09 11:48:18.768466 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]   File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Sun Oct 09 11:48:18.768471 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]   File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Sun Oct 09 11:48:18.768477 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Sun Oct 09 11:48:18.768483 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Sun Oct 09 11:48:18.768488 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662]   File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Sun Oct 09 11:48:18.768505 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] ImportError: No module named 'servicesite'

【问题讨论】:

  • 我不确定你的问题是什么。您只需要使 Apache 配置中的目录与您的目录布局相匹配,无论它是什么。
  • 您是否阅读过有关如何设置 mod_wsgi 的官方 Django 文档? docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi
  • @Graham 这是同样的斗争。假设我在~/projects/projectfolder。然后我run django-admin startproject mysitedjango generated 文件夹变为~/projects/projectfolder/mysite,settings.py 和wsgi.py 的位置变为~/projects/projectfolder/mysite/mysite 所以在查看站点文档时,我不得不假设mysite.com 只是项目文件夹他们创建并进入该目录,然后创建了 mysite 项目。
  • @Graham-Dumpleton 采用上述方法(请记住,我在这篇文章中使用 ~ 来保持路径短......马上WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py 变得错误。使用我的例子,@ 987654331@ 是文件的正确路径,而不是~/projects/projectfolder/mysite/wsgi.py。假设mysite.com 是他们创建的django-admin 项目,那么为什么二级文件夹是mysite 而不是mysite.com?当我创建项目时,它会在项目文件夹中创建./projectname/projectname./manage.py
  • @GrahamDumpleton 有趣的是,mysite.com 不是一个有效的项目名称,这只会进一步证明我的观点。

标签: python django apache mod-wsgi


【解决方案1】:

在你的 /etc/apache2/apache2.conf 文件中添加

<Directory /home/dimitris/mysite>
    AllowOverride All
    Require all granted
</Directory>

示例中的您的 site.conf 文件我使用 virtualenv 加载 mysite

WSGIDaemonProcess myp user=dimitris group=dimitris threads=5  python-path=/home/dimitris/myenv/lib/python2.7/site-packages

<VirtualHost *:80>
    #ServerName mysite
    #ServerAlias mysite
    ServerAdmin webmaster@localhost
    WSGIProcessGroup myp
    WSGIScriptAlias / /home/dimitris/mysite/mysite/wsgi.py

    Alias /favicon.ico /home/dimitris/mysite/favicon.ico
    Alias /media/ /home/dimitris/mysite/media/
    Alias /static/ /home/dimitris/mysite/project-static/
    DocumentRoot /home/dimitris/mysite
    <Directory /home/dimitris/mysite/>
            Order allow,deny
            allow from all
    </Directory>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
    ErrorLog /var/log/mysite-error.log
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog /var/log/mysite-access.log combined
</VirtualHost>

在 django wsgi.py 文件中添加

import os
DJANGO_PATH =  os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
sys.path.append(DJANGO_PATH)

在上面的代码中,将 dimitris 替换为您的用户名

【讨论】:

    【解决方案2】:

    您可以在 /home/user 中创建项目,或者通过在指定路径的任何位置附加额外的 /myproject 来更改 apache conf 文件,如下所示:

    Alias /static /home/user/myproject/myproject/static
    <Directory /home/user/myproject/myproject/static>
        Require all granted
    </Directory>
    
    <Directory /home/user/myproject/myproject/myproject>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    
    WSGIDaemonProcess myproject python-path=/home/user/myproject/myproject:/home/user/myproject/myproject/myprojectenv/lib/python2.7/site-packages
    WSGIProcessGroup myproject
    WSGIScriptAlias / /home/user/myproject/myproject/myproject/wsgi.py
    

    这很令人困惑,因为您在 myproject 文件夹中创建了项目(与您的项目名称相同),而不是在其他地方创建它。

    【讨论】:

      猜你喜欢
      • 2013-04-12
      • 2021-07-13
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 2020-03-29
      • 2021-05-22
      相关资源
      最近更新 更多