我有一个不错的Django项目,运行在127.0.0.1:8000。我通常通过以下方式运行它:
$ python manage.py runserver
现在,我想要在下运行它Apache
。
这是我尝试的:
已安装并启用mod_wsgi。它确实可以正常工作,因为安装后我在控制台中看到了:"apache2_invoke: Enable module wsgi"
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
而已。我不确定自己缺少什么,但是重新启动apache
并转到后localhost
,在我的浏览器中,我看到了/var/www
文件夹中的页面。而我希望看到Django应用程序。我想我需要编辑apache2.conf
(如何?)并做一些其他魔术(例如下载并安装一些额外的魔术东西,进行一些魔术apache配置等)。
我尝试了几十个教程,但都没有帮助-要么Apache没有重新启动,要么Django项目没有在浏览器中呈现。因此,我需要遵循一些基本步骤才能使其正常工作。
编辑
当我尝试在标签Files
内没有标签的情况下启动apache时Directory
,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
。可能我们错过WSGIPythonPath
了apacheconf
文件中的变量。但是我不确定在现实情况下应该指向何处,而不是抽象情况下。如果我遵循一些教程并将其设置为WSGIPythonPath /home/jacobian/django/apps/
,则apache无法启动。因此,我不知道该怎么办。
通常,为django配置apache2 + wsgi非常容易;但是,如果您错过任何步骤,则找出问题可能很麻烦;只需尝试迭代下面提到的步骤,它将有助于您找出问题所在。
以下是您可能要尝试的步骤:
[根据用户需求进行编辑]
*重命名了某些字段,以使其更易于理解
假设您在/ 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]
安装Apache
apt-get install apache2
安装mod_wsgi
apt-get安装libapache2-mod-wsgi
制作一个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()
把它所有的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>
启用该网站
须藤a2ensite sitename.conf
重新启动Apache
sudo服务apache2重新启动
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句