wsgi: module not found 'django' error when deploying via apache

39 views
Skip to first unread message

Sam Johnson

unread,
Dec 29, 2022, 3:29:54 PM12/29/22
to modwsgi
I have been trying forever to get my django api to deploy correctly via apache on my new ec2 instance running amazon linux 2. I have installed python3-mod_wsgi.x86_64 and my venv is using python 3.7.15. Trying to go to my django app url I am getting a 500 error. The server runs fine locally.

I have imported python3-mod_wsgi.x86_64 in my httpd.conf file via:
LoadModule wsgi_module /etc/httpd/modules/python3-mod_wsgi.x86_64.so

I am pretty new to this set up, happy to provide any additional info needed.

Error log shows:
[Tue Dec 20 21:31:30.690951 2022] [:error] [pid 19216] /usr
[Tue Dec 20 21:31:30.691287 2022] [:error] [pid 19216] mod_wsgi (pid=19216): Target WSGI script '.../project/project/wsgi.py' cannot be loaded as Python module. 
[Tue Dec 20 21:31:30.691323 2022] [:error] [pid 19216] mod_wsgi (pid=19216): Exception occurred processing WSGI script '.../project/project/wsgi.py'.
[Tue Dec 20 21:31:30.691393 2022] [:error] [pid 19216] Traceback (most recent call last): 
[Tue Dec 20 21:31:30.691423 2022] [:error] [pid 19216] File ".../project/project/wsgi.py", line 19, in <module> 
[Tue Dec 20 21:31:30.691428 2022] [:error] [pid 19216] from django.core.wsgi import get_wsgi_application 
[Tue Dec 20 21:31:30.691444 2022] [:error] [pid 19216] ModuleNotFoundError: No module named 'django' 
[Tue Dec 20 21:31:51.190670 2022] [:error] [pid 19217] 3.7.15 (default, Oct 31 2022, 22:44:31) [Tue Dec 20 21:31:51.190707 2022] [:error] [pid 19217] [GCC 7.3.1 20180712 (Red Hat 7.3.1-15)]


My apache conf file:
<VirtualHost *:80> 
ServerName api.project.com 
DocumentRoot path/to/project/root 
WSGIScriptAlias / /path/to/wsgi.py WSGIDaemonProcess project-name processes=4 threads=1 display-name=%{GROUP} python-path=path/to/lib/python3.7/site-packages:/path/to/project/root WSGIProcessGroup project-group 
<Directory "/path/to/project/root"> 
Require all granted 
</Directory> 
#SSL stuff... 
</VirtualHost>

my wsgi.py file:
Screenshot 2022-12-29 at 11.20.06 AM.png

I think I originally had the wrong mod_wsgi installed (built for python2). I removed that and installed python3-mod_wsgi.x86_64. 

I then realized that my mod_wsgi was for python version 3.7, but my application venv was running python 3.6.8. I then wiped the venv and created a new one with python 3.7.15. Changed the conf path to the correct 3.7 site-packages (my venv). Still same error

Would appreciate any help with this, and again, happy to provide more info if needed.


Graham Dumpleton

unread,
Dec 30, 2022, 1:04:29 AM12/30/22
to mod...@googlegroups.com

On 30 Dec 2022, at 4:34 am, Sam Johnson <johnso...@gmail.com> wrote:

I have been trying forever to get my django api to deploy correctly via apache on my new ec2 instance running amazon linux 2. I have installed python3-mod_wsgi.x86_64 and my venv is using python 3.7.15. Trying to go to my django app url I am getting a 500 error. The server runs fine locally.

I have imported python3-mod_wsgi.x86_64 in my httpd.conf file via:
LoadModule wsgi_module /etc/httpd/modules/python3-mod_wsgi.x86_64.so

I am pretty new to this set up, happy to provide any additional info needed.

Error log shows:
[Tue Dec 20 21:31:30.690951 2022] [:error] [pid 19216] /usr
[Tue Dec 20 21:31:30.691287 2022] [:error] [pid 19216] mod_wsgi (pid=19216): Target WSGI script '.../project/project/wsgi.py' cannot be loaded as Python module. 
[Tue Dec 20 21:31:30.691323 2022] [:error] [pid 19216] mod_wsgi (pid=19216): Exception occurred processing WSGI script '.../project/project/wsgi.py'.
[Tue Dec 20 21:31:30.691393 2022] [:error] [pid 19216] Traceback (most recent call last): 
[Tue Dec 20 21:31:30.691423 2022] [:error] [pid 19216] File ".../project/project/wsgi.py", line 19, in <module> 
[Tue Dec 20 21:31:30.691428 2022] [:error] [pid 19216] from django.core.wsgi import get_wsgi_application 
[Tue Dec 20 21:31:30.691444 2022] [:error] [pid 19216] ModuleNotFoundError: No module named 'django' 
[Tue Dec 20 21:31:51.190670 2022] [:error] [pid 19217] 3.7.15 (default, Oct 31 2022, 22:44:31) [Tue Dec 20 21:31:51.190707 2022] [:error] [pid 19217] [GCC 7.3.1 20180712 (Red Hat 7.3.1-15)]


My apache conf file:
<VirtualHost *:80> 
ServerName api.project.com 
DocumentRoot path/to/project/root 
WSGIScriptAlias / /path/to/wsgi.py WSGIDaemonProcess project-name processes=4 threads=1 display-name=%{GROUP} python-path=path/to/lib/python3.7/site-packages:/path/to/project/root WSGIProcessGroup project-group

For a start this is wrong in a number of ways. Try with:

WSGIDaemonProcess project-name processes=4 threads=1 display-name=%{GROUP} python-home=/path/to/venv python-path=/path/to/project/root
WSGIScriptAlias / /path/to/wsgi.py process-group=project-name application-group=%{GLOBAL}

You should not be setting python-path to be the site-packages directory for a virtual environment, you should use python-home and set it to the root of the virtual environment.

Set process-group on WSGIScriptAlias instead of using WSGIProcessGroup as avoids some issues. You didn't have a matching name either way.

Also do tests in:


to ensure mod_wsgi is actually using the Python version you think it is.

Also read:


where it explains how to configure things for a virtual environment.

Also suggest you add:

   WSGIRestrictEmbedded On

outside of any VirtualHost to disable Python in Apache child processes so only enabled in mod_wsgi daemon processes.


<Directory "/path/to/project/root"> 
Require all granted 
</Directory> 
#SSL stuff... 
</VirtualHost>

my wsgi.py file:
<Screenshot 2022-12-29 at 11.20.06 AM.png>

I think I originally had the wrong mod_wsgi installed (built for python2). I removed that and installed python3-mod_wsgi.x86_64. 

I then realized that my mod_wsgi was for python version 3.7, but my application venv was running python 3.6.8. I then wiped the venv and created a new one with python 3.7.15. Changed the conf path to the correct 3.7 site-packages (my venv). Still same error

Would appreciate any help with this, and again, happy to provide more info if needed.



--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/modwsgi/d6358f22-e749-4f55-a9e6-6bf5310076d7n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages