Re: [modwsgi] Target WSGI script not found or unable to stat: /var/www/wsgi-scripts/myapp.wsgi

1,822 views
Skip to first unread message

Graham Dumpleton

unread,
Aug 28, 2012, 9:24:19 PM8/28/12
to mod...@googlegroups.com
Don't you mean:

WSGIScriptAlias /myapp /var/www/wsgi-scripts/myapp/wsgi.py

You have to modify the target file based on where it is. Can't copy
documentation blindly.

Graham

On 29 August 2012 10:59, hhummel <hhhu...@gmail.com> wrote:
> I'm having trouble with getting mod_wsgi going on FC15 linux. I'm trying to run the simple "Hello World" demo.
>
> Here's httpd.conf:
>
> LoadModule wsgi_module modules/mod_wsgi.so
>
> WSGIScriptAlias /myapp /var/www/wsgi-scripts/myapp.wsgi
>
> <Directory /var/www/wsgi-scripts>
> Order allow,deny
> Allow from all
> </Directory>
>
> The app is in /var/www/wsgi-scripts/myapp/wsgi.py, and is copied directly from the documentation:
>
> def application(environ, start_response):
> status = '200 OK'
> output = 'Hello World!'
>
> response_headers = [('Content-type', 'text/plain'),
> ('Content-Length', str(len(output)))]
> start_response(status, response_headers)
>
> return [output]
>
> When I restart the server and use the URL 127.0.0.1/mapp is see this error log:
>
> [Tue Aug 28 20:44:26 2012] [notice] Apache/2.2.22 (Unix) DAV/2 mod_python/3.3.1 Python/2.7.1 mod_wsgi/3.4 PHP/5.3.13 mod_ssl/2.2.22 OpenSSL/1.0.0j-fips mod_perl/2.0.4 Perl/v5.12.4 configured -- resuming normal operations
> [Tue Aug 28 20:44:26 2012] [info] Server built: Feb 13 2012 14:50:23
> [Tue Aug 28 20:44:26 2012] [info] mod_wsgi (pid=556): Attach interpreter ''.
> [Tue Aug 28 20:44:26 2012] [info] mod_wsgi (pid=555): Attach interpreter ''.
> [Tue Aug 28 20:44:32 2012] [error] [client 127.0.0.1] Target WSGI script not found or unable to stat: /var/www/wsgi-scripts/myapp.wsgi
>
> Permissions on the file:
> [harry@mars mysite]$ ls -l
> total 4
> -rw-r--r-- 1 harry harry 1428 Aug 28 17:21 wsgi.py
>
> Permissions on the dir:
> [harry@mars apache]$ ls -l
> total 4
> drwxrwxr-x 2 harry harry 4096 Aug 28 18:25 mysite
>
> I'm kind of stuck what else to try. Can you help?
>
> Many thanks,
>
> Harry
>
> --
> You received this message because you are subscribed to the Google Groups "modwsgi" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/modwsgi/-/G98EjEg88ZUJ.
> To post to this group, send email to mod...@googlegroups.com.
> To unsubscribe from this group, send email to modwsgi+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/modwsgi?hl=en.
>

hhummel

unread,
Aug 28, 2012, 11:05:49 PM8/28/12
to mod...@googlegroups.com
Noted. 

That helped a lot, thanks!

I was confused by django, where you refer to mysite/settings.py as mysite.settings.  I assumed I did the same thing here.

Harry

Graham Dumpleton

unread,
Aug 28, 2012, 11:09:12 PM8/28/12
to mod...@googlegroups.com
It is only a sample. Django 1.4 also changed things and now produces a
wsgi.py file you which can be confusing. The Django documentation has
an updated example.

Graham
> https://groups.google.com/d/msg/modwsgi/-/gQ7PnrBU564J.

hhummel

unread,
Aug 29, 2012, 10:46:42 AM8/29/12
to mod...@googlegroups.com
Thanks again for your help and in responding so quickly.  The py-con movie helped a lot too.

