我不知道如何用mod_wsgi和Apache运行Django?

时间:2015-10-16 14:39:01

标签: python django macos apache mod-wsgi

我在Google上阅读了文档并进行了搜索,但我无法理解如何将Django与Apache结合起来。 接下来我该怎么办?

我的设置:UnicodeDecodeError with Django when I execute syncdb

$ python -V
    2.7.10

$ httpd -v
    2.4.16

$ python
    import django
    print django.get_version()
        1.8.5

$ brew tap homebrew/apache

$ brew install mod_wsgi

$ brew info mod_wsgi    

$ mkdir -p ~/Sites

$ chmod 755 ~/Sites

$ cd ~/Sites

$ django-admin.py startproject myproject

$ cd myproject

$ python manage.py startapp myapp

$ sudo vi /etc/apache2/httpd.conf  # remove comment
    Include /private/etc/apache2/extra/httpd-userdir.conf


$ sudo vi /etc/apache2/extra/httpd-userdir.conf  # remove comment
    Include /private/etc/apache2/users/*.conf

$ sudo chmod 644  /etc/apache2/users/$USER.conf

$ sudo vi /etc/apache2/users/$USER.conf
    <Directory "/Users/$USER/Sites/">
        Options Indexes Multiviews FollowSymlinks
        AllowOverride all
        Require all granted
    </Directory>

$ sudo vi /private/etc/apache2/other/django.conf
    LoadModule wsgi_module /usr/local/Cellar/mod_wsgi/4.4.11/libexec/mod_wsgi.so
    WSGIScriptAlias /wsgi /Users/$USER/Sites/myproject/myproject/wsgi.py
    #WSGIPythonPath /Users/$USER/Sites/myproject

    <Directory /Users/$USER/Sites/myproject/myproject>
    <Files wsgi.py>
        Require all granted
    </Files>
    </Directory>

$ vi myproject/wsgi.py
    import os, site, sys
    from django.core.wsgi import get_wsgi_application
    # sys.path.append('/Users/$USER/Sites/myproject')
    # sys.path.append('/Users/$USER/Sites/myproject/myproject')
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
    application = get_wsgi_application()

$ sudo apachectl graceful

然后,我访问了http://localhost/wsgi/http://localhost/myapp/等等,但是......

$ tail /private/var/log/apache2/error_log
File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__
  self._setup(name)
File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 44, in _setup
  self._wrapped = Settings(settings_module)
File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 92, in __init__
  mod = importlib.import_module(self.SETTINGS_MODULE)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
  __import__(name)
ImportError: No module named myproject.settings

据推测,我的许可是错的。 但我不知道该怎么做。

$ cd && ls -l
    drwxr-xr-x   7 foo  staff   238 10 16 20:56 Sites

$ cd Sites/ && ls -l
    drwxr-xr-x  9 foo  staff  306 10 16 22:23 myproject

$ cd myproject/ && ls -l
-rw-r--r--   1 foo  staff  12288 10 16 20:57 db.sqlite3
-rwxr-xr-x   1 foo  staff    252 10 16 20:56 manage.py
drwxr-xr-x  13 foo  staff    442 10 16 21:28 myapp
drwxr-xr-x  11 foo  staff    374 10 16 22:39 myproject
drwxr-xr-x   4 foo  staff    136 10 16 21:30 templates
-rwxr-xr-x   1 foo  staff    275 10 16 22:23 wsgi-test.py

$ ls -l myproject/
-rw-r--r--  1 foo  staff     0 10 16 20:56 __init__.py
-rw-r--r--  1 foo  staff   139 10 16 20:56 __init__.pyc
-rw-r--r--@ 1 foo  staff  3120 10 16 21:28 settings.py
-rw-r--r--  1 foo  staff  3245 10 16 21:28 settings.pyc
-rw-r--r--@ 1 foo  staff  1070 10 16 21:20 urls.py
-rw-r--r--  1 foo  staff  1129 10 16 21:28 urls.pyc
-rw-r--r--  1 foo  staff   518 10 16 22:38 wsgi.py
-rw-r--r--  1 foo  staff   600 10 16 20:57 wsgi.pyc

$ ls -l myapp/
-rw-r--r--  1 foo  staff     0 10 16 20:56 __init__.py
-rw-r--r--  1 foo  staff   135 10 16 21:28 __init__.pyc
-rw-r--r--  1 foo  staff    63 10 16 20:56 admin.py
-rw-r--r--  1 foo  staff   192 10 16 21:28 admin.pyc
drwxr-xr-x  4 foo  staff   136 10 16 21:28 migrations
-rw-r--r--@ 1 foo  staff    82 10 16 21:04 models.py
-rw-r--r--  1 foo  staff   189 10 16 21:28 models.pyc
-rw-r--r--  1 foo  staff    60 10 16 20:56 tests.py
-rw-r--r--@ 1 foo  staff   887 10 16 21:23 views.py
-rw-r--r--  1 foo  staff  1033 10 16 21:28 views.pyc

请你给我一些建议吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

您的wsgi脚本和设置文件位于同一文件夹中 尝试更改

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

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