Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django with WSGI behind Apache VirtualHost gives 404

I'm trying to run a Django application on an Apache server, in a virtual host using WSGI.

I followed the Django docs exactly, but it there still seems to be some issue here.

I have everything configured properly as far as I can tell.

wsgi.py:

import os
import sys

path='/var/www/docs/'
sys.path.append(path)

os.environ["DJANGO_SETTINGS_MODULE"] = "DocsEngine.settings"

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

virtualhost file:

Listen 8000

WSGIPythonPath /var/www/docs

<VirtualHost *:8000>
    WSGIScriptAlias / /var/www/docs/DocsEngine/wsgi.py

    ServerName myurl.com
    Alias /resources/ /var/www/docs/resources/

    <Directory /var/www/docs/DocsEngine>
        <Files wsgi.py>
            Require all granted
        </Files> 
    </Directory>

    CustomLog /var/log/apache2/wsgi-access.log combined  
    ErrorLog  /var/log/apache2/wsgi-error.log

</VirtualHost>

When navigating to myurl.com:8000/ it comes up with a 404 page.

  • My firewall has port 8000 open
  • This vhost is enabled in apache.
  • Apache has mod_wsgi enabled
  • The django app works when running it locally with the dev server.
  • Both my local machine and server are running python 2.7.6 and django 1.7.3

I am able to access stuff located in my /resources directory, so I know apache can recognize some part of this.

I've been banging my head on this for hours, any help would be much appreciated.

like image 289
Patrick Hennessy Avatar asked Mar 03 '15 02:03

Patrick Hennessy


2 Answers

After about 14 hours of Google-fu and a lot of failures, I finally found the solution. Apparently they made undocumented breaking changes in both Django 1.7 and Apache 2.4.

First, I needed to disable mod_python from Apache. Apparently it causes silent problems when WSGI is also enabled. As soon as I disabled it, my logs started blowing up, thank god. Here is the link I found where this solution was described to me: http://marc-abramowitz.com/archives/2012/09/28/some-tips-for-setting-up-apache-and-mod_wsgi/

Given the errors found from disabling mod_python, I discovered that Django's WSGI.py file needs to be initialized in this manner, differing to what is described in the docs:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

https://www.pythonanywhere.com/forums/topic/1629/#id_post_11030

Hope this helps poor souls who encounter this issue in the future.

like image 91
Patrick Hennessy Avatar answered Nov 01 '22 15:11

Patrick Hennessy


please have a look these steps : create new file :

sudo nano /etc/apache2/sites-available/domain_name

write these lines into your new file.

<VirtualHost *:8000>
        ServerAdmin [email protected]
        ServerName myurl.com
        ServerAlias myurl.com
        WSGIScriptAlias / /var/www/docs/DocsEngine/wsgi.py

        Alias /static/ /home/cis/DjangoLive/cismailer/static/
        <Location "/static/">
            Options -Indexes
        </Location>
</VirtualHost>

Then run:

sudo a2ensite domain_name

Then Open file :

sudo nano /etc/apache2/ports.conf

change port in this file :

NameVirtualHost *:8000
Listen 8000

Then sudo service apache2 restart

Hope this will work for you.

if you using virtualenv your wsgi script file look like this:

import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with

site.addsitedir('/path/of/your/env/lib/python2.7/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('/var/www/docs/DocsEngine')
sys.path.append('/var/www/docs/DocsEngine/DocsEngine')


# Activate your virtual env
activate_env=os.path.expanduser("/path/of/your/env/bin/activate_this.py")

execfile(activate_env, dict(__file__=activate_env))

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


import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Here is some like you can use for more info: http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/

https://www.digitalocean.com/community/tutorials/how-to-run-django-with-mod_wsgi-and-apache-with-a-virtualenv-python-environment-on-a-debian-vps

like image 2
Yogesh dwivedi Geitpl Avatar answered Nov 01 '22 15:11

Yogesh dwivedi Geitpl