http://blip.tv/pycon-australia/getting-started-with-apache-mod_wsgi-3859481

hhummel

unread,
Aug 30, 2012, 3:53:46 PM8/30/12
to mod...@googlegroups.com
Things went much better fixing the name of the wsgi file, but I've struggling with the static files.  I don't have a lot of users and I'm not that worried about performance, but I'm interested in having the static content password projected.  Therefore I'd like password protected under the django views rather than server the static content through apache.

Here's the httpd.conf:

<Directory /var/www/django/static>
    Order deny,allow
    Allow from all
</Directory>

WSGIScriptAlias / /var/www/django/mysite/apache/django.wsgi

<Directory /var/www/django/mysite/apache/mysite>

    Order allow,deny
    Allow from all
</Directory>

#Alias /static/ /var/www/django/static/

If I remove the comment # from the Alias command, everything works, but my static content is exposed on the public web, too.  With the Alias command commented out, I don't see the static content anymore, although the template appears correctly.

I used the collectstatic command, (although it' not really relevant as I don't have lots of apps), so the static content is in /var/www/django/static.

Here is the settings.py pertaining to STATIC:

STATIC_ROOT = '/var/www/django/static/'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

I added the instrumentation to the settings.py recommended in http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html

I see verbose output in the error log, so I'll try to abstract what might be useful.  /var/forecast_charts is the URL

[Thu Aug 30 15:22:12 2012] [notice] Apache/2.2.22 (Unix) DAV/2 mod_python/3.3.1 Python/2.7.1 mod_wsgi/3.4 PHP/5.3.13 mod_ssl/2.2.22 OpenSSL/1.0.0j-fips mod_perl/2.0.4 Perl/v5.12.4 configured -- resuming normal operations
[Thu Aug 30 15:22:12 2012] [info] Server built: Feb 13 2012 14:50:23
[Thu Aug 30 15:22:12 2012] [info] mod_wsgi (pid=14827): Attach interpreter ''.
[Thu Aug 30 15:22:12 2012] [info] mod_wsgi (pid=14829): Attach interpreter ''.
[Thu Aug 30 15:22:12 2012] [info] mod_wsgi (pid=14828): Attach interpreter ''.
[Thu Aug 30 15:22:16 2012] [info] mod_wsgi (pid=14822): Create interpreter '127.0.0.1|'.
[Thu Aug 30 15:22:16 2012] [info] [client 127.0.0.1] mod_wsgi (pid=14822, process='', application='127.0.0.1|'): Loading WSGI script '/var/www/django/mysite/apache/django.wsgi'., referer: http://127.0.0.1/var/forecast_charts
[Thu Aug 30 15:22:17 2012] [error] __name__ = mysite.settings
[Thu Aug 30 15:22:17 2012] [error] __file__ = /var/www/django/mysite/apache/../mysite/settings.pyc
[Thu Aug 30 15:22:17 2012] [error] os.getpid() = 14822
[Thu Aug 30 15:22:17 2012] [error] os.getcwd() = /
[Thu Aug 30 15:22:17 2012] [error] os.curdir = .
[Thu Aug 30 15:22:17 2012] [error] sys.path = ['/var/www/django/mysite/apache/..', '/usr/lib/python2.7/site-packages/rpy2-2.2.6dev_20120811-py2.7-linux-x86_64.egg', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/PIL', '/usr/lib64/python2.7/site-packages/gst-0.10', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info', '/var/www/django/mysite', '/var/www/django/static']

...

[Thu Aug 30 15:22:17 2012] [error] sys.modules.has_key('mysite') = True
[Thu Aug 30 15:22:17 2012] [error] sys.modules['mysite'].__name__ = mysite
[Thu Aug 30 15:22:17 2012] [error] sys.modules['mysite'].__file__ = /var/www/django/mysite/apache/../mysite/__init__.pyc
[Thu Aug 30 15:22:17 2012] [error] os.environ['DJANGO_SETTINGS_MODULE'] = mysite.settings
[Thu Aug 30 15:22:17 2012] [info] mod_wsgi (pid=14824): Create interpreter '127.0.0.1|'.

