无法在Apache下运行Django项目

时间:2015-09-21 07:59:57

标签: python django apache

我有一个很好的Django项目,运行在127.0.0.1:8000。我通常通过以下方式运行它:

$ python manage.py runserver

现在,我想要的是在Apache下运行它。

这就是我的尝试:

  1. 已安装并启用了mod_wsgi。它确实有效,因为当我安装它时,我在控制台中看到:"apache2_invoke: Enable module wsgi"

  2. 以这种方式编辑000-default.conf文件:

    <VirtualHost *:80>
    WSGIScriptAlias / /home/jacobian/django/apps/apps/wsgi.py
    <Directory /home/jacobian/django/apps/apps/>
        <Files wsgi.py>
           Require all granted
        </Files>
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    

  3. 就是这样。我不确定她是怎么回事,但在我重新启动apache并转到localhost后,在我的浏览器中,我看到/var/www文件夹中的一个页面。而我希望看到一个django应用程序。我想我需要编辑apache2.conf(如何?)并做一些其他的魔术技巧(比如下载并安装一些额外的魔法内容,制作一些神奇的apache配置等)。

    我尝试了几十个教程,但没有一个帮助 - 要么apache没有重启,要么django项目没有在浏览器中呈现。因此,我需要遵循一些基本步骤才能使其发挥作用。

    修改

    当我尝试在标记Files内没有标记Directory的情况下启动apache时,apache无法执行此操作,但同时我在error.log中看不到其他消息。但是,如果我使用此标记:

    <Directory /home/jacobian/django/apps/apps/>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    

    然后我在error.log中收到一堆错误消息。这是整个清单:

    [Mon Sep 21 13:55:45.901935 2015] [wsgi:warn] [pid 39222:tid 140543251101568] mod_wsgi: Compiled for Python/2.7.8.
    [Mon Sep 21 13:55:45.901995 2015] [wsgi:warn] [pid 39222:tid 140543251101568] mod_wsgi: Runtime using Python/2.7.9.
    [Mon Sep 21 13:55:45.902149 2015] [mpm_event:notice] [pid 39222:tid 140543251101568] AH00489: Apache/2.4.10 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.9 configured -- resuming normal operations
    [Mon Sep 21 13:55:45.902186 2015] [core:notice] [pid 39222:tid 140543251101568] AH00094: Command line: '/usr/sbin/apache2'
    [Mon Sep 21 13:55:46.851442 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] mod_wsgi (pid=41314): Target WSGI script '/home/jacobian/django/apps/apps/wsgi.py' cannot be loaded as Python module.
    [Mon Sep 21 13:55:46.851498 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] mod_wsgi (pid=41314): Exception occurred processing WSGI script '/home/jacobian/django/apps/apps/wsgi.py'.
    [Mon Sep 21 13:55:46.851526 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] Traceback (most recent call last):
    [Mon Sep 21 13:55:46.851557 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601]   File "/home/jacobian/django/apps/apps/wsgi.py", line 14, in <module>
    [Mon Sep 21 13:55:46.851696 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601]     application = get_wsgi_application()
    [Mon Sep 21 13:55:46.851734 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601]   File "/usr/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    [Mon Sep 21 13:55:46.851800 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601]     django.setup()
    [Mon Sep 21 13:55:46.851818 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601]   File "/usr/lib/python2.7/dist-packages/django/__init__.py", line 20, in setup
    [Mon Sep 21 13:55:46.851874 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
    [Mon Sep 21 13:55:46.851892 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601]   File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 46, in __getattr__
    [Mon Sep 21 13:55:46.851994 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601]     self._setup(name)
    [Mon Sep 21 13:55:46.852013 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601]   File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
    [Mon Sep 21 13:55:46.852038 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601]     self._wrapped = Settings(settings_module)
    [Mon Sep 21 13:55:46.852053 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601]   File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 98, in __init__    
    [Mon Sep 21 13:55:46.852072 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601]     % (self.SETTINGS_MODULE, e)
    [Mon Sep 21 13:55:46.852097 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] ImportError: Could not import settings 'apps.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named apps.settings
    

    我不确定wsgi.py文件有什么问题。这是使用$ django-admin startproject apps创建的标准文件。可能我们错过了apache WSGIPythonPath文件中的conf变量。但是我不确定在现实世界中它应该指向何处,而不是在一些抽象的情况下。如果我按照一些教程并将其设置为WSGIPythonPath /home/jacobian/django/apps/,则apache无法启动。所以,我不知道我能做些什么。

1 个答案:

答案 0 :(得分:2)

通常很容易为django配置apache2 + wsgi;但是如果你错过了任何一步,那么追查问题会很麻烦;只是尝试迭代下面提到的步骤,它将帮助您找出问题。

请找到django documenatation on this here.

以下是您可能想要尝试的步骤:

[编辑以满足用户需求]

*重新启动某些字段以使其更容易理解

让我们说你在/ home / jacobian / django /

创建了你的django项目
django_project/
|-- django_project
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   |-- wsgi.py
`-- manage.py

它应该类似于上面的树,你在apache中指定wsgi的路径 将是:

/home/jacobian/django/django_project/django_project/wsgi.py

或其他方面

PROJECT_PATH意味着/home/jacobian/django/ PROJECT_NAME意味着django_project [特定于Ubuntu]

  1. 安装Apache

    apt-get install apache2

  2. 安装mod_wsgi

    apt-get install libapache2-mod-wsgi

  3. 制作WSGI处理程序脚本;根据django安装的版本,你可以这样做:

    import os
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_project.settings")
    
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    
  4. 将其全部写入apache conf,将其写入sitename.conf

    中的/etc/apache2/sites-available/新文件
    <VirtualHost *:80>
      WSGIScriptAlias / PROJECT_PATH/PROJECT_NAME/PROJECT_NAME/wsgi.py
      Alias /static PROJECT_PATH/PROJECT_NAME/static/
      Alias /media PROJECT_PATH/PROJECT_NAME/media/
    
     ServerName www.example.com
    
     <Directory />
       Options -Indexes
     </Directory>
     <Directory PROJECT_PATH/PROJECT_NAME/>
       Order allow,deny
       Allow from all
       Options -Indexes
     </Directory>
    
    </VirtualHost> 
    
  5. 启用该网站

    sudo a2ensite sitename.conf

  6. 重启Apache

    sudo service apache2 restart