【问题标题】:Error on deploying Django app using WSGI on Apache2 server在 Apache2 服务器上使用 WSGI 部署 Django 应用程序时出错
【发布时间】:2016-03-30 21:14:09
【问题描述】:

我正在尝试使用 Ubuntu 在 Apache2 服务器上设置 Django 应用程序。 我使用了以下教程来做要领。
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04

我也使用了以下解决方案,但它对我不起作用。
Django (wsgi) and Wordpress coexisting in Apache virtualhost

其他参考:
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/modwsgi/

我的 apache2 配置文件如下。

<VirtualHost *:80>

    Alias /static /home/ubuntu/test_pfi_app/static
    #Alias /media /home/ubuntu/test_pfi_app/media

    #DocumentRoot /var/www/html

    <Directory /home/ubuntu/test_pfi_app/static>
        Require all granted
    </Directory>

    WSGIDaemonProcess myproject python-path=/home/ubuntu/test_pfi_app:/usr/local/lib/python2.7/site-packages
    WSGIProcessGroup myproject
    WSGIScriptAlias /django /home/ubuntu/test_pfi_app/myproject/wsgi.py process-group=myproject
    #WSGIScriptAliasMatch /django /home/ubuntu/test_pfi_app/myproject/wsgi.py

    <Directory /home/ubuntu/test_pfi_app/myproject>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
ServerName app1.example.co.in
DocumentRoot /var/www/html/ocdemo
</VirtualHost>

<VirtualHost *:80>
ServerName app2.example.co.in
DocumentRoot /var/www/html/echosuat
</VirtualHost>

<VirtualHost *:80>
ServerName task.example.co.in
DocumentRoot /var/www/html/tasks
</VirtualHost>

我可以使用 IP/django 访问 django 应用程序,但访问服务器的根目录会重定向到 /var/www url,如下图所示。

所有其他应用程序都可以从IP/html/app_name 访问,而它们应该可以从IP/app_name 访问

我无法在配置文件中进行正确的配置。

另外,我无法在访问IP/django 时看到“欢迎使用 django”页面。而是给出一个“Page not found (404)

在访问 IP/django 时未获得默认的 django 页面。

在访问 IP/django 时获取此页面
虽然 IP/django/admin 工作正常。

【问题讨论】:

  • 您有任何可以共享的 Apache 日志文件吗?
  • 我会在明天之前添加它们。我查看了日志,发现它们没有多大用处。

标签: python django apache2 django-wsgi django-deployment


【解决方案1】:

您已明确要求 Apache 通过将该路径设置为 WSGIScriptAlias 来在 /django 上为您的 Django 应用程序提供服务。如果您想在/ 获得它,这是正常的做法,您应该在那里使用/

【讨论】:

  • 是的。这正是我想做的。问题是 php 应用程序无法正确访问。我希望可以使用/app_name 访问它们,但现在可以使用/html/app_name 访问它们。
【解决方案2】:

在阅读了有关配置文档的更多信息后,我终于找到了解决方案。

<VirtualHost *:80>

    Alias /static /home/ubuntu/app/static
    Alias /media /home/ubuntu/app/media

    DocumentRoot /var/www/html

    <Directory /home/ubuntu/app/static>
        Require all granted
    </Directory>

    WSGIDaemonProcess app python-path=/home/ubuntu/app:/usr/local/lib/python2.7/site-packages
    WSGIProcessGroup app
    WSGIScriptAlias /django /home/ubuntu/app/app/wsgi.py process-group=app

    <Directory /home/ubuntu/app/app>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
ServerName ocdemo.example.co.in
DocumentRoot /var/www/html/ocdemo
</VirtualHost>

<VirtualHost *:80>
ServerName echosuat.example.co.in
DocumentRoot /var/www/html/echosuat
</VirtualHost>

<VirtualHost *:80>
ServerName task.example.co.in
DocumentRoot /var/www/html/tasks
</VirtualHost>

我需要更改的只是在 django 应用程序的配置中添加 DocumentRoot。

【讨论】:

    猜你喜欢
    • 2015-11-21
    • 2021-01-21
    • 2022-12-30
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2013-07-15
    • 2019-08-21
    • 2020-08-02
    相关资源
    最近更新 更多