Here's django.wsgi.  I used the version from django 1.4 but used the apache parallel directory as per your recommendations:
import os
import sys

#Takes advantage of setting up parallel file structure for mysite/apache/wsgi.py and mysite/mysite/settings.py
root = os.path.join(os.path.dirname(__file__), '..')
sys.path.insert(0, root)

#Seems to need the absolute path as well
sys.path.append('/var/www/django/mysite')
sys.path.append('/var/www/django/static')

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

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()


Any advice on why the static content isn't serving through mod_wsgi?

Thanks again for your help, this is great software!


Harry


On Tuesday, August 28, 2012 9:24:22 PM UTC-4, Graham Dumpleton wrote:

Graham Dumpleton

unread,
Aug 31, 2012, 1:38:06 AM8/31/12
to mod...@googlegroups.com
You are probably going to have to ask on Django users list how to
protect static files served by Django under its own security
mechanisms. Is not usually how its down. Often you have to have static
assets downloadable to even see the log in page, so you can't protect
everything.

What stuff there is about using Django password database for
protecting stuff under Apache using Basic auth is detailed in:

http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms

Graham
> https://groups.google.com/d/msg/modwsgi/-/2Zl25hjvkeQJ.

hhummel

unread,
Aug 31, 2012, 8:05:10 AM8/31/12
to mod...@googlegroups.com
Okay, I'll give that a try.  I took a crack at the mod_authn_alias approach you outlined but hit a snag.  Of the several approaches you discuss in the Access Control document, is there one that you recommend?

Thanks again,


Harry

On Tuesday, August 28, 2012 9:24:22 PM UTC-4, Graham Dumpleton wrote:

Graham Dumpleton

unread,
Aug 31, 2012, 8:08:26 AM8/31/12
to mod...@googlegroups.com
They aren't choices. They are for different things.

The three phases are:

* access
* authentication
* authorization

To perhaps try and understand the distinction read:

http://httpd.apache.org/docs/current/howto/access.html
http://httpd.apache.org/docs/current/howto/auth.html

That said, it is hard for me to recommend anything as only you
understand your application and what you are trying to do. Anyone else
can only assume things based on what information you decide to make
available.

Graham
> https://groups.google.com/d/msg/modwsgi/-/DnUuprqz8PwJ.

hhummel

unread,
Aug 31, 2012, 9:33:54 AM8/31/12
to mod...@googlegroups.com
Thanks Graham,

That helped.  What I wanted to do is to have apache server the static content only to signed in users.  The static content is in /var/www/django/static,  so I did this in httpd.conf

#Set up django and mod_wsgi

#Set up access control for the static content.  See code.google.com/p/mdowsgi/wiki/AccessControlMechanisms by Graham Dumbleton
<AuthnProviderAlias wsgi django>
WSGIAuthUserScript /var/www/django/mysite/apache/auth.wsgi application-group=django
</AuthnProviderAlias>

#Set up the WSGI script alias file
WSGIScriptAlias / /var/www/django/mysite/apache/django.wsgi

#Allow access to wsgi directory for apache
<Directory /var/www/django/mysite/apache/mysite>

    Order allow,deny
    Allow from all
</Directory>

#Use apache to serve content from the static files
Alias /static/ /var/www/django/static/

#Set up access control for the static directory, and get the user authentication from wsgi

<Directory /var/www/django/static>
    Order deny,allow
    Allow from all

    WSGIApplicationGroup django

    AuthType Basic
    AuthName "Django Site"
    AuthBasicProvider django
    Require valid-user
</Directory>

Works!  The user has to renter his password the first time he hits static content, but then the browser saves it so that's fine.  Thanks again for the coaching!


Harry
On Tuesday, August 28, 2012 9:24:22 PM UTC-4, Graham Dumpleton wrote:
Reply all
Reply to author
Forward
0 new messages