2015-10-16 133 views
0

我閱讀文檔並在Google上搜索,但我無法理解如何將Django與Apache結合在一起。 接下來應該做什麼?我不明白如何用mod_wsgi和Apache運行Django?

我的設置: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 
[email protected] 1 foo staff 3120 10 16 21:28 settings.py 
-rw-r--r-- 1 foo staff 3245 10 16 21:28 settings.pyc 
[email protected] 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 
[email protected] 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 
[email protected] 1 foo staff 887 10 16 21:23 views.py 
-rw-r--r-- 1 foo staff 1033 10 16 21:28 views.pyc 

您能否給我一些建議?

謝謝。

回答

1

你的WSGI腳本,並設置文件是在同一文件夾 嘗試改變

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

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

謝謝您的回覆,安東。我可以解決它。我很愚蠢,誤解。根成爲http:// localhost/wsgi /,所以我必須訪問http:// localhost/wsgi/myapp /。謝謝。 – yamachan

相關問題