Amr Bekhit
Amr Bekhit

Reputation: 4813

Django and apache2 (no virtualenv): ImportError: No module named 'django'

I'm trying to port over a small Django site from one server to another. On the new server, I have the following installed:

Ubuntu 16.04.3 LTS
Python 3.5.2
apache 2.4.18
mod_wsgi 4.5.17 (built from source with Python 3.5)
django 1.11.4 (installed using pip3 **without** a virtualenv)

At the bottom of my apache2.conf file, I added the following lines

# Django settings
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
WSGIPythonPath /home/user/ProductionServer

Where /home/user/ProductionServer is the root directory of my django site.

In sites-enabled/000-default.conf, I have the following:

<VirtualHost *:2799>
        ServerName myserver.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

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

        WSGIScriptAlias / /home/user/ProductionServer/my_app/wsgi.py

        Alias /static /home/user/ProductionServer/static

        <Directory /home/user/ProductionServer/my_app>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>
        <Directory /home/user/ProductionServer/static>
                Require all granted
        </Directory>
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

I can run the site fine using the manage.py runserver command. When I come to run it through apache, however, I got the following error message in the apache2.error file:

[Tue Aug 08 10:53:39.291771 2017] [mpm_event:notice] [pid 1892:tid 139862763386752] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.5.17 Python/3.5 configured -- resuming normal operations
[Tue Aug 08 10:53:39.291903 2017] [core:notice] [pid 1892:tid 139862763386752] AH00094: Command line: '/usr/sbin/apache2'
[Tue Aug 08 10:53:49.966143 2017] [wsgi:error] [pid 1893:tid 139862665881344] [client 85.97.123.183:56389] mod_wsgi (pid=1893): Target WSGI script '/home/user/ProductionServer/my_app/wsgi.py' cannot be loaded as Python module.
[Tue Aug 08 10:53:49.966273 2017] [wsgi:error] [pid 1893:tid 139862665881344] [client 85.97.123.183:56389] mod_wsgi (pid=1893): Exception occurred processing WSGI script '/home/user/ProductionServer/my_app/wsgi.py'.
[Tue Aug 08 10:53:49.966521 2017] [wsgi:error] [pid 1893:tid 139862665881344] [client 85.97.123.183:56389] Traceback (most recent call last):
[Tue Aug 08 10:53:49.966584 2017] [wsgi:error] [pid 1893:tid 139862665881344] [client 85.97.123.183:56389]   File "/home/user/ProductionServer/my_app/wsgi.py", line 12, in <module>
[Tue Aug 08 10:53:49.966595 2017] [wsgi:error] [pid 1893:tid 139862665881344] [client 85.97.123.183:56389]     from django.core.wsgi import get_wsgi_application
[Tue Aug 08 10:53:49.966620 2017] [wsgi:error] [pid 1893:tid 139862665881344] [client 85.97.123.183:56389] ImportError: No module named 'django'

My folder structure is as follows:

ProductionServer
├── another_app
│   ├── ....
├── manage.py
├── my_app
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── static
    └── ...

Any thoughts?

Upvotes: 0

Views: 2521

Answers (2)

Amr Bekhit
Amr Bekhit

Reputation: 4813

The problem was that despite what I initially thought, pip had installed django only for my local user. To install django globally, I had to run pip with the -H option:

sudo -H pip3 install django

Upvotes: 1

Jayground
Jayground

Reputation: 1877

if you use daemon mode, you can add two lines before WSGIScriptAlias

you can check django manual

example

WSGIDaemonProcess projectname python-path=/home/user/ProductServer/:/home/user/myvenv/lib/Python3.5/site-packages
WSGIProcessGroup projectname
WSGIScriptAlias / /home/user/ProductionServer/my_app/wsgi.py

Upvotes: 2

Related Questions