patrickn
patrickn

Reputation: 2561

Django + Apache + mod_wsgi permission denied

I finished the tutorial on Django's site about using mod_wsgi (here), and having substituted my paths as appropriate, results in a big fat "Permission denied." when I try to access /. Here is the stuff I added to httpd.conf (mod_wsgi is enabled earlier in the conf file):

# Django configuration

WSGIScriptAlias / /usr/local/django/billing/apache/django.wsgi

<Directory /usr/local/django/billing/apache/django.wsgi>
Order allow,deny
Allow from all
</Directory>

AliasMatch ^/([^/]*\.css) /usr/local/wsgi/static/styles/$1

Alias /media/ /usr/local/django/billing/media/
Alias /static/ /usr/local/django/billing/static/

<Directory /usr/local/django/billing/static>
Order deny,allow
Allow from all
</Directory>

<Directory /usr/local/django/billing/media>
Order deny,allow
Allow from all
</Directory>

Edit #1:

I've gone through the slides multiple times, from the start: still no joy. Even after opening up the path to the script, chmod'ing every relevant directory to be readable, and chmod'ing the .wsgi script, I still get permission denied. If I change the directory path from /usr/local/django/billing/apache/django.wsgi to have the django.wsgi truncated, the server returns a configuration error, despite that being how it's configured in the slides.

Upvotes: 6

Views: 34611

Answers (4)

tstoev
tstoev

Reputation: 1435

I got it working after installing flask(in venv) and setting the WSGISocketPrefix. I am deploying on centos6.8 running python3.6 in venv.

The project folder referenced here is the place where the actual django code is stored. The project public folder references here is accessed by the apache and contains simlinks to relevant resources along with the .htaccess and the .wsgi files relevant for the execution

The socket prefix may vary depending on the os and the apache configuration. The permissions may vary depending on the apache version if you have issues you can change:

Order allow,deny
 Allow from all

to

Require all granted

here is my mod_wsgi configuration (httpd.conf)

LoadModule wsgi_module **path to venv**/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so

WSGIPythonHome **path to venv**
WSGIDaemonProcess flask user=**user name** group=**user group**
WSGIProcessGroup flask
WSGISocketPrefix /var/run/wsgi 

<Directory **path-to-project-dir**>
     Options ExecCGI MultiViews Indexes
     MultiViewsMatch Handlers

     AddHandler wsgi-script .py
     AddHandler wsgi-script .wsgi

     DirectoryIndex index.html index.php index.py app.wsgi

     Order allow,deny
     Allow from all
</Directory>

here is the virtual host (httpd.conf)

<VirtualHost *:80>
    DocumentRoot **project public folder**
    ServerName **www.project.com**
    ServerAlias **project.com**

    WSGIScriptAlias / **project public folder**/site.wsgi

    Alias /media/ **project public folder**/media/
    <Directory **project public folder**/media>
        Order allow,deny
        Allow from all
    </Directory>

    Alias /static/ **project public folder**/static/
    <Directory **project public folder**/static>
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

here is the site.wsgi file

import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('**path to venv**/lib/python3.6/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('**path to project folder containing manage.py**')
sys.path.append('**path to project folder containing settings.py**')

os.environ['DJANGO_SETTINGS_MODULE'] = '**project name**.settings'

# Activate your virtual env
#activate_venv.py is an empty python file which will activate
#the virtual environment when executed. Create it manually
activate_env=os.path.expanduser("**path to venv**/bin/activate_venv.py")
exec(open(activate_env).read(), dict(__file__=activate_env))

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

I had to move following two lines from the wsgi.py to "init.py" in the same folder to solve "Applications not ready error"

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

Here is a dump of the packages in the virtual environment

Click==7.0
Django==2.2.1
django-debug-toolbar==1.11
django-redis==4.10.0
django-tastypie==0.14.2
Flask==1.0.2
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
mod-wsgi==4.6.5
mysqlclient==1.4.2.post1
Pillow==6.0.0
pip==19.1.1
python-dateutil==2.8.0
python-mimeparse==1.6.0
pytz==2019.1
redis==3.2.1
setuptools==41.0.1
simplejson==3.16.0
six==1.12.0
sqlparse==0.3.0
Werkzeug==0.15.4

Upvotes: 1

pedwards
pedwards

Reputation: 423

Same configuration, same environment... but everything was working except a simple call to Popen() in one of my django/python routines...

"Permission denied"

Turned out to be SELINUX (enforcing mode) blocking apache.

You can make SELINUX happy with your application by running the following commands:

# semanage fcontext -a -t httpd_sys_rw_content_t '/path/to/your/app(/.*)?'
# restorecon -R -v /path/to/your/app

Upvotes: 19

Ryu_hayabusa
Ryu_hayabusa

Reputation: 3722

I had the same issue,Sometimes this happends if the WSGI application is located outside of any directories already configured to be accessible to Apache, particularly when it is on your home directory, its good to specify user=username directive.

/etc/apahe2/sites-avaliable/myvhost [section]

WSGIDaemonProcess localhost python-path=/home/hemanth/ecm:/home/env/lib/python2.7/site-packages  user=hemanth
WSGIProcessGroup localhost

/etc/apahe2/sites-avaliable/myvhost [full]

    <VirtualHost *:80>
            ServerAdmin [email protected]
            ServerName localhost
            ServerAlias localhost

        DocumentRoot /home/hemanth/ecm

        <Directory /home/hemanth/ecm>
            Order allow,deny
            Allow from all
            </Directory>

           WSGIScriptAlias / /home/hemanth/ecm/index.wsgi       
           WSGIDaemonProcess localhost python-path=/home/hemanth/ecm:/home/env/lib/python2.7/site-packages user=hemanth     
           WSGIProcessGroup localhost


           Alias /static/ /home/hemanth/ecm/static/

           Alias /media/ /home/hemanth/ecm/media/   
           <Directory /home/hemanth/ecm/media/>
           Order allow,deny
           Allow from all
           </Directory>

           <Location "/static/">
            Options -Indexes
            </Location>     

           ErrorLog /home/hemanth/ecm/error.log 
</VirtualHost>

index.wsgi

import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/home/hemanth/env/local/lib/python2.7/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('/home/hemanth/ecm')
sys.path.append('/home/hemanth/ecm/ecm')

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

# Activate your virtual env
activate_env="/home/hemanth/env/bin/activate_this.py"
execfile(activate_env, dict(__file__=activate_env))

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

Upvotes: 6

Related Questions