Apache, Django and wsgi, No module named 'encodings' - django

I've looked through many stack overflow questions with the same issues, but none of them solved my issue. I'm trying to deploy Django on AWS - Ubuntu 18.04 using Apache and WSGI.
When running my website with Django's servers on port 8000, everything works fine. Then when I attempt to run on apache I get the following error in my logs.
Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
Command line: '/usr/sbin/apache2'
(2)No such file or directory: mod_wsgi: Unable to stat Python home /var/www/html/projects/venv
Python interpreter may not be able to be initialized correctly. Verify the supplied path
and access permissions for whole of the path.
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
As it says clearly mod_wsgi and Python 3 are configured. I am using a virtual environment. My project is located at:
/var/www/html/project/venv/mysite
My Apache conf file is configured as:
<Directory /var/www/html/projects/venv/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess mysite python-path=/var/www/html/projects/venv/mysite python-home =/var/www/html/projects/venv
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/html/projects/venv/mysite/mysite/wsgi.py
Alias /static /var/www/html/projects/venv/mysite/polls/static
<Directory /var/www/html/projects/venv/mysite/polls/static>
Require all granted
</Directory>
I'm not sure where to go from here. I don't fully understand what the error means. I have Python installed in my Venv. There is a file pyenv.conf which reads
home = /usr
implementation = CPython
version_info = 3.6.9.final.0
virtualenv = 20.0.20
include-system-site-packages = false
base-prefix = /usr
base-exec-prefix = /usr
base-executable = /usr/bin/python3
I'm wondering if this base-executable path being different than my python path in the apache config file is causing problems, but I'm not sure exactly what I need to do from here (or if this is even part of the problem. I believe permissions should not be the issue since I added all of my files within the var/www/html folder.
Update: I added the recommended permissions to all files in the chain and that didn't change anything.
Any advice is appreciated.

Related

Unabled to deploy django + apache on aws Ubuntu server

I am trying to deploy a Django Application using Apache on an Ubuntu Server, I am having a weird error (500 internal error server error) when using port :80.
I made some test using port :8000 running the command:
python3 manage.py runserver 0.0.0.0:8000
And the application is working without any problem there, but when I try to access the application using port 80 it does work.
My apache config file looks like this:
<VirtualHost *:80>
Alias /static /home/ubuntu/gh_system/static
<Directory /home/ubuntu/gh_system/static>
Require all granted
</Directory>
<Directory /home/ubuntu/gh_system/HRSYSTEM>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess HRSYSTEM python-home=/home/ubuntu/gh_system/env python-path=/home/ubuntu/gh_system>
WSGIProcessGroup HRSYSTEM
WSGIScriptAlias / /home/ubuntu/gh_system/HRSYSTEM/wsgi.py
I reviewed the apached2 errors logs, and I am getting a: No module named 'django', my wsgi files is:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "HRSYSTEM.settings")
application = get_wsgi_application()
After some research, testing and reviewing the apache logs with:
sudo tail -100 /var/log/apache2/error.log
I found that the problem was related to this line:
WSGIDaemonProcess HRSYSTEM ***python-home=/home/ubuntu/gh_system/env*** python-path=/home/ubuntu/gh_system>
Basically, apache was trying to read the packages inside of the virtual environment but I did install them using the command:
sudo pip3 install -r requirements.txt
Apparently, using the "sudo" command was installing the packages outside the virtual environment, it is so weird; but without "sudo" the console logs the error permission denied, so I give chmod 700 permission to the virtual environment and install again the packages without the "sudo", and then it works!
Some of the things I learned about the errors:
Problem: Not module 'encodings' --> this problem occurs when apache can not access your packages, usually for a bad path specified in the config file.
Problem: Not module 'x module, could be django' means you need to install the package using pip3.
Hope it helps someone! and just to point out, something I have been learning lately, is: when you spend too much time trying to solve a problem, and there is not much information about it on internet, it means that something really, really basic is failing in your project; for example, installing the packages outside the virtual environment without even realizing it, just like I did.

Django not running: ModuleNotFoundError: No module named 'encodings'

