Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'No module named 'encodings'' error when deploying django app on apache with mod_wsgi

Versions list:

Centos 7---django 2.1.7---Apache 2.4.6---Python 3.6

Check EDIT 5 for most recent error log

followed this tutorial here: https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-centos-7

I am able to run sudo systemctl start httpd and when i go to the url i get a Internal sever error (logs at the bottom)

My file structure

home
└── user
    └── projects
        └── myapp
            ├── app
            │   ├── <All Code for Webapp including static dir>  
            ├── env (virtualenv)
            ├── manage.py
            ├── new
            │   ├── settings.py
            │   ├── urls.py
            │   └── wsgi.py
            └── requirements.txt

And i hoping someone can see a mistake in my django.conf located in my further down in my httpd folder. And hoping these are the endpoints i'm looking for

EDIT: This file is located here: /etc/httpd/conf.d/django.conf

django.conf

Alias /static /home/user/projects/myapp/app/static
<Directory /home/user/projects/myapp/app/static>
    Require all granted
</Directory>

<Directory /home/user/projects/myapp/new>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess myapp python-path=/home/user/projects/myapp python-home=/home/user/projects/myapp/env 
WSGIProcessGroup myapp
WSGIScriptAlias / /home/user/projects/myapp/new/wsgi.py

I'm not sure if these are pointing to all the right places and was hoping someone could give me a second look.

And i havent touched wsgi.py and was wondering if i am missing any logic there.

my wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'new.settings')

application = get_wsgi_application()

EDIT2: i am able to run the server but get a 500 internal server error and when i check the error_logs i get this

Updated Logs

[Mon Mar 11 10:40:39.865611 2019] [core:notice] [pid 9371] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
[Mon Mar 11 10:40:39.868149 2019] [suexec:notice] [pid 9371] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Mar 11 10:40:39.918015 2019] [auth_digest:notice] [pid 9371] AH01757: generating secret for digest authentication ...
[Mon Mar 11 10:40:39.919681 2019] [lbmethod_heartbeat:notice] [pid 9371] AH02282: No slotmem from mod_heartmonitor
[Mon Mar 11 10:40:39.948303 2019] [mpm_prefork:notice] [pid 9371] AH00163: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations
[Mon Mar 11 10:40:39.948370 2019] [core:notice] [pid 9371] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Mon Mar 11 10:40:42.878806 2019] [mime_magic:error] [pid 9376] [client ip] AH01512: mod_mime_magic: can't read `/home/user/projects/myapp/new/wsgi.py'
[Mon Mar 11 10:40:42.879459 2019] [mime_magic:error] [pid 9376] [client ip] AH01512: mod_mime_magic: can't read `/home/user/projects/myapp/new/wsgi.py'
[Mon Mar 11 10:40:42.905048 2019] [:error] [pid 9372] (13)Permission denied: [remote ip] mod_wsgi (pid=9372, process='myapp', application='<url>|'): Call to fopen() failed for '/home/user/projects/myapp/new/wsgi.py'

EDIT3

In this log file it says this Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 configured when i am running python3.6 not 2.7.5, Maybe changing this would help?

EDIT 4

So i changed my WSGIDaemonProcess to

WSGIDaemonProcess myapp python-path=/home/user/projects/myapp python-home=/home/user/projects/app/env/lib/python3.6/site-packages user=<user>

Now im getting these errors

[Tue Mar 12 10:38:09.111397 2019] [mime_magic:error] [pid 18804] [client ip] AH01512: mod_mime_magic: can't read `/home/user/project/myapp/new/wsgi.py'
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site

so it looks like i need to uninstall mod_wsgi and reinstall to compile with python3.6 and not 2.7

How can i succesfully do this? i uninstall mod_wsgi and try mreinstall using sudo pip3.6 install mod_wsgi Now it doesnt recognize the new install of mod_wsgi and wont start the server because of an invalid command 'WSGIDaemonProcess' this is bc the new download isnt configured with httpd? how would i do this?

EDIT 5

Successfully installed mod_wsgi compiled with correct python version and able to run server, set SELinux to permissive to get rid of cant find '/route/to/wsgi/py. and now I am now getting no module name 'encoding' errors that repeats.

