Use Apache 2 + mod_wsgi for Python app
Question: I'm trying to use Apache 2 + mod_wsgi for my Python app. But get an error:

    
    
    Internal Server Error
    The server encountered an internal error or misconfiguration and was unable to complete your request.
    Please contact the server administrator, webmaster@test.local and inform them of the time the error occurred, and anything you might have done that may have caused the error.
    More information about this error may be available in the server error log.
    

My configurations: Ubuntu 12.04, Apache2, Python3.3, Django 1.6,
libapache2-mod-wsgi-py3.

My settings.

Folder structure:

    
    
    www
    └── test.local
        ├── test_site
        │   ├── manage.py
        │   └── test_site
        │       ├── __init__.py
        │       ├── settings.py
        │       ├── urls.py
        │       └── wsgi.py
        └── test_site.wsgi
    

/etc/hosts:

    
    
    127.0.0.1   localhost
    127.0.0.2   test.local
    127.0.1.1   ube-home
    
    # The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    

/etc/apache2/sites-available/test.local

    
    
    <VirtualHost 127.0.0.2:80>
        ServerAdmin webmaster@test.local
        ServerName test.local
            ServerAlias www.test.local
    
        WSGIScriptAlias / /home/ube/www/test.local/test_site.wsgi
    
        DocumentRoot /home/ube/www/test.local
        <Directory />
            Options FollowSymLinks
            AllowOverride All
        </Directory>
        <Directory /home/ube/www/test.local/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
        </Directory>
    
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
            AllowOverride All
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Order allow,deny
            Allow from all
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
    
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
    
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        Alias /doc/ "/usr/share/doc/"
        <Directory "/usr/share/doc/">
            Options Indexes MultiViews FollowSymLinks
            AllowOverride ALl
            Order deny,allow
            Deny from all
            Allow from 127.0.0.0/255.0.0.0 ::1/128
        </Directory>
    </VirtualHost>
    

/home/ube/www/test.local/test_site.wsgi

    
    
    import os
    import sys  
    sys.path.append('/home/ube/www/test.local/')
    os.environ['DJANGO_SETTINGS_MODULE'] = 'test_site.settings'
    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()
    

I haven't forgotten:

    
    
    sudo a2ensite test.local
    sudo service apache2 restart
    

Error log says:

    
    
    [Sun Mar 30 01:35:06 2014] [error] [client 127.0.0.1] Traceback (most recent call last):
    [Sun Mar 30 01:35:06 2014] [error] [client 127.0.0.1]   File "/home/ube/www/test.local/test_site.wsgi", line 5, in <module>
    [Sun Mar 30 01:35:06 2014] [error] [client 127.0.0.1]     import django.core.handlers.wsgi
    [Sun Mar 30 01:35:06 2014] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
    [Sun Mar 30 01:35:06 2014] [error] [client 127.0.0.1] mod_wsgi (pid=15638): Target WSGI script '/home/ube/www/test.local/test_site.wsgi' cannot be loaded as Python module.
    [Sun Mar 30 01:35:06 2014] [error] [client 127.0.0.1] mod_wsgi (pid=15638): Exception occurred processing WSGI script '/home/ube/www/test.local/test_site.wsgi'.
    [Sun Mar 30 01:35:06 2014] [error] [client 127.0.0.1] Traceback (most recent call last):
    [Sun Mar 30 01:35:06 2014] [error] [client 127.0.0.1]   File "/home/ube/www/test.local/test_site.wsgi", line 5, in <module>
    [Sun Mar 30 01:35:06 2014] [error] [client 127.0.0.1]     import django.core.handlers.wsgi
    [Sun Mar 30 01:35:06 2014] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
    


Answer: Solution in my case:

    
    
    import os
    import sys  
    sys.path.append('/home/ube/www/test.local/test_site/')
    sys.path.append('/home/ube/www/test.local/')
    sys.path.append('/home/ube/www/')
    sys.path.append('/usr/local/lib/python3.3/dist-packages')
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'test_site.settings'
    
    from django.core.handlers.wsgi import WSGIHandler
    application = WSGIHandler()
    

I added more sys.path =).

