apache wsgi_mod fails to run on python 3.10 - django

I am developing a django app using django 4 and python 3.10.
I had a server using apache and mod_wsgi, that used to run an old app using python3.8.
I've created a virtual env from 3.10. Command line manage.py is working well.
I've changed apache's conf:
WSGIDaemonProcess my_app310 python-path=/home/ubuntu/my_app310:/home/ubuntu/venv/my_app310/lib/python3.10/site-packages
WSGIProcessGroup my_app310
WSGIScriptAlias / /home/ubuntu/my_app310/my_app310/wsgi.py
But I keep getting:
[mpm_event:notice] AH00489: Apache/2.4.41 (Ubuntu) mod_wsgi/4.6.8 Python/3.8 configured -- resuming normal operations
[core:notice] AH00094: Command line: '/usr/sbin/apache2'
[wsgi:error] mod_wsgi (pid=1724834): Failed to exec Python script file '/home/ubuntu/my_app310/my_app310/wsgi.py'.
[wsgi:error] mod_wsgi (pid=1724834): Exception occurred processing WSGI script '/home/ubuntu/my_app310/my_app310/wsgi.py'.
[wsgi:error] Traceback (most recent call last):
[wsgi:error] File "/home/ubuntu/venv/my_app310/lib/python3.10/site-packages/django/utils/timezone.py", line 10, in <module>
[wsgi:error] import zoneinfo
[wsgi:error] ModuleNotFoundError: No module named 'zoneinfo'
Which suggest that something is running using python3.8 and not python3.10 (mod_wsgi?). What am I missing?

to configure apache2 with a certain Python Version just do the following:
go into your virtual_env that you have created with Python3.10
$ pip install mod_wsgi
$ mod_wsgi-express module-config
you get an output like
LoadModule wsgi_module "..../lib/python3.10/site-packages/mod_wsgi/server/mod_wsgi-py310.cpython-310-x86_64-linux-gnu.so"
SSGIPythonHome "..."
copy the 2 lines into /etc/apache2/apache2.conf
restart apache2
you should see something like
[mpm_event:notice] AH00489: Apache/2.4.41 (Ubuntu) mod_wsgi/4.6.8 Python/3.10 config .....

WSGI Clearly says that File "/home/ubuntu/venv/my_app310/lib/python3.10/site-packages/django/utils/timezone.py", given this path has no module named zoneinfo try importing module into your python virtual environment.
Example:
pip install (module)

Related

'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
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)
Try permission on dir level rather on file.
<Directory /home/user/projects/myapp/new>
Require all granted
</Directory>
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
You have to pass library location for env variable.
WSGIDaemonProcess myapp python-path=/home/user/projects/myapp python-home=/home/user/projects/myapp/env/lib/<PYTHON VERSION>/site-packages/

Django folder permissions for installation

