seeker
seeker

Reputation: 5

Error [ImportError: No module named 'django'] while trying to integrate apache2 with Django 1.11

I really need help with this as I've been trying to get the bottom of it for a while. I'm trying to get Django to use the apache2 server.However, when I try to log to the apache page I get :


Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log. Apache/2.4.7 (Ubuntu) Server at 127.0.1.1 Port 80


I have copied my version(s) output from the ubuntu 1404LTS


Python 3.6.0+ (default, Feb 4 2017, 11:11:33) [GCC 4.9.4] on linux Type "help", "copyright", "credits" or "license" for more information.


django.VERSION (1, 11, 4, 'final', 0)


dpkg -l | grep wsgi ii libapache2-mod-wsgi-py3 3.4-4ubuntu2.1.14.04.2 amd64 Python 3 WSGI adapter module for Apache


When I open the logs, I see the below:


[Tue Feb 27 02:32:43.409875 2018] [:error] [pid 3211:tid 140311519147904] AssertionError: [Tue Feb 27 02:32:44.388403 2018] [:warn] [pid 3567:tid 139906080114560] mod_wsgi: Compiled for Python/3.4.0. [Tue Feb 27 02:32:44.388477 2018] [:warn] [pid 3567:tid 139906080114560] mod_wsgi: Runtime using Python/3.4.3. [Tue Feb 27 02:32:44.389364 2018] [mpm_event:notice] [pid 3567:tid 139906080114560] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/3.4.3 co$ [Tue Feb 27 02:32:44.389393 2018] [core:notice] [pid 3567:tid 139906080114560] AH00094: Command line: '/usr/sbin/apache2' [Tue Feb 27 02:32:56.461421 2018] [:error] [pid 3570:tid 139905975727872] [remote 127.0.0.1:10860] mod_wsgi (pid=3570): Target WSGI script '/ho$ [Tue Feb 27 02:32:56.461514 2018] [:error] [pid 3570:tid 139905975727872] [remote 127.0.0.1:10860] mod_wsgi (pid=3570): Exception occurred proc$ [Tue Feb 27 02:32:56.474525 2018] [:error] [pid 3570:tid 139905975727872] [remote 127.0.0.1:10860] Traceback (most recent call last): [Tue Feb 27 02:32:56.476070 2018] [:error] [pid 3570:tid 139905975727872] [remote 127.0.0.1:10860] File "/home/serverio/server.io_Tools/server.i$ [Tue Feb 27 02:32:56.476112 2018] [:error] [pid 3570:tid 139905975727872] [remote 127.0.0.1:10860] from django.core.wsgi import get_wsgi_ap$ [Tue Feb 27 02:32:56.476194 2018] [:error] [pid 3570:tid 139905975727872] [remote 127.0.0.1:10860] ImportError: No module named 'django' [Tue Feb 27 02:32:59.026294 2018] [:error] [pid 3570:tid 139905858123520] [remote 127.0.0.1:30046] mod_wsgi (pid=3570): Target WSGI script '/ho$ [Tue Feb 27 02:32:59.026362 2018] [:error] [pid 3570:tid 139905858123520] [remote 127.0.0.1:30046] mod_wsgi (pid=3570): Exception occurred proc$ [Tue Feb 27 02:32:59.026402 2018] [:error] [pid 3570:tid 139905858123520] [remote 127.0.0.1:30046] Traceback (most recent call last): [Tue Feb 27 02:32:59.026461 2018] [:error] [pid 3570:tid 139905858123520] [remote 127.0.0.1:30046] File "/home/serverio/server.io_Tools/server.i$ [Tue Feb 27 02:32:59.026480 2018] [:error] [pid 3570:tid 139905858123520] [remote 127.0.0.1:30046] from django.core.wsgi import get_wsgi_ap$ [Tue Feb 27 02:32:59.026507 2018] [:error] [pid 3570:tid 139905858123520] [remote 127.0.0.1:30046] ImportError: No module named 'django' [Tue Feb 27 02:33:07.853432 2018] [:error] [pid 3570:tid 139905975727872] [remote 127.0.0.1:30046] mod_wsgi (pid=3570): Target WSGI script '/ho$


And this is my 000-default configurations for the apache server:


ServerName serverio

ServerAdmin webmaster@localhost

Alias /static /home/serverio/server.io_Tools/server.io/mysite/userManager/static
    <Directory /home/serverio/server.io_Tools/server.io/mysite/userManager/static>
       Require all granted
     </Directory>

<Directory /home/serverio/server.io_Tools/server.io/mysite/mysite>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIDaemonProcess serverio python-path=/home/serverio/server.io_Tools/server.io:/home/serverio/server.io_Tools/server.io/python3.6/site-packages
WSGIProcessGroup serverio
WSGIScriptAlias / /home/serverio/server.io_Tools/server.io/mysite/mysite/wsgi.py


ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

____________________________________________________________________________

when I tried the steps that Yka advised I get the errors: when I run the configure command, I get the following error:__checking for ___________________________________________________________________________ apxs2... no checking for apxs... no checking for gcc... gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ISO C89... none needed checking for prctl... yes checking Apache version... ./configure: line 2765: apxs: command not found ./configure: line 2765: apxs: command not found ./configure: line 2766: apxs: command not found ./configure: line 2769: /: Is a directory