I have installed django website in a new Ubuntu 17.10 server. It is based on python3. It works fine when i run the website in development mode using
python manage.py runserver
But when i tried to configure it via apache2 webserver in port 80 by adding wsgi in apache by editing default config file as shown..
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /home/usr/mygpsproject/gps_restapi/gps_restapi>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess gps_restapi python-path=/home/usr/mygpsproject/gps_restapi/ python-home=/home/usr/mygpsproject/venv/bin/
WSGIProcessGroup gps_restapi
WSGIScriptAlias / /home/usr/mygpsproject/gps_restapi/gps_restapi/wsgi.py
</VirtualHost>
but Site is not working. When I checked the apache error log to find this error...
[Sat Apr 14 16:16:09.201739 2018] [core:notice] [pid 5089:tid 140258267696064] AH00051: child pid 7500 exit signal Aborted (6), possible coredump in /etc/apache2
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00007f906c390bc0 (most recent call first):
I am not able to find what is this encodings module. it worked fine in development mode. so python is not missing any libraries for sure.
What could be the problem with my configuration.
Instead of:
python-home=/home/usr/mygpsproject/venv/bin/
you should be using:
python-home=/home/usr/mygpsproject/venv
Review the documentation on using virtual environments. You are supposed to point to the root of the virtual environment as given by sys.prefix, not the bin directory.
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
Also ensure that the virtual environment is created from the same Python version that mod_wsgi was compiled for.
Finally, depending on the Linux distribution used, you also shouldn't place your code under your personal home directory as the permissions on the home directory can be such that the user that runs your code as under Apache can't access the home directory.

Apache with virtualenv and mod_wsgi : ImportError : No module named 'django'

I'm trying to serve a little django project with the following Apache configuration :
Apache virtualhost configuration :
<VirtualHost *>
ServerName servername
[...]
<Directory "/path/to/project/project">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess project python-path=/path/to/project:/path/to/Envs/venv/lib/python3.5/site-packages
WSGIScriptAlias / /path/to/project/project/wsgi.py
</VirtualHost>
I also have the following wsgi.py :
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example.settings")
application = get_wsgi_application()
I have no problem to serve STATIC files and MEDIA files.
I also checked permissions and tried to recursively use 755, then 777 to my virtualenv's site-package directory. It didn't work.
But when trying to reach the root of my site I get the following :
from django.core.wsgi import get_wsgi_application
ImportError: No module named 'django'
I guessed that it was a Python path related problem since django is installed in my virtualenv. But I added the relevant python paths to the WSGIDaemonProcess's python-path attribute so I don't get why it doesn't work.
I also guess I could add the relevant directory to my Python path in my wsgi.py by using the site module, but I'd like to understand why the Apache configuration I tried isn't enough. Did I miss something?
You are missing a WSGIProcessGroup directive or equivalent option on WSGIScriptAlias, so your application isn't actually being run in that daemon process group where you have set the virtual environment.
See Using mod_wsgi daemon mode
I would also recommend ensuring application group is set to '%{GLOBAL}' if that is the only application you are running in the daemon process group.
Thus use:
WSGIScriptAlias / /path/to/project/project/wsgi.py \
process-group=project application-group=%{GLOBAL}
Also better to use python-home for the virtual environment.
WSGIDaemonProcess project python-path=/path/to/project \
python-home=/path/to/Envs/venv
See:
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
My rep is not over 50 so I cannot comment, but I'd like to share my discovery.
In WSGIDaemonProcess, if you are using Python 3.5, you need to set exactly as #graham-dumpleton say, with
python-home=/path/to/Envs/venv
set explicitly.
However, if you are using Python 3.4 (or some older version Python like 2.7 as far as I know), you'll have to configure it as
python-path=/path/to/project:/path/to/Envs/venv/lib/python3.4/site-packages
just like what the asker did.
Really weird.
For me the problem was I had mod wsgi installed for python2. I had to reinstall it for python3:
sudo apt-get install libapache2-mod-wsgi-py3
The issue for me was that I was running libapache2-mod-wsgi-py3 on python version 3.9. When I rolled back the python version to 3.7, there was no issue.

Django - Apache2 - Postgresql: error with _psycopg