The official Django installation documentation seems to be lacking in exactly how to set up folder permissions post-installation so that a 403 Foridden error does not show up on a live apache installation.
I've spent 2 months going through thousands of forum posts, hundreds of guides, and countless Stackoverflow Q&As about setting up folder permissions. No single two of these posts, guides or Q&As are the same. All are riddled with users having to implement custom solutions here and there to get things working, there's no consistency anywhere as to exactly what folder permissions should be set up. I probably speak for silent untold thousands who gave up when trying to do folder permissions.
So my question is simple, how do I set the correct file and folder permissions on Ubuntu 16.04, configured with Python 2.11 in a virtual environment, with Django on an Apache mod_wsgi server?
My structure is as per the official documentation - /home/main/myproject/project
All of these folders (home, main, myproject and project) have 755 permissions owned by a non-root sudo user called 'main' which I use for everything.
I've done:
chmod 664 ~/myproject/db.sqlite3
chmod 775 ~/myproject
sudo chown :www-data ~/myproject/db.sqlite3
sudo chown :www-data ~/myproject
Apparently these are all the permissions I need. Yet I still get a 403 forbidden error. How on earth do I please get this Django thing live?
Edit: I've followed this tutorial exactly: https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-debian-8 particularly the part with appending /etc/apache2/sites-available/000-default.conf
My 000-default.conf append looks like:
<VirtualHost *:80>
. . .
Alias /static /home/main/myproject/static
<Directory /home/main/myproject/static>
Require all granted
</Directory>
# Next, add the following directory block
<Directory /home/main/myproject/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess project python-home=/home/main/myproject/projectenv python-path=/home/main/myproject
WSGIProcessGroup project
WSGIScriptAlias / /home/main/myproject/project/wsgi.py
My Apache error logs don't seem to show any issues after visiting the forbidden domain index:
[Wed Jun 27 07:03:46.807525 2018] [mpm_prefork:notice] [pid 21287] AH00171:
Graceful restart requested, doing restart
[Wed Jun 27 07:03:46.878042 2018] [wsgi:warn] [pid 21287] mod_wsgi: Compiled for Python/2.7.11.
[Wed Jun 27 07:03:46.878052 2018] [wsgi:warn] [pid 21287] mod_wsgi: Runtime using Python/2.7.12.
[Wed Jun 27 07:03:46.878142 2018] [mpm_prefork:notice] [pid 21287] AH00163: Apache/2.4.18 (Ubuntu) mod_fcgid/2.3.9 OpenSSL/1.0.2g mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
[Wed Jun 27 07:03:46.878156 2018] [core:notice] [pid 21287] AH00094: Command line: '/usr/sbin/apache2'
[Wed Jun 27 07:03:50.444008 2018] [mpm_prefork:notice] [pid 21287] AH00171: Graceful restart requested, doing restart
[Wed Jun 27 07:03:50.517693 2018] [wsgi:warn] [pid 21287] mod_wsgi: Compiled for Python/2.7.11.
[Wed Jun 27 07:03:50.517704 2018] [wsgi:warn] [pid 21287] mod_wsgi: Runtime using Python/2.7.12.
[Wed Jun 27 07:03:50.517823 2018] [mpm_prefork:notice] [pid 21287] AH00163: Apache/2.4.18 (Ubuntu) mod_fcgid/2.3.9 OpenSSL/1.0.2g mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
[Wed Jun 27 07:03:50.517837 2018] [core:notice] [pid 21287] AH00094: Command line: '/usr/sbin/apache2'

Getting Photologue was unable to import the Python Imaging Library error in django_inventory while deploying with Apache+Mod_wsgi

I am trying to deploy Django Inventory with Apache + Mod_wsgi in Virtual environment. But It gives 500 server side error claiming that it cannot import PIL. But PIL and Photologue are already installed within my virtualenv. And I had no problem getting a hello-world project running within this setup.
FYI: Django Inventory itself installs as an app when installed with pip. And resides in the site-packages of the virtual-environment. But I have copied it fro the ste packages and put it in the /var/www/html root to serve from apache. Could this be a reason?
Thanks in advance.
[Tue Jun 21 10:50:24.308075 2016] [wsgi:error] [pid 19197] [remote ::1:224] File "/var/www/html/imsenv/lib/python2.7/site-packages/django_inventory/apps/photos/models.py", line 6, in <module>
[Tue Jun 21 10:50:24.308099 2016] [wsgi:error] [pid 19197] [remote ::1:224] from photologue.models import ImageModel
[Tue Jun 21 10:50:24.308108 2016] [wsgi:error] [pid 19197] [remote ::1:224] File "/var/www/html/imsenv/lib/python2.7/site-packages/photologue/models.py", line 33, in <module>
[Tue Jun 21 10:50:24.308131 2016] [wsgi:error] [pid 19197] [remote ::1:224] raise ImportError('Photologue was unable to import the Python Imaging Library. Please confirm it`s installed and available on your current Python path.')
[Tue Jun 21 10:50:24.308170 2016] [wsgi:error] [pid 19197] [remote ::1:224] ImportError: Photologue was unable to import the Python Imaging Library. Please confirm it`s installed and available on your current Python path.
This is probably not the answer that you're looking for, but...
Django-photologue 2.3 is a really old version - about 6 years old.
Installing PIL was often troublesome; I am the maintainer of Django-photologue since 2012, and I've had to write some ugly code in order to import PIL correctly (the installation path tended to change from installation to installation). I think it was in version 2.6 that I made these changes.
Nowadays everyone just uses Pillow instead :-)
By the way, I looked at the requirements file for django-inventory and it uses very old 3rd party software. For example, Django 1.6 - which is no longer supported.

raise ImportError 'PILKit was unable to import the Imaging Library'

when i try to install PIL on my ubuntu server in virtualenv it is throwing this error
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-3qvaiv6l/PIL
however i have pillow installed. even then it is showing this error in error.logs
[Mon Mar 30 16:24:14.892127 2015] [:error] [pid 31004:tid 139913894803200] [client 122.167.116.201:57073] raise ImportError('PILKit was unable to import the P$
$n Imaging Library. Please confirm it`s installed and available on your current Python path.
What is the case here and how should i proceed?
Finally i got the answer. the problem was occuring because thewsgi.py file was not having the python path for the virtual environment in appropriate way.

