4

I am trying to deploy a very basic Django application with Apache 2.4 on Ubuntu 18.04 without using a virtual environment. When wsgi.py executes, it cannot find django module.

I have tried setting sys.path in wsgi, various solutions that define different configuration settings for 000-default.conf. Changing ownership of the site-packages folder to www-data for Apache, but nothing seems to work. I could make it work using virtualenv but for a production server, I do not want to use virtualenv. I can import django in Python's command line without an issue.

Following is my sample.tst.conf, if have already activated it using a2ensite command.

<code>
<VirtualHost *:80>

    ServerName sample.tst
    ServerAdmin webmaster@sample.tst
    DocumentRoot /var/www/html

    <Directory /home/raza/projects/sample/sample>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess sample python-path=/home/raza/projects/sample
    WSGIProcessGroup sample

    WSGIScriptAlias / /home/raza/projects/sample/sample/wsgi.py

    <Location />
    WSGIProcessGroup sample
    </Location>

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

</VirtualHost>
<code>

How can I fix this problem? I am struggling with this issue for more than a week.

I am a very experienced programmer but very new to Linux, python, and Apache platform, so I may be making some obvious mistake.

I get following error in Apache log file:

[Tue Jul 02 18:05:22.458785 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170] mod_wsgi (pid=12490): Target WSGI script '/home/raza/projects/sample/sample/wsgi.py' cannot be loaded as Python module.
[Tue Jul 02 18:05:22.458854 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170] mod_wsgi (pid=12490): Exception occurred processing WSGI script '/home/raza/projects/sample/sample/wsgi.py'.
[Tue Jul 02 18:05:22.458916 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170] Traceback (most recent call last):
[Tue Jul 02 18:05:22.459009 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170]   File "/home/raza/projects/sample/sample/wsgi.py", line 12, in <module>
[Tue Jul 02 18:05:22.459037 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170]     from django.core.wsgi import get_wsgi_application
[Tue Jul 02 18:05:22.459072 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170] ModuleNotFoundError: No module named 'django'
[Tue Jul 02 18:05:22.490159 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172] mod_wsgi (pid=12490): Target WSGI script '/home/raza/projects/sample/sample/wsgi.py' cannot be loaded as Python module.
[Tue Jul 02 18:05:22.490240 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172] mod_wsgi (pid=12490): Exception occurred processing WSGI script '/home/raza/projects/sample/sample/wsgi.py'.
[Tue Jul 02 18:05:22.490297 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172] Traceback (most recent call last):
[Tue Jul 02 18:05:22.490314 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172]   File "/home/raza/projects/sample/sample/wsgi.py", line 12, in <module>
[Tue Jul 02 18:05:22.490318 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172]     from django.core.wsgi import get_wsgi_application
[Tue Jul 02 18:05:22.490330 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172] ModuleNotFoundError: No module named 'django'
Jasurbek
  • 2,946
  • 3
  • 20
  • 37
Raza
  • 41
  • 1
  • 2
  • 2
    Probably your django is installed under different python environment. I would recommend to check under which python version you have installed django and also check if you use any python env – lefterisnik Jul 02 '19 at 13:17
  • I have only installed Python 3.6, and django is installed in /home/raza/.local/lib/python3.6/site-packages. On this machine, I did not install venv or virtualenv just to avoid any confusion. I can also import django from python3 command line. – Raza Jul 02 '19 at 13:25
  • 1
    try `which python3`. This should print you `home/raza/.local/lib/python3.6/bin/python/` but I assume that it will show you `/usr/bin/python3.6` or `/usr/local/bin/python3.6` which it will mean that you have install django under global python not your python installed on your home folder. – lefterisnik Jul 02 '19 at 13:31
  • You are right, it says /usr/bin/python3. How can I fix this? for my curiosity, why I can import django from python3 command line? – Raza Jul 02 '19 at 13:34
  • Change this file `sample.tst.conf` to point to your global python3 installation. When you type python3 in terminal it executes `/usr/bin/python3` and because I assume you did `sudo pip install django` django was installed under global python3. If you want you can point pip to use your local python (I don't know how you could do that, usually you need to create virtualenv). – lefterisnik Jul 02 '19 at 13:38
  • Does this answer your question? [500 internal server error mod\_wsgi apache "importerror: No Module named 'django'](https://stackoverflow.com/questions/43330231/500-internal-server-error-mod-wsgi-apache-importerror-no-module-named-django). If you are using venv and you have this error, see this [answer](https://stackoverflow.com/a/69068026/8484965) – Mohammad Nazari Sep 06 '21 at 00:12

1 Answers1

1

Looks like django is installed into python2 (which is used by your OS). python3 comes with pre-installed pip3 package installer. So use "sudo pip3 install django" command to install django into python3 environment. pip will only install the packages into python2.

P K
  • 162
  • 12
  • 1
    Thanks for highlighting Python2 possibility. I checked that in my initial attempts to resolve the issue and this was not the case. Problem was that my Django installation was going in usr/local/bin/python3.6 folder instead of usr/bin/python3.6, and Apache could not access that folder. I stopped my efforts on deploying Django globally because I had to give Apache rights to that local folder. Now I am using virtualenv and everythiong is working fine for me. – Raza Aug 09 '19 at 13:10