Django - Apache2 - Postgresql: error with _psycopg - django

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.

Related

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

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.

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 (1.6) + Apache2

First time I setup Django and Apache, and I'm having a tough time. It's seems easy by following tutorials, but it doesn't seem to work.
Basically, the steps I followed (on Debian):
Install Django (and some libraries)
Install Apache2
Install mod_wsgi
Then I've put my Django app in /root/, so the path is something like :
root/
.Projet/
.myprojet/
.site/
. .#here the models.py, views.py, etc of my site
.myprojet/
. .#here the settings.py, wsgi.py
. .static/
. .#static files in folders
.templates
. .#my templates
.database.sql
.manage.py
Then in /etc/apache2 I created the file httpd.conf, with this inside :
WSGIScriptAlias / /root/Projet/myprojet/myprojet/wsgi.py
WSGIPythonPath /root/Projet/myprojet
<Directory /root/Projet/myprojet/myprojet>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
I do a2enmod wsgi (it tells me "Module wsgi already enabled").
Then service apache2 restart (successful, but tells me I have no VirtualHosts).
When I go to my website (using the IP adress), just the Apache "It works" display.
Thank you for your help ^^
You shouldn't put your Apache configuration file in /etc/apache2/. The fact that you mention a2enmod implies that you are on a Debian-based distro like Ubuntu. For those systems, you need to give that file a name related to the app - call it django if you like - and put it in /etc/apache2/sites-available. Then you can run a2ensite django to link it to sites-enabled, restart Apache, and it should work.

Configuring Apache 2 with mod_wsgi for Django - no httpd.conf file

I am using Apache 2 on raspbian os. I read somewhere that in the newest versions of Apache there is no httpd.conf file, is this true? [Edit] (This is true)
My apache server is running (I can see the default web page), and mod_wsgi is successfully installed. So I began the instructions here for using django 1.5 with apache. However it tells me I need to add the following lines to the httpd.conf file:
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
However, there is no httpd.conf file present in my version of apache. I have checked /etc/httpd/confand /etc/apache2 for the http.conf file. /etc/apache2 is where apache is though.
In short, where should I add the required settings so that apache recognizes mod_wsgi.
Possibly, many distros use
/etc/apache2/apache2.conf
Instead. However you shouldn't really edit that and it affects apache2 globally, i.e. all you sites (virtual-hosts). If you have only the one site better edit the contents of:
/etc/apache2/sites-available/default
The Ubuntu Apache2 configuration documentation is a good getting started guide: https://help.ubuntu.com/14.04/serverguide/httpd.html#http-configuration
Here is tutorial configuring Apache2 with mod_wsgi for Django on Ubuntu 11.04 wihtout httpd.conf: http://blog.madspace.me/configure-python-django-with-apache/, and one on 12.04: http://www.lennu.net/2012/05/14/django-deployement-installation-to-ubuntu-12-dot-04-server/