使用教程设置Django和Apache失败

1 投票
1 回答
1406 浏览
提问于 2025-04-18 06:39

我正在尝试使用django网站上的教程来设置apache和一个现有的django项目,教程链接在这里。我的操作系统是Ubuntu,所有东西都已经安装好了(django、apache2、libapache2-mod-wsgi)。

这是我的配置文件:

WSGIPythonPath /home/avlahop/development/django/rhombus2/rhombus/

<VirtualHost *:80>
    ServerName myrhobmus.com
    ServerAlias www.myrhombus.com
    WSGIScriptAlias / /home/avlahop/development/django/rhombus2/rhombus/rhombus/wsgi.py 
</VirtualHost>

在我创建了配置文件后,我运行了a2ensite命令来启用这个网站。

把WSGIPythonPath放在VirtualHost里面让我在apache配置测试时出错。按照示例中的指示,把Files放在目录里面也出现了同样的错误。

当我访问www.myrhombus.com时,谷歌浏览器显示找不到指定地址的消息。

我哪里做错了?网上的每个教程都在用旧的file.wsgi文件,而现在django会为你创建一个.py扩展名的python文件。我该如何用apache来运行我的django项目?如果我想在自己的服务器上投入生产环境,django代码和模板文件应该放在哪里呢?

编辑:我只看到“Index of /”的页面。我需要对我的网站位置做些什么来处理权限问题吗?你能给我一个有效的django网站apache配置文件的例子吗?

1 个回答

0

我使用的是Django 1.8,并且成功在Apache服务器上部署了我的项目。我遵循了一些基本的概念,比如你所提到的,并且提前启用了Apache的这个模块。

启用模块

你可以通过这个问题了解我的项目结构。参考这个问题来理解项目结构

--------------这是我的虚拟主机文件----------------------------------

WSGIPythonPath /home/umayanga/Desktop/view_site/serialKey_gen_site:/home/umayanga/Desktop/view_site/serialKey_gen_site/myvenv/lib/python3.4/site$

<VirtualHost *:80>
    ServerAdmin admin@key.com
    ServerName key.com
    ServerAlias www.key.com

    Alias /templates/ /home/umayanga/Desktop/view_site/serialKey_gen_site/templates/
    Alias /static/ /home/umayanga/Desktop/view_site/serialKey_gen_site/static/


    <Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/static">
           Require all granted
    </Directory>

    <Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/templates">
           Require all granted
    </Directory>

    WSGIScriptAlias / /home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py

    <Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite">
        Options Indexes FollowSymLinks
        AllowOverride all
        Require all granted
        <Files wsgi.py>
               Require all granted
        </Files>
    </Directory>

</VirtualHost>

-----------------wsgi.py---------------------------------------

"""
WSGI config for mysite project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application



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

application = get_wsgi_application()

我觉得这些对你会有帮助。

撰写回答