首页 文章

使用apache2部署django

提问于
浏览
1

(我在我的脑海里)我试图在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',
)

以前当我进入网络服务器(覆盆子pi)的ip时,我被自动路由到存储在var / www /中的index.html .

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

(如果我设置debug = False我得到错误请求(400)而不是)

1 回答

  • 1

    我认为你在Apache配置中缺少一些东西:

    • AddHandler wsgi-script .py

    • In <Directory xxx> 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>
    

相关问题