./configure: line 2964: apxs: command not found configure: creating ./config.status config.status: creating Makefile


when I try to issue sudo make, i get :


apxs -c -I/usr/include/python3.6m -DNDEBUG -Wc,-g -Wc,-O2 src/server/mod_wsgi.c src/server/wsgi_*.c -L/usr/lib -L/usr/lib/python3.6/config-3.6m -lpython3.6m -lpthread -ldl -lutil -lm /bin/sh: 1: apxs: not found make: *** [src/server/mod_wsgi.la] Error 127

YKH, check out the new error i'm getting


when i run :sudo apt-get install apache2-dev it get the below errror as soon as i enter sudo make.

virtual-machine:~/Downloads/mod_wsgi-4.5.24$ sudo make 
/usr/bin/apxs2 -c -I/usr/include/python3.6m -DNDEBUG  -Wc,-g -Wc,-O2  src/server/mod_wsgi.c src/server/wsgi_*.c -L/usr/lib -L/usr/lib/python3.6/config-3.6m  -lpython3.6m -lpthread -ldl  -lutil -lm
/usr/share/apr-1.0/build/libtool --silent --mode=compile --tag=disable-static x86_64-linux-gnu-gcc -std=gnu99 -prefer-pic -pipe -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security  -D_FORTIFY_SOURCE=2   -DLINUX -D_REENTRANT -D_GNU_SOURCE  -pthread  -I/usr/include/apache2  -I/usr/include/apr-1.0   -I/usr/include/apr-1.0 -I/usr/include -g -O2 -I/usr/include/python3.6m -DNDEBUG  -c -o src/server/mod_wsgi.lo src/server/mod_wsgi.c && touch src/server/mod_wsgi.slo
In file included from src/server/mod_wsgi.c:22:0:
src/server/wsgi_python.h:24:20: fatal error: Python.h: No such file or directory
 #include <Python.h>
                    ^
compilation terminated.
apxs:Error: Command failed with rc=65536
.
make: *** [src/server/mod_wsgi.la] Error 1


I also tried this another machine, this time on ubuntu 16LTS and get a similar 
error message as shown below:
_________________________________________________________


  2018] [mpm_event:notice] [pid 65824:tid 140243591341952] AH00491: caught
SIGTERM, shutting down [Tue Feb 27 14:49:42.462678 2018]
[mpm_event:notice] [pid 1366:tid 140394775304064] AH00489: Apache/2.4.18
(Ubuntu) configured -- resuming normal operations [Tue Feb 27
14:49:42.463290 2018] [core:notice] [pid 1366:tid 140394775304064]
AH00094: Command line: '/usr/sbin/apache2' [Tue Feb 27 14:55:49.361004
2018] [mpm_event:notice] [pid 1366:tid 140394775304064] AH00491: caught
SIGTERM, shutting down [Tue Feb 27 14:55:50.444530 2018] [wsgi:warn]
[pid 2393:tid 139752168679296] mod_wsgi: Compiled for Python/3.5.1+.
[Tue Feb 27 14:55:50.444616 2018] [wsgi:warn] [pid 2393:tid
139752168679296] mod_wsgi: Runtime using Python/3.5.2. [Tue Feb 27
14:55:50.445196 2018] [mpm_event:notice] [pid 2393:tid 139752168679296]
AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured
-- resuming normal operations [Tue Feb 27 14:55:50.445223 2018]
[core:notice] [pid 2393:tid 139752168679296] AH00094: Command line:
'/usr/sbin/apache2' [Tue Feb 27 16:26:53.178835 2018] [mpm_event:notice]
[pid 2393:tid 139752168679296] AH00491: caught SIGTERM, shutting down
[Tue Feb 27 16:26:54.191342 2018] [mpm_event:notice] [pid 2972:tid
139914716534656] AH00489: Apache/2.4.18 (Ubuntu) configured -- resuming

Upvotes: 0

Views: 367

Answers (1)

Ykh
Ykh

Reputation: 7717

As can be see by paths in the messages, your mod_wsgi is compiled for Python 3.4. The code you are trying to run for Django is python3.6.

You need to reinstall mod_wsgi compiled against Python 3.X version you want to use.

Note also that you cannot use mod_wsgi compiled for one Python version with a Python virtual environment for a different version. The mod_wsgi must be compiled for the same version.

mod_wsgi issue239

Go here download latest mod_wsgi ,copy it to your server, run:

    tar xvfz mod_wsgi-X.Y.tar.gz
    cd mod_wsgi-X.Y/
    sudo ./configure --with-python=/usr/bin/python3.6
    sudo make
    sudo make install 

    sudo nano /etc/apache2/mods-available/wsgi.load
    LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so # write this to wsgi.load file

    sudo a2enmod wsgi 
    sudo service apache2 restart

The mod_wsgi can't run django project with python3.4 and python3.6 at same time,if you have some project run with python3.4 before,you need update it's virtual environment to python3.6 too, as django2.0(which will stop maintain at 2019/03) is the last version support python3.4,update all project to python3.6 is a good choice,that all!


On ubuntu14.04、Apache 2.4.7、Django 2.0.2

This is how i install python3.6:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6 python3.6-dev python3.6-venv

and apache2:

sudo apt-get update
sudo apt-get install apache2 apache2-dev

create virtual environment with:

python3.6 -m venv environment

Upvotes: 0

Related Questions