【问题标题】:Deploy django with apache2使用 apache2 部署 django
【发布时间】:2014-05-20 10:32:59
【问题描述】:

(我有点不知所措)我正在尝试在 apache2 上部署我的 django 服务器。我已经构建了一个相当大的前端应用程序(当前使用 apache2 部署)并且不想通过 django 提供任何视图,而是希望将后端作为服务主体。

我已按照此处的说明进行操作:

https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/modwsgi

在 modwsgi 部分之后,我在我的 apache 日志文件中得到了这个:

  [Tue May 20 12:19:44 2014] [notice] Apache/2.2.22 (Debian) PHP/5.4.4-14+deb7u8 mod_wsgi/3.4 Python/2.7.3 configured -- resuming normal operations

将其附加到 apache 配置文件后:

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

WSGIScriptAlias / /var/www/PhotodiceServer/PhotodiceServer/wsgi.py
WSGIPythonPath /var/www/PhotodiceServer

<Directory /var/www/PhotodiceServer/PhotodiceServer>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

这是wsgi文件的内容:

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

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

这是 urls.py 文件的内容:

from django.conf.urls import patterns, include, url
from django.contrib.auth.models import User, Group
from rest_framework import viewsets, routers

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browseable API.
url(r'^admin/', include(admin.site.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
)
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
model = User

class GroupViewSet(viewsets.ModelViewSet):
model = Group


# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'groups', GroupViewSet)

这是已安装的应用程序:

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'photod',
)

之前,当我输入网络服务器(树莓派)的 ip 时,我会自动路由到存储在 var/www/ 中的 index.html。

现在,当我输入 webserve 的 ip 时,找不到 404 页面,我是否必须明确说明什么 url 会以某种方式加载静态文件?以及这将如何与角度路由一起使用? (我用过angular-ui-router.js)?

(如果我设置 debug =False 我会得到 Bad Request (400))

【问题讨论】:

  • 几个问题:您为什么使用过时的指南(该页面顶部有一个红色警告,请尝试使用实际安装的指南:docs.djangoproject.com/en/1.6/howto/deployment/wsgi/modwsgi)。另一个是:为什么是 Apache?有更好的替代品,例如 Nginx(更好的性能、安全性等)。此外,发布您的实际配置可能会很有帮助,而不仅仅是指向您遵循的指南的链接。这意味着:您的 Apache .conf 文件,您安装该 Django 应用程序的方式/位置。
  • 谢谢,我在更改为其他指南后编辑了帖子。为什么是阿帕奇?让我们说我的名字是约翰·斯诺(我什么都不知道)。我已经在我的 var/www 指令中安装了 django 应用程序...使用我认为的 starproject 命令...我已经尝试配置了一段时间不记得确切
  • Internal Server Error 表示您的应用配置存在一些错误。你能把你wsgi.py的内容放在这里吗?还可以尝试将DEBUG=True 放在应用程序的设置中,这样任何请求都会显示错误回溯而不是简洁的错误消息。您也可以尝试在该服务器上运行manage.py runserver(可以吗,对吗?)并在该服务器上的某些基于控制台的浏览器中显示该页面(链接)或将其存储在html文件wget(wget http://localhost:8000)中跨度>
  • 看起来您想要做的是使用 django 作为 Angular 的 REST 后端。阅读this tutorial,它会向您展示如何做到这一点。 what are the best practices to use AngularJS with django 问题也很有用;最后django-angular 也刚刚发布。
  • 我删除了angularjs 标签,因为这个问题不是关于角度的,而是使用 mod_wsgi 设置 django。碰巧您正在使用 angular 作为 django 应用程序的前端,但这与此无关;您还在一篇文章中提出了多个问题。请将帖子限制在一个(可回答的)问题上。

标签: python django apache


【解决方案1】:

我认为您在 Apache 配置中遗漏了一些东西:

  1. AddHandler wsgi-script .py
  2. &lt;Directory xxx&gt; Options +ExecCGI SetHandler wsgi-script

试试这些,然后看看它会说什么。

编辑***因为你显然不熟悉 apache!

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

AddHandler wsgi-script .py
WSGIScriptAlias / /var/www/PhotodiceServer/PhotodiceServer/wsgi.py
WSGIPythonPath /var/www/PhotodiceServer

<Directory /var/www/PhotodiceServer/PhotodiceServer>
    Options +ExecCGI
    <Files wsgi.py>
        Order deny,allow
        Allow from all
    </Files>
</Directory>

另一个编辑***

这是我使用的配置,(虽然最后我只是运行了 uWSGI 服务器和 ProxyPass,因为这有点痛苦!!)

<VirtualHost *:80>
    ServerName my.local.domain

    SetEnv APPLICATION_ENV development

    Alias /favicon.ico /path/to/project/favicon.ico

    AliasMatch ^/static/(.*)$ /path/to/project/static/$1

    AddHandler wsgi-script .py
    WSGIScriptAlias / /path/to/project/project/wsgi.py

    WSGIDaemonProcess _WSGI user=chazmead group=www-data processes=1 threads=5 python-path=/path/to/project/venv/lib/python2.7/site-packages:/path/to/project
    WSGIProcessGroup _WSGI

    <Directory "/path/to/project/project/">
        SetHandler wsgi-script
        Options Indexes FollowSymLinks ExecCGI

        Order Allow,Deny
        Allow from all
    </Directory>

</VirtualHost>

【讨论】:

  • 2.是什么意思? Options +ExecCGI SetHandler wsgi-script ....
  • LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so WSGIScriptAlias / /var/www/PhotodiceServer/PhotodiceServer/wsgi.py WSGIPythonPath /var/www/PhotodiceServer AddHandler wsgi-script .py Options +ExecCGI SetHandler wsgi-script 命令deny,allow Allow from all 还是404
  • @BAYELK 查看最近的编辑!如果它对您有用,请不要忘记标记为正确。
  • 感谢您的编辑,但它没有解决任何问题:/ 仍然是 404
  • 尝试添加一个空的 url 处理程序:url(r"^$", "app.views.view")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
  • 2018-04-21
  • 2019-01-08
  • 2021-01-21
  • 2017-02-01
  • 2020-07-26
  • 2018-03-30
相关资源
最近更新 更多