Chad Crowe Chad Crowe - 9 days ago 9
Linux Question

Django Apache Redhat Permission denied, call to fopen() failed

/var/log/httpd/error_log says,

[Tue Nov 22 12:54:51.763863 2016] [mime_magic:error] AH01512: mod_mime_magic: can't read /var/www/Django_Project/cbtraffic/cbTraffic/cbTraffic/wsgi.py', referer: http://ipaddress/Live
[Tue Nov 22 12:54:51.763953 2016] [mime_magic:error] AH01512: mod_mime_magic: can't read /var/www/Django_Project/cbtraffic/cbTraffic/cbTraffic/wsgi.py', referer: http://ipaddress/Live
[Tue Nov 22 12:54:51.771547 2016] [:error] (13)Permission denied: client ######## mod_wsgi (pid=39821, process='', application='omadi-trafcbi01.intranet.hdr|'): Call to fopen() failed for '/var/www/Django_Project/cbtraffic/cbTraffic/cbTraffic/wsgi.py'., referer: http://ipaddress/Live


I moved my project out of my home folder to /var/www/ in an attempt to fix the permission issues (to no avail).

here is what I've added to /etc/httpd/conf/httpd.conf,

WSGIScriptAlias / /var/www/Django_Project/cbtraffic/cbTraffic/cbTraffic/wsgi.py
WSGIPythonPath /bin/python3/site-packages:/var/www/Django_Project/cbtraffic/cbTraffic
<Directory /var/www/Django_Project/cbtraffic/cbTraffic/cbTraffic>
<Files wsgi.py>
Require all granted
</Files>
</Directory>


I also have a /etc/httpd/sites-enabled/000-default file,

<VirtualHost *:80>
WSGIScriptAlias / /var/www/Django_Project/cbtraffic/cbTraffic/cbTraffic/wsgi.py
<Directory "/var/www/Django_Project/cbtraffic/cbTraffic/cbTraffic">
Require all granted
</Directory>
WSGIDaemonProcess cbTraffic python-path=/var/www/Django_Project:/bin/python3/site-packages
WSGIProcessGroup cbTraffic
WSGIScriptAlias / /var/www/Django_Project/cbtraffic/cbTraffic/cbTraffic/wsgi.py
</VirtualHost>


Here is my wsgi.py file,

import os
import sys
import site
sys.path.append("/var/www/Django_Project/cbtraffic/cbTraffic")
sys.path.append("/var/www/Django_Project/cbtraffic/cbTraffic/cbTraffic/wsgi.py")
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cbTraffic.settings")
application = get_wsgi_application()


This is my first time trying to launch Django on Redhat. Any help would be appreciated.

Answer

My initial issue was with Selinux. I went to /etc/selinux/config and changed the file so that

SELINUX=permissive

before, my file had said

SELINUX=enforcing

This change got rid of the permission error.

From there I had other errors with either being unable to import site, or being unable to import django.core.wsgi.

I went to the Django docs https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/ and added a virtualenv and used mod_wsgi in daemon mode.

The overall problem I was having was with apache using Python 2. I would get a tiny message telling me that loading mod_wsgi was being skipped,

"AH01574: module wsgi_module is already loaded, skipping". 

It turns out that I had mod_wsgi (version for python 2) installed on the machine. I removed this and upon running service httpd start I got the error,

"httpd: Syntax error on line 379 of /etc/httpd/conf/httpd.conf: Cannot load /etc/httpd/modules/mod_wsgi.so into server: /etc/httpd/modules/mod_wsgi.so: cannot open shared o...file or directory"

It turns out that the mod_wsgi was not loading. Well, I had forgotten to make the mod_wsgi for python 3 with the new virtualenv location.

I used these instructions for getting mod_wsgi to work with Python 3 on Redhat

https://code.google.com/p/modwsgi/wiki/DownloadTheSoftware

cd /usr/local/src
sudo wget https://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz
sudo tar -zxvf mod_wsgi-3.4.tar.gz 
cd mod_wsgi-3.4/
sudo ./configure --with-python=/usr/bin/python3.3
sudo make
sudo make install
mod_wsgi.so is placed in /usr/lib/apache2/modules/

But this time I used my path to python3 within my virtualenv. I was also installing my pip packages with, "

pip install -I <name>

". The -I will install the package within your virtual environment (instead of using the global site-packages).

Here is the main part of my /etc/httpd/conf/httpd.conf file:

WSGIScriptAlias / /var/www/Django_Project/cbtraffic/cbTraffic/cbTraffic/wsgi.py process-group=example.com


WSGIDaemonProcess example.com python-path=/var/www/Django_Project/cbtraffic/cbTraffic:/usr/lib/python3.4/site-packages/cbenv/lib/python3.4/site-packages
WSGIProcessGroup example.com


<Directory /var/www/Django_Project/cbtraffic/cbTraffic/cbTraffic>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

then at the bottom of my /etc/httpd/conf/httpd.conf file:

LoadModule wsgi_module /etc/httpd/modules/mod_wsgi.so
Include /etc/httpd/sites-enabled/000-default
IncludeOptional conf.d/*.conf

This loads the mod_wsgi.so file you made. As you can see, I also had a sites-enabled/000-default file. Here are those contents below:

<VirtualHost *:80>
    WSGIScriptAlias / /var/www/Django_Project/cbtraffic/cbTraffic/cbTraffic/wsgi.py
    <Directory "/var/www/Django_Project/cbtraffic/cbTraffic/cbTraffic">
      Require all granted
    </Directory>


WSGIDaemonProcess cbTraffic python-path=/var/www/Django_Project:/usr/lib/python3.4/site-packages/cbenv/lib/python3.4/site-packages
    WSGIProcessGroup cbTraffic
    WSGIScriptAlias / /var/www/Django_Project/cbtraffic/cbTraffic/cbTraffic/wsgi.py




</VirtualHost>