Trying to deploy a django project on apache (2.2) on Debian 7.
Using Django 1.8 and a virtual environment with python3.4.
Getting the following error:
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named _psycop
I have tried the suggestions pointed out here, and here.
Here is my apache conf.
(following lines are nested within VirtualHost scope)
#WSGI configuration for ds_monitor django project
Alias /static /home/pkaramol/Applications/timeds/ds_monitor/static_root
<Directory /home/pkaramol/Applications/timeds/ds_monitor/static_root>
Allow from all
</Directory>
<Directory /home/pkaramol/Applications/timeds/ds_monitor/ds_monitor>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
WSGIDaemonProcess ds_monitor python-path=/home/pkaramol/Applications/timeds/ds_monitor:/home/pkaramol/Applications/timeds/venv_p34dj18/lib/python3.4/site-packages
WSGIProcessGroup ds_monitor
WSGIScriptAlias /ds_monitor /home/pkaramol/Applications/timeds/ds_monitor/ds_monitor/wsgi.py
and this outside VirtualHost scope (to enable production - time virtual environment)
WSGIPythonPath /home/pkaramol/Applications/timeds/ds_monitor:/home/pkaramol/Applications/timeds/venv_p34dj18/lib/python3.4/site-packages
Another issue is the following line present in apache's log stack trace
File "/usr/lib/python3.2/importlib/_bootstrap.py", line 821, in _gcd_import
loader.load_module(name)
Why is it using system's python 3.2?
Have I done s.th wrong in terms of pointing the virtualenv path correctly in wsgi confs?
In any case, the psycopg2 module is also installed in the system (not only in virtualenv)
Seems very weird but the whole issue most likely had to do with the python version that mod_wsgi was compiled against.
I had downloaded / configured / installed version 4.5.2 of mod_wsgi from here but forgot to configure with the appropriate python version.
So I run through the whole process again (starting from unzipping the original tarball) and configuring as follows:
./configure --with-python=/usr/bin/python3.5
Restarted apache and problem solved.

Django Apache wsgi virtualenv import error

I'm trying to deploy Django (located in a virtualenv) on Apache using WSGI deploying. I'm following the default tutorial from https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/
wsgi.py (the default one which Django generated, with the comments dropped):
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
apache2.conf (its the same httpd.conf just in Debian ). Appended this to the end:
WSGIScriptAlias / /home/user/Desktop/expofit/expofit_hg/py/server/server/wsgi.py
WSGIDaemonProcess example.com python-path=/home/user/Desktop/expofit/expofit_hg/py/server:/home/user/Desktop/expofit/expofit_env/lib/python2.7/site-packages
WSGIProcessGroup example.com
<Directory /home/user/Desktop/expofit/expofit_hg/py/server/server>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
Alias /static/ /home/user/Desktop/expofit/expofit_hg/py/server/server/static
<Directory /home/user/Desktop/expofit/expofit_hg/py/server/server/static>
Order deny,allow
Allow from all
</Directory>
However, this ends with an error:
[Thu Dec 06 17:08:40 2012] [error] [client 192.168.56.1] ImportError: No module named django.core.wsgi
It seems that the standard python is accessible, since
import os
yields no errors. So it seems that modules imported from the virtualenv aren't importable.
The tutorial said:
A further change required to the above configuration if you use
daemon mode is that you can't use WSGIPythonPath; instead you should
use the python-path option to WSGIDaemonProcess, for example:
WSGIDaemonProcess example.com python-path=/path/to/mysite.com:/path/to/venv/lib/python2.7/site-packages
WSGIProcessGroup example.com
What am I missing?
The problem was in the permissions. I didn't check who was the user, and what the permissions were at the beginning, however, when I changed the permission 777 to all the directories containing Django code files, it started working.
I'm aware that a person has to be as careful as possible with permissions, and that giving 777 to everything isn't the best way to do it but should check how to make it work with minimum permission change. It however solves the problem in the question.
the pythonpath your envinronment is different than the apache one i think.
install django "globaly" with easy_install or pip
or add .virtualenv pythonpath to the mod_wsgi config
WSGIPythonPath directory|directory-1:directory-2:
MOD_wsgi config
For a single app this is the easiest to get out of the box, see http://code.google.com/p/modwsgi/wiki/VirtualEnvironments#Baseline_Environment Neither this or using WSGIPythonPath can be done for just a vhost but must be global.
WSGIPythonHome [path to virtualenv folder]
If you have multiple apps - using sys.path to append your virtualenv's site-packages folder at the top of wsgi.py seems the easiest thing to do, see http://code.google.com/p/modwsgi/wiki/VirtualEnvironments#Application_Environments.