可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm using Ubuntu 10.04.
I create a django project under /home/wong2/Code/python/django2/
named atest
and create a wsgi file setting.wsgi
in the same directory
Here is the content of setting.wsgi
:
import os
import sys
path = '/home/wong2/Code/python/django2'
if path not in sys.path:
sys.path.append(path)
os.environ["DJANGO_SETTINGS_MODULE"] = "atest.settings"
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
and here is what I add to my my httpd.conf:
<VirtualHost *:80>
ServerName localhost
WSGIScriptAlias / /home/wong2/Code/python/django2/setting.wsgi
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
<Directory "/home/wong2/Code/python/django2/atest">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
The problem is, when I visit http://localhost, it says
Forbidden
You don't have permission to access /
on this server.
Thanks a lot.
回答1:
The second directory block doesn't match where you have your WSGI script file installed. It is very bad practice though to stick the WSGI script file in a location where source code or other sensitive files exist, ie., same directory or sub directory. Instead you should stick it in a sub directory of its own. Thus:
WSGIScriptAlias / /home/wong2/Code/python/django2/atest/apache/setting.wsgi
<Directory "/home/wong2/Code/python/django2/atest/apache">
Order allow,deny
Allow from all
</Directory>
So, create 'apache' subdirectory under 'atest'. Move 'setting.wsgi' into that 'apache' subdirectory and change config to above.
Your problem also may be caused by restrictive permisions on your home directory as Apache cannot see inside.
Go watch:
http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations
as it explains these permissions problems as well as issues like where to stick your code and the WSGI script file.
Also read:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
回答2:
With Django 1.5+ you should use the suggested way described in the documentation:
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/modwsgi/
回答3:
I had the same problem due to restricted file permissions.
By default your home folder user has the settings:
ls -l /home/
drwx------ 36 user user 12288 Nov 28 20:56 user
meaning that no one else but yourself is able to even browse that folder.
Adding execute option to the folder fixed my problem
chmod o+x /home/user/
ls -l /home/
drwx-----x 36 user user 12288 Nov 28 20:56 user
回答4:
try http://localhost/index.html does that show the apache page?
If you wsgi is configured on root correctly it wont.
You get that error message because you might be asking the webserver for the directory listing of www folder
edit:
My projects are always in
/var/pyproj//pysrc/
and server files i put in
/var/py_proj//server/
Here is my go to wsgi script
import os, sys
import logging
logger =logging.getLogger("")
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(levelname)-8s %(messages)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
sys.stdout = sys.stderr
from os.path import abspath, dirname, join
import site
PROJECT_ROOT = abspath(join(dirname(__file__), "../pysrc"))
SETTINGS_LOC = abspath(join(PROJECT_ROOT, ''))
site.addsitedir(SETTINGS_LOC)
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"
from django.core.management import setup_environ
import settings
setup_environ(settings)
sys.path.insert(0, join(PROJECT_ROOT, "apps"))
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
and i use a vhost.conf
WSGIDaemonProcess pinax_demo user=www-data group=www-data threads=10 python-path=/var/.virtualenvs/pinax_demo/lib/python2.6/site-packages
<Directory /var/py_proj/pinax_demo/server/>
Order deny,allow
Allow from all
</Directory>
Alias /pinax_demo /var/py_proj/pinax_demo/server/pinax_demo.wsgi
<Location /pinax_demo/>
#WSGIProcessGroup must match the WSGIDaemonProcess
WSGIProcessGroup pinax_demo
SetHandler wsgi-script
Options +ExecCGI
</Location>
which i include in the sites-available folder in a file that looks like this
<VirtualHost *:8080>
DocumentRoot /var/www/
<Directory /var/www/>
Order deny,allow
Allow from all
Options -Indexes FollowSymLinks
DirectoryIndex index.php
AllowOverride None
</Directory>
ServerAdmin oto@example.com
LogLevel error
ErrorLog /var/srv_logs/apache_error.log
CustomLog /var/srv_logs/apache_access.log combined
#insert
Include /var/py_proj/pinax_demo/server/vhost.conf
Include /var/py_proj/<other>/server/vhost.conf
Include /var/py_proj/<other>/server/vhost.conf
</VirtualHost>
httpd.conf is empty. Apache is also terrible at sending static files so i have my apache setup on port 8080 and nginx setup on port 80 which forwards to apache. You should be able to just change the port on my sites-avaiable file to run it without nginx but i would not go live that way
回答5:
You may get a surprise where your WSGI has its home directory! On my admittedly naive wsgi_mod installation, it is / - I was so surprised I had to write this script to make sure:
import os
def application(environ, start_response):
status = '200 OK'; e=os.listdir(os.getcwd())
output = '''WSGI %s<hr>'''%(e)
response_headers = [('Content-type', 'text/html'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
That means imported applications will need a full path to be found, e.g.:
sys.path.append('/home/foo/www/Forms')
from DataCommon import page
This is the recommended (by Google) approach to this:
import sys; path='/home/foo/www/Forms';
if path not in sys.path: sys.path.append(path)