[Wed Mar 13 15:20:46.133597 2019] [core:notice] [pid 4403] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
[Tue Mar 12 14:11:57.520271 2019] [core:notice] [pid 866] AH00052: child pid 891 exit signal Aborted (6)
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

currently my permissions are all under user apache and group apache, and seems they are all under the right permissions, and i tried changing the root user/group to apache as well, and ran chmod -R 777 at the Myapp directory to make sure it had proper permissions.

Permissions under projects:

home/user/projects# ls -l

total 0
drwxrw-r-x. 7 apache apache 109 Mar 12 12:48 myapp

home/user/projects/myapp# ls -l

Permissions under myapp:

total 12
drwxrwxr-x. 7 apache apache 4096 Mar  7 13:18 app
drwxr-xr-x. 5 apache apache   56 Mar 12 12:48 env
-rwxrwxr-x. 1 apache apache  535 Mar  5 13:33 manage.py
drwxrwxr-x. 3 apache apache  110 Mar  7 14:27 new
-rw-rw-r--. 1 apache apache  869 Mar  7 14:42 requirements.txt
like image 671
Mfreeman Avatar asked Mar 08 '19 15:03

Mfreeman


People also ask

What is mod_wsgi in Django?

mod_wsgi is an Apache module which can host any Python WSGI application, including Django. Django will work with any version of Apache which supports mod_wsgi. The official mod_wsgi documentation is your source for all the details about how to use mod_wsgi.

What is mod_wsgi in Apache?

mod_wsgi is an Apache HTTP Server module by Graham Dumpleton that provides a WSGI compliant interface for hosting Python based web applications under Apache. As of version 4.5. 3, mod_wsgi supports Python 2 and 3 (starting from 2.6 and 3.2).


3 Answers

According to mod_wsgi's documentation:

user=name | user=#uid Defines the UNIX user name or numeric user uid of the user that the daemon processes should be run as. If this option is not supplied the daemon processes will be run as the same user that Apache would run child processes, as defined by the User directive, and it is not necessary to set this to the Apache user yourself.

I suppose that the apache user cannot access your home directory. Either try to add the user=myUserdirective to your WSGIDaemonProcess or chown your directory to the apache user. Another possible way would be to put your application inside a directory owned by apache user.

EDIT

I really should learn to read. According to the docs, it looks like you should recompile mod_wsgi using a python3.X interpreter in order to run python3.X applications (use the same versions for both compiling and application's environment)

If you want to use a different version of Python, you will need to reinstall mod_wsgi, compiling it for the version you want. It is not possible for the one mod_wsgi instance to run applications for both Python 2 and 3 at the same time.

EDIT 2

To answer one of your comments: It's normal that yum won't remove the version you installed through pip. It has no way to guess that you installed packages through other package managers AFAIK. I think that you should copy some the .so (I think it's mod_wsgi[version].so) files that lies in the directory where pip installed mod_wsgi (inside your venv/global site_packages I think) into the folder where Apache/Httpd loads its modules files (it's something like /usr/lib/apache2/modules on alpine/ubuntu so I guess it is /usr/lib/httpd/modules for CentOS, but can be different, sorry I can't give you more specific help) after moving out of this folder the old mod_wsgi.so. Don't forget to shutdown httpd before doing this, and restart it after.

Source: https://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html (section python-home)

like image 53
Yoiro Avatar answered Nov 01 '22 11:11

Yoiro


Try permission on dir level rather on file.

<Directory /home/user/projects/myapp/new>
     Require all granted
</Directory>
like image 34
webbyfox Avatar answered Nov 01 '22 11:11

webbyfox


You can check user and group for apache by

egrep -i '^user|^group' /etc/httpd/conf/httpd.conf

So make sure you have permissions on /home/user/projects/myapp/new/wsgi.py

recheck by ls -l

Try after for example if User is apache and group is apache

sudo chown apache:apache /home/user/projects/myapp/new/wsgi.py

like image 1
Yugandhar Chaudhari Avatar answered Nov 01 '22 11:11

Yugandhar Chaudhari