Django Fett
Django Fett

Reputation: 87

Apache mod_wsgi throwing error "403 Forbidden" when deploying a Django project

I have been trying to deploy a Django site using mod_wsgi on a CentOS server recently, but so far when I try to access the django site through my laptop, the web page has only been displaying error: 403 Forbidden You don't have permission to access / on this server.

In addition to reading all the obvious documentation, I have looked at these previous questions:

Environment:

I am running the following version of apache:

# apachectl -V  
Server version: Apache/2.2.15 (Unix)  
Server built:   Apr  3 2014 23:56:16  
Server's Module Magic Number: 20051115:25  
Server loaded:  APR 1.3.9, APR-Util 1.3.9  
Compiled using: APR 1.3.9, APR-Util 1.3.9  
Architecture:   64-bit  
Server MPM:     Prefork  
threaded:     no  
forked:     yes (variable process count)

I installed mod_wsgi using Yum and have confirmed it is installed on the server:

# httpd -M | grep wsgi 
wsgi_module (shared)
Syntax OK

My httpd.conf wsgi config snippet is as follows:

#
# Add WSGI configuration
# 

WSGIScriptAlias / /usr/local/django/basic/basic/apache/wsgi.py
WSGIPythonPath /usr/local/django/basic/
WSGIDaemonProcess ###.###.###.###
WSGIProcessGroup ###.###.###.###

<Directory /usr/local/django/basic/basic/apache>
    <Files wsgi.py>
        Options FollowSymLinks
        Order deny,allow
        Allow from all
    </Files>
</Directory>

Finally my wsgi.py script is:

"""
WSGI config for basic project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/
"""

import os
import sys

path = "/usr/local/django/basic/basic/apache"
if path not in sys.path:
     sys.path.append(path)

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

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

Output from error log:

[Fri Oct 24 14:10:43 2014] [error] [client (redacted)] Symbolic link not allowed or link target not accessible: /usr/local/django/basic  
[Fri Oct 24 14:11:25 2014] [error] [client (redacted)] Symbolic link not allowed or link target not accessible: /usr/local/django/basic  
[Fri Oct 24 14:14:02 2014] [error] [client (redacted)] Symbolic link not allowed or link target not accessible: /usr/local/django/basic  

Notes:

Upvotes: 1

Views: 4015

Answers (2)

Django Fett
Django Fett

Reputation: 87

Sorry, that I posted this a bit late. This was the final fix to my apache config that ultimately worked.

WSGIScriptAlias /basic /var/www/django/basic/basic/wsgi.py
WSGIPythonPath /var/www/django/basic/

<Directory /var/www/django/basic/basic>
    Options FollowSymLinks
    <Files wsgi.py>
        Order allow,deny
        Allow from all
    </Files>
</Directory>

Alias /static /var/www/django/basic/basic/static

And this is the final version of my wsgi.py file in python. The key line of code here was the PYTHON_EGG_CACHE. That variable was by default set to a directory that did not exist. I set it to /tmp/.python-eggs Make sure that .python-eggs has correct permissions for the apache user to read/write to it wherever you may place this file.

"""
WSGI config for basic project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/
"""

import os
import sys

path = "/usr/local/django/basic/basic/apache"
    if path not in sys.path:
        sys.path.append(path)

os.environ['PYTHON_EGG_CACHE'] = '/tmp/.python-eggs'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "basic.settings")

#print os.getenv("DJANGO_SETTINGS_MODULE")
#print os.getenv("PYTHON_EGG_CACHE")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Side note:

  • A friendly reminder to make sure that every file in your django application is readable, (and writeable if needed) by the apache user. Git once overwrote a file to an old permission I had set up once and it took me a little time to figure out the permissions had changed without realizing it.

Upvotes: 1

andy
andy

Reputation: 21

May be you forget set DocumentRoot. You should make the DocumentRoot can be read and wrote by apache users. just do like this:

sudo chown -R www_default:www_default /path/to/you/DocumentRoot

and you can also do like this :

sudo chmod -R 755 /path/to/your/DocumentRoot

Upvotes: 0

Related Questions