6

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.

Hunsu
  • 3,281
  • 7
  • 29
  • 64
patrickn
  • 2,501
  • 4
  • 22
  • 21

4 Answers4

19

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
Jim Hunziker
  • 14,111
  • 8
  • 58
  • 64
pedwards
  • 423
  • 2
  • 12
  • 3
    This tuned out to be the reason for my problem as well. I had to turn off SELinux. You can check the current value by reading /etc/selinux/config file. Alternatively running getenforce shows the current setting. If you want ti disable it, run setenforce 0 which will set it to permissive. I'm not sure what's the best way to keep SELinux on and at the same time fix this issue. – m.hashemian Nov 02 '15 at 18:17
  • This is the correct answer We should avoid turning off selinux as correct answers over internet – José Apr 13 '20 at 21:41
6

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 xy@gmail.om
            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()
Ryu_hayabusa
  • 3,666
  • 2
  • 29
  • 32
6

I had the same problem with permission denied. https://serverfault.com/questions/357804/apache2-mod-wsgi-django-named-virtual-servers

The specific error is described in: http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations

Community
  • 1
  • 1
dm03514
  • 54,664
  • 18
  • 108
  • 145
  • Thank you for the link! I wont be in a position to test the things in the slideshow until tomorrow, but I will be sure to return with what works and doesn't. – patrickn Feb 21 '12 at 23:53
  • See my edit above: I am at my wit's end with this issue. Nothing I read seems to mirror my situation. I get a server configuration error when I should be getting a permission denied error on slide #8. Opening the directory to access the script returns a configuration error. I think I'm going nuts. – patrickn Feb 22 '12 at 15:50
  • 1
    Success! Using http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango, I noticed that they did two sys.path.append commands... one for the root folder containing the django project, and another for the actual project itself (which the slideshow uses)... adding in the root folder to the path solved the problem! – patrickn Feb 22 '12 at 16:09
1

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
tstoev
  • 1,415
  • 11
  • 12