在 Apache2 服务器上使用 WSGI 部署 Django 应用程序时出错

Error on deploying Django app using WSGI on Apache2 server

我正在尝试使用 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 时看到 "welcome to django" 页面。它给出了一个“找不到页面(404)

访问 IP/django.
时未获取默认的 django 页面

在访问时获取此页面 IP/django
不过,IP/django/admin 工作正常。

您通过将该路径作为 WSGIScriptAlias 专门要求 Apache 在 /django 为您的 Django 应用程序提供服务。如果你想要它在 /,这是正常的做法,你应该在那里使用 /

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

<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。