【问题标题】:Apache2 and context path for virtual host with Django and AngularJS带有 Django 和 AngularJS 的虚拟主机的 Apache2 和上下文路径
【发布时间】:2016-12-01 18:04:54
【问题描述】:

我有以下可用的 Django 配置:

WSGIScriptAlias / /MyDjangoProjectFolder/MyDjangoProject/wsgi.py
WSGIPythonPath /MyDjangoProjectFolder

<Directory /MyDjangoProjectFolder/MyDjangoProject>
   <Files wsgi.py>
        Order deny,allow
       Require all granted
   </Files>
</Directory>

Alias /base_context_path/static/ /MyDjangoProjectFolder/static/

<Directory /MyDjangoProjectFolder/static>
    Require all granted
</Directory>

Django 响应&lt;ip&gt;/base_context_path/rest(用于由前端调用的其余 API)和&lt;ip&gt;/base_context_path/admin(用于管理,它使用 base_context_path/static)。 所以Django需要的一切都在&lt;ip&gt;/base_context_path/*

现在我需要在同一个 Apache2 上部署一个用 Angular 开发的网站,所以我想了解如何让它工作。我有这个网站的域名(mydomainname.org),但不是 django 应用程序的专用域名。当访问我的域名时,我希望我的网站会出现。

这是我对我的网站的尝试:

<VirtualHost *:80>
    ServerName mydomainname.org
    DocumentRoot /MyWebSiteFolder
    DirectoryIndex index.html
 <Directory "/MyWebSiteFolder">
# redirect rules for managing AngularJS
  </Directory>
</VirtualHost>

它不起作用 (403)。

对于我的 Django 应用程序,这是我创建的虚拟主机,它不能正常工作(403):

<VirtualHost *:80>
ServerName mydomainname
Alias /base_context_path/static/ /MyDjangoProjectFolder/static

WSGIScriptAlias / /MyDjangoProjectFolder/MyDjangoProject/wsgi.py

WSGIDaemonProcess MyDjangoProject python-path=/MyDjangoProjectFolder:/usr/local/lib/python2.7/site-packages
WSGIProcessGroup MyDjangoProject


<Directory /MyDjangoProjectFolder/static>
        Options -Indexes
    Require all granted
</Directory>

所以我有点卡住了。

【问题讨论】:

  • 我建议将 nginx 与 django/uwsgi 一起使用。如果你想试试看这个link
  • 这是您在 django 虚拟主机配置中的 ServerName 的拼写错误吗?我希望它是您的完全限定域名...(我假设您也关闭了 virtualhost 标签。)

标签: python django apache virtualhost


【解决方案1】:

对于目录,你需要添加以下指令:

  Require all granted 

或访问目录的等效项。 Apache 可能无权查看该目录或被配置为有权访问它。您还需要确保 Apache 具有读取、写入和执行权限。

同时检查 apache 日志以获取有关正在发生的错误 (403) 的更多信息。

我也建议像 Aki003 一样使用 nginx,因为它更容易配置和使用 uwsgi。

【讨论】:

    【解决方案2】:

    要让您的 Django 应用程序在 Apache 下运行,您应该安装 mod_wsgi(建议使用 CMMI 方法,以确保您为用于创建虚拟环境的相同 python 版本编译它)。

    如果你没有 httpd.conf 文件,编辑 /etc/apache2/apache2.conf 并写下这一行:

    Include /etc/apache2/httpd.conf

    然后继续创建httpd.conf 并在其中添加以下内容:

    LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
    
    WSGIPythonHome /some/path/where/is/an/empty/venv
    
    Alias /static/ /MyDjangoProjectFolder/MyDjangoProject/static/
    
    <Directory /MyDjangoProjectFolder/MyDjangoProject/static>
        Require all granted
    </Directory>
    
    WSGIScriptAlias / /MyDjangoProjectFolder/MyDjangoProject/wsgi.py process-group=MyDjangoProject
    
    <Directory /MyDjangoProjectFolder/MyDjangoProject>
    <Files wsgi.py>
        Require all granted
    </Files>
    </Directory>
    
    

    (旁注):您可能还希望在此处包含安全标头:

    Header set X-Frame-Options SAMEORIGIN
    Header set X-XSS-Protection 1;mode=block
    Header set X-Content-Type-Options nosniff
    Header set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
    

    您的 VirtualHost 文件应如下所示:

    <VirtualHost *>
        WSGIDaemonProcess MyDjangoProject 
        WSGIProcessGroup MyDjangoProject
    
        WSGIScriptAlias / /MyDjangoProjectFolder/MyDjangoProject/wsgi.py 
    
        <Directory /MyDjangoProjectFolder/MyDjangoProject>
            Require all granted
        </Directory>
    <VirtualHost>
    
    

    根据mod_wsgi 文档,这是为多个应用程序使用守护模式的配置(您可能想添加或测试其他应用程序,所以这是一个很好的准备)。

    /some/path/where/is/an/empty/venv 是您要创建空虚拟环境的路径(使用virtualenv !! 这样您就可以访问activate_this.py 脚本!!) 确保在 django 项目中创建并激活另一个虚拟环境,您将在其中安装所有依赖项。

    您可能希望这样做,因为当您从 WSGI 文件中激活虚拟环境时,只会使用来自 python 虚拟环境的 site-packages 目录。 如果您有任何机会忽略了从requirements.txt 安装软件包,但这些软件包安装在主 python 安装中,它们将从那里使用,如果版本错误或具有不同的依赖项,您可能会遇到一些错误。

    下一步是修改wsgi.py,首先添加这些行(在任何导入之前):

    python_home='/path/to/the/MyDjangoProjectFolder/venv'
    activate_this=python_home+'/bin/activate_this.py'
    with open(activate_this) as file_:
            exec(file_.read(), dict(__file__=activate_this))
    

    以便在 mod_wsgi 解析这个文件时,激活所有依赖项所在的虚拟环境,以便正常运行。

    另外,在同一个文件(wsgi.py)中,将 django 项目的路径添加到 sys 路径:

    import sys
    sys.path.append('/path/to/the/MyDjangoProjectFolder')
    

    另外,别忘了设置 DEBUG = False 和编辑 ALLOWED_HOSTS = ['.mydomainname.org']

    【讨论】:

      【解决方案3】:

      这是我每次用于 Apache2 的设置模板。

          Apache2:
          sudo apt-get install apache2 libapache2-mod-wsgi-py3
          cd /etc/apache2/sites-available/
      
          sudo nano 000-default.conf
      
          <VirtualHost *:80>
              ServerAdmin webmaster@localhost
              DocumentRoot /var/www/html
              
      
              ErrorLog /home/user/site/logs/error.log
              CustomLog /home/user/site/access.log combined
      
              ServerName website.com
                  ServerAlias www.website.com
      
              Alias /static /home/user/django_project/static
              <Directory /home/user/django_project/static>
                  require all granted
              </Directory>
      
              Alias /media /home/user/django_project/media
              <Directory /home/user/django_project/media>
                  require all granted
              </Directory>
      
              Alias /images /home/user/django_project/images
              <Directory /home/user/django_project/images>
                  require all granted
              </Directory>
      
              <Directory /home/user/django_project/folder_containing_settings>
                  <files wsgi.py>
                      require all granted
                  </files>
              </Directory>
              
              WSGIDaemonProcess django_project python-path=/home/user/django_project python-home=/home/user/django_project/env
              WSGIProcessGroup django_project
              WSGIScriptAlias / /home/user/django_project/folder_containing_settings/wsgi.py
      
      
          </VirtualHost><span style="font-weight: 400;">
      
      
              
          sudo apachectl configtest
      

      【讨论】:

        【解决方案4】:

        您必须创建两个配置文件,一个用于 django 应用程序,另一个用于前端应用程序

        首先你可以通过域和子域来制作 前端 example.com 后端 api.example.com

        或者你改变 WSGIScriptAlias / to WSGIScriptAlias /base_context_path

        默认情况下会前置

        【讨论】:

          猜你喜欢
          • 2014-12-24
          • 2015-03-24
          • 1970-01-01
          • 2012-09-05
          • 2015-10-06
          • 2013-10-16
          • 2012-04-30
          • 2023-04-05
          • 2012-03-14
          相关资源
          最近更新 更多