ImportError: No module named django.core.wsgi Apache + VirtualEnv + AWS + WSGI

I am trying to publish my site on an Amazon's EC2 Instance, and I keep getting a 500 error. I really don't know why.
//Log Files
[Sun Feb 17 23:12:48.066802 2013] mod_wsgi (pid=2102): Target WSGI script '/srv/www/app/poka/apache/wsgi.py' cannot be loaded as Python module.
[Sun Feb 17 23:12:48.066840 2013] mod_wsgi (pid=2102): Exception occurred processing WSGI script '/srv/www/app/poka/apache/wsgi.py'.
[Sun Feb 17 23:12:48.066864 2013] Traceback (most recent call last):
[Sun Feb 17 23:12:48.066889 2013] File "/srv/www/mysite/poka/apache/wsgi.py", line 26, in <module>
[Sun Feb 17 23:12:48.066920 2013] from django.core.wsgi import get_wsgi_application
[Sun Feb 17 23:12:48.066945 2013] ImportError: No module named django.core.wsgi
//Apache Config Files
WSGIScriptAlias / /srv/www/app/mysite/apache/wsgi.py
WSGIDaemonProcess mysite python-path=/srv/www/app/mysite:/home/ec2-user/.virtualenvs/mysite-main/lib/python2.7/site-packages
WSGIProcessGroup mysite
<Directory /srv/www/app/mysite/apache/>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
<Directory /srv/www/app/mysite/apache/>
Order deny,allow
Allow from all
</Directory>
<Directory /home/ec2-user/app/mysite/static>
Order deny,allow
Allow from all
</Directory>
<Directory /home/ec2-user/app/mysite/media>
Order deny,allow
Allow from all
</Directory>
//wsgi.py
import os
import sys
import site
site.addsitedir('/home/ec2-user/.virtualenvs/mysite-main/lib/python2.7/site-packages')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
path = '/srv/www/app/mysite'
if path not in sys.path:
sys.path.append(path)
I know that this is an old thread but I've just bumped into the same issue and I don't think that this is caused by a missing package. As the Django core distribution contains the correct wsgi handler already.
The problem here is that when wsgi.py is executed it's missing the packages of the site-packages from your virtualenv. (If you have activated your virtualenv, and done pip install django then everything is fine. You have the necessary django packages).
As far as I'm concerned, I fixed the issue modifying the sys.path in my Path/to/Project/Project/wsgi.py file.
You have to append your project dir and your virtualenv site-packages to the sys.path List.
Here is my wsgi.py file contained in my project (Talking about the wsgi.py created with django-admin.py start-project)... that I had to modify in order to make it work with Apache
# =====================
# wsgi.py file begin
import os, sys
# add the hellodjango project path into the sys.path
sys.path.append('<PATH_TO_MY_DJANGO_PROJECT>/hellodjango')
# add the virtualenv site-packages path to the sys.path
sys.path.append('<PATH_TO_VIRTUALENV>/Lib/site-packages')
# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
# wsgi.py file end
# ===================
Make sure:
you added mod_wsgi to the Apache modules dir
mod_wsgi must be compiled for the OS, Apache and Python version you have
added the load module command into your httpd.conf to load mod_wsgi module
LoadModule wsgi_module modules/mod_wsgi.so
configured Django specifics in your httpd.conf or any conf you include in your httpd.conf
Based on the documentation How to use Django with Apache and mod_wsgi
WSGIScriptAlias / <PATH_TO_PROJECT>/hellodjango/hellodjango/wsgi.py
WSGIPythonPath <PATH_TO_PROJECT>:<PATH_TO_VIRTUALENV>/Lib/site-packages
<Directory <PATH_TO_PROJECT>/hellodjango/hellodjango>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
Hope this helps. It worked for me.
I had the same issue. My libapache2-mod-wsgi was python 2.x and not python 3.x
found the solution here: https://stackoverflow.com/a/28118284/2489042
credits to #nima
$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3
Warning from #alxs before you copy/paste these commands:
If there are python 2 projects running on the server that use wsgi and apache, the above commands will effectively shut them down.
Add this to the Apache configuration file:
WSGIPythonHome /home/ec2-user/.virtualenvs/mysite-main
I had a similar error just now. It turns out that our Django code was developed on python 3.5, but for some reasons the people who deployed our server setup virtualEnv with python 2.7. We redeployed with python 3.5 and everything worked for us
Below was the error message I received:
$ python serviceStartup.py
Traceback (most recent call last):
File "serviceStartup.py", line 10, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
Hope this will help with anyone seeing a similar error message!
For me, this indicated Django wasn't installed on the sever. Fixed via
pip install Django
In general, make sure you installed requirements:
pip install -r requirements.txt
If using a virtual environment, activate it before installing requirements.
You've configured everything very well my friend, just need to give the apache user permission to access both project and virtualenv dirs.
Example:
sudo chown -R www-data:www-data /home/ubuntu/projects
sudo chown -R www-data:www-data /home/ubuntu/virtualenv
This solved my problem with ImportError: No module named django.core.wsgi (virtualenvs folders) and ImportError: No module named <project-name>.settings (projects folders)
At first glance, I am sorry for my English. I also faced this issue, and I have solved it by changing 'wsgi.py' file to:
import os
import django
from django.core.handlers.wsgi import WSGIHandler
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "eisentask.settings.production")
django.setup(set_prefix=False)
application = WSGIHandler()
I am using Centos and the nginx and gunicorn for my project...
I had this problem...
wsgi file is not reachable maybe it's bcz of the your current directory
I changed my current directory and It worked...
my project name is coda so I typed
cd coda
and then
gunicorn --bind 0.0.0.0:8001 coda.wsgi
In case someone came here using AWS Lightsail bitnami..
The problem is that 'python install packages in a directory that apache do not see'.
so: packages get installed in '/home/bitnami/.local/lib/python3.8/site-packages/' and Apache looks in '/opt/bitnami/python/lib/python3.8/site-packages/'.
The temporary solution I followed is copying packages to Apache eyes folder with this command 'cp -r /home/bitnami/.local/lib/python3.8/site-packages/* /opt/bitnami/python/lib/python3.8/site-packages/'
Hi I got the same issue in Ubuntu 20.04 server. I fixed it by installing apache2-dev
sudo apt-get install apache2-dev
In mod_wsgi documentation its important to have both apache2 and apache2-dev installed.
Run these commands:
$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3
For me it was some variables that needed to be setted (for windows) :
set PYTHONHOME=F:\path\to\python
set PYTHONPATH=F:\path\to\python